const utils = require('../utils/utils.js'); const markdown = require( "markdown" ).markdown; const sql = require('../utils/sql'); var Remarkable = require('remarkable'); var hljs = require('highlight.js'); // Actual default values var md = new Remarkable( { html: true, highlight: function (str, lang) { if (lang && hljs.getLanguage(lang)) { try { return hljs.highlight(lang, str).value; } catch (err) {} } try { return hljs.highlightAuto(str).value; } catch (err) {} return ''; // use external default escaping } }); module.exports= { /** * Renders a preview of the post with a link to view more * * @param res * @param post */ renderPreview: function(post) { return new Promise(function(resolve, reject) { //var html = "
"; var html = "
"; //image if(!(post.picture_url === "n/a")) { html +="\"\""; } html += "
"; //title html += "

" + post.name + "

"; //date html += "
" + post.published.toDateString() + "
"; html +="
"; html += "
"; try { sql.getCategory(post.category_id).then(function(category) { var pathName = "entries/" + category[0].url + "/" + post.url + ".md"; var markDown = utils.getFileContents(pathName).toString(); markDown = markDown.split("(media/").join("(" + "../entries/" + category[0].url + "/media/"); var htmlPost = md.render(markDown).split("

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

" + htmlPost[i]; } html = html.split("").join(""); html += "

\n" + "

\n" + "
\n"; html += "


"; resolve(html) }).catch(function(error) { reject(error); }); } catch(ex) { reject(ex); } }); }, /** * renderPost() displays a single blog post in it's entirety * * @param res result sent to user * @param post sql data about the blog post * @return {*|Promise} */ renderPost: function(post) { return new Promise(function (resolve, reject) { var htmlHead = "
"; //image if(!(post.picture_url === "n/a")) { htmlHead +="\"\""; } htmlHead += "
"; //title htmlHead += "

" + post.name + "

"; //date htmlHead += "
" + post.published.toDateString() + "
"; htmlHead +="
"; var html = "
"; try { sql.getCategory(post.category_id).then(function(category) { var pathName = "entries/" + category[0].url + "/" + post.url + ".md"; var markDown = utils.getFileContents(pathName).toString(); markDown = markDown.split("(media/").join("(" + "../entries/" + category[0].url + "/media/"); html += md.render(markDown); html = html.split("").join(""); html += "


"; resolve(htmlHead + html); }); } catch(ex) { reject(ex); } }); } };