2020-05-19 22:51:04 +02:00
|
|
|
import * as DataEvent from '../../src/com/events/DataEvent';
|
|
|
|
import StringUtils from '../../src/com/utils/StringUtils';
|
2020-06-19 01:21:33 +02:00
|
|
|
import Settings, { SETTINGS_FILE, SETTINGS_TAG } from './Settings';
|
2020-05-19 22:51:04 +02:00
|
|
|
import fs from 'fs-extra';
|
2020-05-21 03:14:19 +02:00
|
|
|
import sanitize from 'sanitize-html';
|
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-06-19 01:21:33 +02:00
|
|
|
const settings = new Settings();
|
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-06-26 05:49:12 +02:00
|
|
|
publishAll(pages, theme, author) {
|
2020-05-19 22:51:04 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2020-06-19 01:21:33 +02:00
|
|
|
settings
|
|
|
|
.load(SETTINGS_FILE)
|
|
|
|
.then(config => {
|
|
|
|
let response = [];
|
|
|
|
let count = _.filter(pages, page => {
|
2020-06-24 22:23:53 +02:00
|
|
|
return page.metadata.deleted === false && page.metadata.published === true;
|
2020-06-19 01:21:33 +02:00
|
|
|
}).length;
|
|
|
|
let rendered = 0;
|
2020-07-02 23:06:57 +02:00
|
|
|
let display_count = 0;
|
|
|
|
let recent = [];
|
2020-07-07 01:12:32 +02:00
|
|
|
let featured = _.filter(pages, page => {
|
|
|
|
return (
|
|
|
|
page.metadata.deleted === false &&
|
|
|
|
page.metadata.published === true &&
|
|
|
|
page.metadata.featured === true
|
|
|
|
);
|
|
|
|
});
|
2020-06-19 01:21:33 +02:00
|
|
|
for (let index = 0; index < pages.length; index++) {
|
2020-07-02 23:06:57 +02:00
|
|
|
pages.sort((a, b) => parseFloat(b.metadata.id) - parseFloat(a.metadata.id));
|
|
|
|
|
2020-06-19 01:21:33 +02:00
|
|
|
const page = pages[index];
|
2020-06-24 22:23:53 +02:00
|
|
|
if (page.metadata.deleted === false && page.metadata.published === true) {
|
2020-07-02 23:06:57 +02:00
|
|
|
if (recent.length < config.global.display_limit) {
|
|
|
|
recent.push({
|
|
|
|
title: page.metadata.title,
|
|
|
|
slug: page.metadata.slug,
|
|
|
|
feature: page.metadata.feature,
|
|
|
|
created: moment(page.metadata.created).fromNow(),
|
|
|
|
path: page.metadata.path
|
|
|
|
});
|
|
|
|
display_count = ++display_count;
|
|
|
|
}
|
2020-06-19 01:21:33 +02:00
|
|
|
let writeFile, template;
|
2020-07-07 01:12:32 +02:00
|
|
|
|
2020-06-19 01:21:33 +02:00
|
|
|
let path =
|
|
|
|
'public/' +
|
|
|
|
moment(page.metadata.created).format('YYYY') +
|
|
|
|
'/' +
|
|
|
|
moment(page.metadata.created).format('MM') +
|
|
|
|
'/';
|
|
|
|
if (page.metadata.layout === 'index') {
|
|
|
|
template = 'content/themes/' + theme + '/index.pug';
|
|
|
|
writeFile = 'public/index.html';
|
|
|
|
} else {
|
|
|
|
writeFile = path + page.metadata.slug + '.html';
|
|
|
|
template = 'content/themes/' + theme + '/page.pug';
|
|
|
|
}
|
|
|
|
|
|
|
|
let buffed = sanitize(page.content, {
|
|
|
|
allowedTags: ['del', 'a', 'iframe', 'img'],
|
|
|
|
allowedAttributes: {
|
|
|
|
a: ['href', 'name', 'target'],
|
|
|
|
img: ['src'],
|
|
|
|
iframe: [
|
|
|
|
'height',
|
|
|
|
'width',
|
|
|
|
'src',
|
|
|
|
'frameborder',
|
|
|
|
'allow',
|
|
|
|
'allowfullscreen'
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
2020-06-26 05:49:12 +02:00
|
|
|
let bag = page.metadata.tags.split(',');
|
|
|
|
let tags = [];
|
|
|
|
for (let index = 0; index < bag.length; index++) {
|
|
|
|
let tag = bag[index].trim();
|
|
|
|
tags.push({
|
|
|
|
label: bag[index],
|
|
|
|
slug: new StringUtils().cleanString(tag)
|
|
|
|
});
|
|
|
|
}
|
2020-06-19 01:21:33 +02:00
|
|
|
buffed = new StringUtils().decodeHTML(buffed);
|
2020-07-07 01:12:32 +02:00
|
|
|
|
2020-06-19 01:21:33 +02:00
|
|
|
let html = md.render(buffed, { html: true, xhtmlOut: true });
|
|
|
|
let file = pug.renderFile(template, {
|
|
|
|
title: page.metadata.title,
|
|
|
|
default_bg: page.metadata.feature,
|
|
|
|
content: html,
|
2020-06-26 05:49:12 +02:00
|
|
|
tags: tags,
|
2020-06-23 02:27:00 +02:00
|
|
|
menu: config.menu,
|
2020-07-02 23:06:57 +02:00
|
|
|
recent_posts: recent,
|
2020-07-07 01:12:32 +02:00
|
|
|
featured_posts: featured,
|
2020-06-26 05:49:12 +02:00
|
|
|
meta: {
|
|
|
|
who: author,
|
|
|
|
when: moment(page.metadata.created).fromNow(),
|
|
|
|
tags: tags
|
|
|
|
},
|
2020-06-23 02:27:00 +02:00
|
|
|
welcome_message: page.metadata.title
|
2020-06-19 01:21:33 +02:00
|
|
|
});
|
2020-05-27 22:16:23 +02:00
|
|
|
|
2020-06-19 01:21:33 +02:00
|
|
|
fs.ensureDir(path).then(() => {
|
|
|
|
fs.writeFile(writeFile, file, err => {
|
|
|
|
// throws an error, you could also catch it here
|
|
|
|
if (err) {
|
|
|
|
response = {
|
|
|
|
type: DataEvent.PAGES_NOT_RENDERED,
|
|
|
|
message: err
|
|
|
|
};
|
|
|
|
reject(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
// success case, the file was saved
|
|
|
|
});
|
|
|
|
});
|
|
|
|
rendered = ++rendered;
|
|
|
|
if (rendered === count) {
|
|
|
|
response = {
|
|
|
|
type: DataEvent.PAGES_RENDERED,
|
|
|
|
message: 'All Pages Rendered. Sweet.'
|
|
|
|
};
|
|
|
|
resolve(response);
|
|
|
|
}
|
|
|
|
} else {
|
2020-06-24 22:23:53 +02:00
|
|
|
if (count === 0) {
|
|
|
|
response = {
|
|
|
|
type: DataEvent.PAGES_RENDERED,
|
|
|
|
message: 'No page rendering needed'
|
|
|
|
};
|
|
|
|
resolve(response);
|
|
|
|
}
|
2020-06-21 23:23:23 +02:00
|
|
|
//check to see if deleted pages have been renderered and delete them
|
2020-06-24 22:23:53 +02:00
|
|
|
if (page.metadata.layout !== 'index') {
|
|
|
|
fs.unlink(
|
|
|
|
'public/' +
|
|
|
|
page.metadata.path +
|
|
|
|
'/' +
|
|
|
|
page.metadata.slug +
|
|
|
|
'.html'
|
2020-06-26 05:49:12 +02:00
|
|
|
)
|
|
|
|
.then()
|
2020-06-29 22:45:35 +02:00
|
|
|
.catch(() => {
|
|
|
|
//console.log('ERROR', err);
|
2020-06-26 05:49:12 +02:00
|
|
|
});
|
2020-06-24 22:23:53 +02:00
|
|
|
}
|
2020-06-14 23:46:45 +02:00
|
|
|
}
|
2020-06-19 01:21:33 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
publishTags(pages) {
|
|
|
|
let self = this;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
self.loadRenderData()
|
|
|
|
.then(result => {
|
|
|
|
let tags = result.tags.tags;
|
|
|
|
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];
|
2020-05-19 22:51:04 +02:00
|
|
|
|
2020-06-29 22:45:35 +02:00
|
|
|
//TODO: filter for deleted and unpublished pages
|
2020-07-09 21:57:03 +02:00
|
|
|
if (
|
|
|
|
page.metedata.deleted === false &&
|
|
|
|
page.metadata.published === true
|
|
|
|
) {
|
|
|
|
if (_.includes(page.metadata.tags, tag.tag_name)) {
|
|
|
|
pageList.push({
|
|
|
|
title: page.metadata.title,
|
|
|
|
slug: page.metadata.slug
|
|
|
|
});
|
|
|
|
}
|
2020-06-19 01:21:33 +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/' + result.settings.global.theme + '/tags.pug',
|
|
|
|
{
|
|
|
|
title: item.tag,
|
|
|
|
default_bg: result.settings.global.background,
|
|
|
|
content_tags: 'THESE ARE TAGS',
|
2020-06-22 04:01:22 +02:00
|
|
|
tag_list: item.tag_list,
|
|
|
|
menu: result.settings.menu
|
2020-06-19 01:21:33 +02:00
|
|
|
}
|
|
|
|
);
|
2020-06-21 03:13:23 +02:00
|
|
|
fs.ensureDir('public/tags', () => {
|
|
|
|
fs.writeFile('public/tags/' + item.slug + '.html', file, err => {
|
|
|
|
// throws an error, you could also catch it here
|
|
|
|
if (err) {
|
|
|
|
response = {
|
|
|
|
type: DataEvent.TAG_PAGES_NOT_RENDERED,
|
|
|
|
message: err
|
|
|
|
};
|
|
|
|
reject(response);
|
|
|
|
}
|
|
|
|
// success case, the file was saved
|
|
|
|
response = {
|
|
|
|
type: DataEvent.TAG_PAGES_RENDERED,
|
|
|
|
message: 'Tag Pages ready to go. Good job.'
|
|
|
|
};
|
|
|
|
resolve(response);
|
|
|
|
});
|
2020-06-14 23:46:45 +02:00
|
|
|
});
|
|
|
|
}
|
2020-06-19 01:21:33 +02:00
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
2020-05-21 03:14:19 +02:00
|
|
|
});
|
|
|
|
}
|
2020-06-19 01:21:33 +02:00
|
|
|
publishArchive(archive) {
|
|
|
|
settings
|
|
|
|
.load(SETTINGS_FILE)
|
|
|
|
.then(settings => {
|
|
|
|
let file = pug.renderFile(
|
|
|
|
'content/themes/' + settings.global.theme + '/archive.pug',
|
|
|
|
{
|
|
|
|
title: 'ARCHIVES',
|
|
|
|
default_bg: settings.global.background,
|
|
|
|
content_tags: 'COLD STORAGE',
|
2020-06-22 04:01:22 +02:00
|
|
|
archives: archive,
|
|
|
|
menu: settings.menu
|
2020-05-19 22:51:04 +02:00
|
|
|
}
|
2020-06-19 01:21:33 +02:00
|
|
|
);
|
2020-05-21 03:14:19 +02:00
|
|
|
|
2020-06-19 01:21:33 +02:00
|
|
|
fs.writeFile('public/archives.html', file, err => {
|
2020-05-21 03:14:19 +02:00
|
|
|
// throws an error, you could also catch it here
|
|
|
|
if (err) {
|
2020-06-24 22:26:13 +02:00
|
|
|
//console.log('ERROR', err);
|
2020-06-19 01:21:33 +02:00
|
|
|
//response = { type: DataEvent.TAG_PAGES_NOT_RENDERED, message: err };
|
2020-05-21 03:14:19 +02:00
|
|
|
}
|
|
|
|
// success case, the file was saved
|
2020-05-19 22:51:04 +02:00
|
|
|
});
|
2020-06-19 01:21:33 +02:00
|
|
|
})
|
2020-06-24 22:26:13 +02:00
|
|
|
.catch(() => {
|
|
|
|
//console.log(err);
|
2020-06-19 01:21:33 +02:00
|
|
|
});
|
2020-05-19 22:51:04 +02:00
|
|
|
}
|
2020-06-19 01:21:33 +02:00
|
|
|
loadRenderData() {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let getSettings = settings.load(SETTINGS_FILE);
|
|
|
|
let getTags = settings.load(SETTINGS_TAG);
|
|
|
|
Promise.all([getSettings, getTags])
|
|
|
|
.then(result => {
|
|
|
|
const [settings, tags] = result;
|
|
|
|
let data = { settings: settings, tags: tags };
|
|
|
|
resolve(data);
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
reject(err);
|
|
|
|
});
|
2020-06-15 03:18:31 +02:00
|
|
|
});
|
|
|
|
}
|
2020-05-19 22:51:04 +02:00
|
|
|
|
|
|
|
//--------------------------
|
|
|
|
// event handlers
|
|
|
|
//--------------------------
|
|
|
|
}
|