2020-05-19 22:51:04 +02:00
|
|
|
import * as DataEvent from '../../src/com/events/DataEvent';
|
|
|
|
import StringUtils from '../../src/com/utils/StringUtils';
|
|
|
|
import fs from 'fs-extra';
|
2020-05-21 03:14:19 +02:00
|
|
|
import sanitize from 'sanitize-html';
|
2020-06-11 22:22:00 +02:00
|
|
|
import Settings from './Settings';
|
2020-05-21 03:14:19 +02:00
|
|
|
const config = require('../../site/settings.json');
|
2020-05-22 06:18:32 +02:00
|
|
|
const tags_list = require('../../site/tags.json');
|
2020-05-19 22:51:04 +02:00
|
|
|
const pug = require('pug');
|
|
|
|
const md = require('markdown-it')('commonmark');
|
2020-05-21 03:14:19 +02:00
|
|
|
const _ = require('lodash');
|
2020-05-27 22:16:23 +02:00
|
|
|
const moment = require('moment');
|
2020-05-19 22:51:04 +02:00
|
|
|
|
|
|
|
export default class Render {
|
|
|
|
//--------------------------
|
|
|
|
// constructor
|
|
|
|
//--------------------------
|
|
|
|
constructor() {}
|
|
|
|
//--------------------------
|
|
|
|
// methods
|
|
|
|
//--------------------------
|
|
|
|
start() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders all pages from markdown to html
|
2020-05-21 03:14:19 +02:00
|
|
|
* @parameter pages: payload for site pages
|
2020-05-19 22:51:04 +02:00
|
|
|
* @parameter theme: current theme being used as defined in settings
|
|
|
|
*/
|
2020-05-21 03:14:19 +02:00
|
|
|
publishAll(pages, theme) {
|
2020-05-19 22:51:04 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-05-21 03:14:19 +02:00
|
|
|
let response = [];
|
|
|
|
for (let index = 0; index < pages.length; index++) {
|
|
|
|
const page = pages[index];
|
2020-05-27 22:16:23 +02:00
|
|
|
let writeFile, template;
|
|
|
|
let path =
|
|
|
|
'public/' +
|
|
|
|
moment(page.metadata.created).format('YYYY') +
|
|
|
|
'/' +
|
|
|
|
moment(page.metadata.created).format('MM') +
|
|
|
|
'/';
|
2020-05-21 03:14:19 +02:00
|
|
|
if (page.metadata.layout === 'index') {
|
2020-05-27 22:16:23 +02:00
|
|
|
template = 'content/themes/' + theme + '/index.pug';
|
|
|
|
writeFile = 'public/index.html';
|
|
|
|
} else {
|
|
|
|
writeFile = path + page.metadata.slug + '.html';
|
|
|
|
template = 'content/themes/' + theme + '/page.pug';
|
|
|
|
}
|
2020-05-19 22:51:04 +02:00
|
|
|
|
2020-05-27 22:16:23 +02:00
|
|
|
let buffed = sanitize(page.content, {
|
|
|
|
allowedTags: ['del', 'a', 'iframe', 'img'],
|
|
|
|
allowedAttributes: {
|
|
|
|
a: ['href', 'name', 'target'],
|
|
|
|
img: ['src'],
|
|
|
|
iframe: [
|
|
|
|
'height',
|
|
|
|
'width',
|
|
|
|
'src',
|
|
|
|
'frameborder',
|
|
|
|
'allow',
|
|
|
|
'allowfullscreen'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
buffed = new StringUtils().decodeHTML(buffed);
|
|
|
|
let html = md.render(buffed, { html: true, xhtmlOut: true });
|
|
|
|
let file = pug.renderFile(template, {
|
|
|
|
title: page.metadata.title,
|
|
|
|
default_bg: page.metadata.feature,
|
2020-05-29 01:13:52 +02:00
|
|
|
content: html,
|
2020-06-11 22:22:00 +02:00
|
|
|
tags: page.metadata.tags,
|
|
|
|
menu: config.menu
|
2020-05-27 22:16:23 +02:00
|
|
|
});
|
2020-05-19 22:51:04 +02:00
|
|
|
|
2020-05-27 22:16:23 +02:00
|
|
|
fs.ensureDir(path).then(() => {
|
|
|
|
fs.writeFile(writeFile, file, err => {
|
2020-05-21 03:14:19 +02:00
|
|
|
// throws an error, you could also catch it here
|
2020-05-19 22:51:04 +02:00
|
|
|
|
2020-05-21 03:14:19 +02:00
|
|
|
if (err) {
|
|
|
|
response = { type: DataEvent.PAGES_NOT_RENDERED, message: err };
|
|
|
|
reject(response);
|
2020-05-19 22:51:04 +02:00
|
|
|
}
|
2020-05-21 03:14:19 +02:00
|
|
|
|
|
|
|
// success case, the file was saved
|
|
|
|
response = {
|
|
|
|
type: DataEvent.PAGES_RENDERED,
|
|
|
|
message: 'All Pages Rendered. Sweet.'
|
|
|
|
};
|
|
|
|
resolve(response);
|
|
|
|
});
|
2020-05-27 22:16:23 +02:00
|
|
|
});
|
2020-05-21 03:14:19 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
publishTags(pages) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-05-22 06:18:32 +02:00
|
|
|
let tags = tags_list.tags;
|
2020-05-21 03:14:19 +02:00
|
|
|
let renderList = [];
|
|
|
|
for (let index = 0; index < tags.length; index++) {
|
|
|
|
let tag = tags[index];
|
|
|
|
//console.log('**TAG**', tag.tag_name);
|
|
|
|
var pageList = [];
|
|
|
|
for (let i = 0; i < pages.length; i++) {
|
|
|
|
let page = pages[i];
|
|
|
|
|
|
|
|
if (_.includes(page.metadata.tags, tag.tag_name)) {
|
|
|
|
pageList.push({ title: page.metadata.title, slug: page.metadata.slug });
|
2020-05-19 22:51:04 +02:00
|
|
|
}
|
2020-05-21 03:14:19 +02:00
|
|
|
}
|
|
|
|
renderList.push({ tag: tag.tag_name, tag_list: pageList, slug: tag.slug });
|
|
|
|
}
|
|
|
|
let response = [];
|
|
|
|
for (let index = 0; index < renderList.length; index++) {
|
|
|
|
let item = renderList[index];
|
|
|
|
let file = pug.renderFile('content/themes/' + config.global.theme + '/tags.pug', {
|
|
|
|
title: item.tag,
|
|
|
|
default_bg: config.global.background,
|
|
|
|
content_tags: 'THESE ARE TAGS',
|
|
|
|
tag_list: item.tag_list
|
|
|
|
});
|
|
|
|
|
|
|
|
fs.writeFile('public/tags/' + item.slug + '.html', file, err => {
|
|
|
|
// throws an error, you could also catch it here
|
|
|
|
if (err) {
|
2020-05-21 23:13:03 +02:00
|
|
|
response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
|
2020-05-21 03:14:19 +02:00
|
|
|
reject(response);
|
|
|
|
}
|
|
|
|
// success case, the file was saved
|
|
|
|
response = {
|
2020-05-21 23:13:03 +02:00
|
|
|
type: DataEvent.TAG_PAGES_RENDERED,
|
|
|
|
message: 'Tag Pages ready to go. Good job.'
|
2020-05-21 03:14:19 +02:00
|
|
|
};
|
|
|
|
resolve(response);
|
2020-05-19 22:51:04 +02:00
|
|
|
});
|
2020-05-21 03:14:19 +02:00
|
|
|
}
|
2020-05-19 22:51:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------
|
|
|
|
// event handlers
|
|
|
|
//--------------------------
|
|
|
|
}
|