const pandoc = require('node-pandoc'); const utils = require('../utils/utils.js'); const sql = require('../utils/sql'); const argsFull = '-S --base-header-level=1 --toc --toc-depth=3 -N --normalize -s --mathjax -t html5'; const argsPreview = '-S --normalize -s --mathjax -t html5'; module.exports= { generateBlogPost: function(post, blocks) { return new Promise(function(resolve, reject) { Promise.all([module.exports.generateBlogPostHeader(post), module.exports.generateBlogPostBody(post, blocks), module.exports.generateBlogPostFooter()]).then(function(content) { resolve(content.join('')); }) }); }, generateBlogPostHeader: function(post) { var htmlHead = "
"; //image if(!(post.picture_url === "n/a")) { htmlHead +="\"\""; } htmlHead += "
"; //title htmlHead += "

" + post.name + "

"; //date htmlHead += "
" + post.published.toDateString() + "
"; htmlHead +="
" + "
"; return htmlHead; }, generateBlogPostBody: function(post, blocks) { return new Promise(function(resolve, reject) { sql.getCategory(post.category_id).then(function(category) { const pathName = "blogContent/posts/" + category[0].url + "/" + post.url + ".md"; var markDown = utils.getFileContents(pathName).toString(); markDown = markDown.split("(media/").join("(" + "../blogContent/posts/" + category[0].url + "/media/"); module.exports.convertToHTML(markDown, 1).then(function(result) { result = result.split("
").join("
"); if(blocks == -1) resolve(result); var htmlBlocks = result.split("

"); var html = ""; for(var i = 0; i < blocks; i++) { html += "

" + htmlBlocks[i]; } html += "

\n" + "

\n" + "
\n"; resolve(html); }).catch(function(error) { reject(error); }) }); }) }, generateBlogPostFooter: function() { return "


"; }, convertToHTML: function(markdownContents, type) { return new Promise(function(resolve, reject) { // Set your callback function callback = function (err, html) { if (err) { reject(err); } html = html.split("").join(""); resolve(html); }; if(type == -1) { pandoc(markdownContents, argsFull, callback); } else { pandoc(markdownContents, argsPreview, callback); } }); }, }