|
|
|
@ -1,9 +1,11 @@
|
|
|
|
|
import * as DataEvent from '../../src/com/events/DataEvent';
|
|
|
|
|
import sanitize from 'sanitize-html';
|
|
|
|
|
import StringUtils from '../../src/com/utils/StringUtils';
|
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
|
import sanitize from 'sanitize-html';
|
|
|
|
|
const config = require('../../site/settings.json');
|
|
|
|
|
const pug = require('pug');
|
|
|
|
|
const md = require('markdown-it')('commonmark');
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
|
|
export default class Render {
|
|
|
|
|
//--------------------------
|
|
|
|
@ -17,61 +19,101 @@ export default class Render {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Renders all pages from markdown to html
|
|
|
|
|
* @parameter pages: payload for site pages
|
|
|
|
|
* @parameter theme: current theme being used as defined in settings
|
|
|
|
|
*/
|
|
|
|
|
publishAll(theme) {
|
|
|
|
|
publishAll(pages, theme) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this.getPage()
|
|
|
|
|
.then(pages => {
|
|
|
|
|
let response = [];
|
|
|
|
|
for (let index = 0; index < pages.length; index++) {
|
|
|
|
|
const page = pages[index];
|
|
|
|
|
if (page.metadata.layout === 'index') {
|
|
|
|
|
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('content/themes/' + theme + '/index.pug', {
|
|
|
|
|
title: page.metadata.title,
|
|
|
|
|
default_bg: page.metadata.feature,
|
|
|
|
|
content_index: html
|
|
|
|
|
});
|
|
|
|
|
let response = [];
|
|
|
|
|
for (let index = 0; index < pages.length; index++) {
|
|
|
|
|
const page = pages[index];
|
|
|
|
|
if (page.metadata.layout === 'index') {
|
|
|
|
|
let buffed = sanitize(page.content, {
|
|
|
|
|
allowedTags: ['del', 'a', 'iframe', 'img'],
|
|
|
|
|
allowedAttributes: {
|
|
|
|
|
a: ['href', 'name', 'target'],
|
|
|
|
|
img: ['src'],
|
|
|
|
|
iframe: [
|
|
|
|
|
'height',
|
|
|
|
|
'width',
|
|
|
|
|
'src',
|
|
|
|
|
'frameborder',
|
|
|
|
|
'allow',
|
|
|
|
|
'allowfullscreen'
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
fs.writeFile('public/index.html', file, err => {
|
|
|
|
|
// throws an error, you could also catch it here
|
|
|
|
|
buffed = new StringUtils().decodeHTML(buffed);
|
|
|
|
|
let html = md.render(buffed, { html: true, xhtmlOut: true });
|
|
|
|
|
let file = pug.renderFile('content/themes/' + theme + '/index.pug', {
|
|
|
|
|
title: page.metadata.title,
|
|
|
|
|
default_bg: page.metadata.feature,
|
|
|
|
|
content_index: html
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
response = { type: DataEvent.PAGES_NOT_RENDERED, message: err };
|
|
|
|
|
reject(response);
|
|
|
|
|
}
|
|
|
|
|
fs.writeFile('public/index.html', file, err => {
|
|
|
|
|
// throws an error, you could also catch it here
|
|
|
|
|
|
|
|
|
|
// success case, the file was saved
|
|
|
|
|
response = {
|
|
|
|
|
type: DataEvent.PAGES_RENDERED,
|
|
|
|
|
message: 'All Pages Rendered. Sweet.'
|
|
|
|
|
};
|
|
|
|
|
resolve(response);
|
|
|
|
|
});
|
|
|
|
|
if (err) {
|
|
|
|
|
response = { type: DataEvent.PAGES_NOT_RENDERED, message: err };
|
|
|
|
|
reject(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// success case, the file was saved
|
|
|
|
|
response = {
|
|
|
|
|
type: DataEvent.PAGES_RENDERED,
|
|
|
|
|
message: 'All Pages Rendered. Sweet.'
|
|
|
|
|
};
|
|
|
|
|
resolve(response);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
publishTags(pages) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
let tags = config.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];
|
|
|
|
|
|
|
|
|
|
if (_.includes(page.metadata.tags, tag.tag_name)) {
|
|
|
|
|
pageList.push({ title: page.metadata.title, slug: page.metadata.slug });
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
reject(err);
|
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
|
response = { type: DataEvent.PAGES_NOT_RENDERED, message: err };
|
|
|
|
|
reject(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// success case, the file was saved
|
|
|
|
|
response = {
|
|
|
|
|
type: DataEvent.PAGES_RENDERED,
|
|
|
|
|
message: 'All Pages Rendered. Sweet.'
|
|
|
|
|
};
|
|
|
|
|
resolve(response);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|