fipamo/brain/data/Render.js

82 lines
2.1 KiB
JavaScript

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';
const pug = require('pug');
const md = require('markdown-it')('commonmark');
export default class Render {
//--------------------------
// constructor
//--------------------------
constructor() {}
//--------------------------
// methods
//--------------------------
start() {}
/**
* Renders all pages from markdown to html
* @parameter theme: current theme being used as defined in settings
*/
publishAll(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
});
fs.writeFile('public/index.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);
});
}
}
})
.catch(err => {
reject(err);
});
});
}
//--------------------------
// event handlers
//--------------------------
}