From 6c998ce20dad26014cedb062e5a9ee670603075e Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sun, 7 Oct 2018 12:41:06 -0400 Subject: [PATCH] Fixed issue with the read more button on preview. --- utils/renderBlogPost.js | 167 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 utils/renderBlogPost.js diff --git a/utils/renderBlogPost.js b/utils/renderBlogPost.js new file mode 100644 index 0000000..5c4ef69 --- /dev/null +++ b/utils/renderBlogPost.js @@ -0,0 +1,167 @@ +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= + { + + /** + * Renders the entire blog post based on the sql data pulled + * from the database. + * + * @param post sql data which has title, date, and header img location + * @param blocks number of blocks to display for a preview or -1 for + * all the blocks + * @returns {Promise} async call which renders the entire blog post. + */ + 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('')); + }).catch(function(error) + { + reject(error); + }) + }); + }, + + /** + * Renders the header of the blog post which contains the header image, and date + * published. + * + * @param post sql data + * @returns {string} + */ + 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; + }, + + + /** + * Method which renders the body of the blog post. This is responsible for getting + * the contents of the markdown/latex file and rendering it into beautiful html. + * + * @param post + * @param blocks + * @returns {Promise} + */ + 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, blocks).then(function(result) + { + + result = result.split("
").join("
"); + + if(blocks == -1) + resolve(result); + + const 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); + }) + + }); + }) + }, + + + /** Method to return the footer of the html blog post. + * + * @returns {string} + */ + generateBlogPostFooter: function() + { + return "


"; + }, + + /** + * Converts markdown into html. + * + * @param markdownContents + * @param type + * @returns {Promise} + */ + 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); + } + }); + }, + }