From 56dc63a7bc22a4df912a73471662949366872eff Mon Sep 17 00:00:00 2001 From: jrtechs Date: Tue, 6 Nov 2018 19:03:54 -0500 Subject: [PATCH] Created system to preview blog posts your working on. --- previewer.js | 57 +++++++++++++++++++++ utils/renderBlogPost.js | 107 +++++++++++++++++++++++++++++----------- utils/sql.js | 3 +- 3 files changed, 136 insertions(+), 31 deletions(-) create mode 100644 previewer.js diff --git a/previewer.js b/previewer.js new file mode 100644 index 0000000..a727ddb --- /dev/null +++ b/previewer.js @@ -0,0 +1,57 @@ +//http server +const http = require('http'); + +//used to parse the request URL +const url = require('url'); + +//express app +const express = require("express"); + +//express app +const app = express(); + +//used to append static content to result +const includes = require('./includes/includes.js'); + +//used to append static content to result +const contentLoader = require('./includes/staticContentServer.js'); + +//port for the server to run on +const port = 8000; + +/** + * Parses the request url and calls correct JS files + */ +app.use(function(request, result) +{ + + const filename = url.parse(request.url, true).pathname; + + if(contentLoader.serveStaticContent(request, result, filename, "")) + { + //do nothing + } + else + { + result.writeHead(200, {'Content-Type': 'text/html'}); + Promise.all([includes.printHeader(), + require('./utils/renderBlogPost.js').generateBlogPostComponent('/programming/', 'cs-theory-exam-2-review', -1), + includes.printFooter()]).then(function (content) + { + result.write(content.join('')); + result.end(); + + }).catch(function (err) + { + console.log(err); + throw err; + }); + } + +}); + + + +http.createServer(app).listen(port); + + diff --git a/utils/renderBlogPost.js b/utils/renderBlogPost.js index 46e492f..ccad6b8 100644 --- a/utils/renderBlogPost.js +++ b/utils/renderBlogPost.js @@ -69,7 +69,7 @@ module.exports= * 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 post stuff from the SQL table * @param blocks * @returns {Promise} */ @@ -79,45 +79,91 @@ module.exports= { 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/"); + resolve(module.exports.generateBlogPost(post.url, category[0].url, blocks)); + // 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("
"); + // + // //this line prevents older versions of pandoc from including invalid cdm scripts + // 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); + // }) - module.exports.convertToHTML(markDown, blocks).then(function(result) - { + }); + }) + }, - result = result.split("
").join("
"); - //this line prevents older versions of pandoc from including invalid cdm scripts - result = result.split("").join(""); + generateBlogPostComponent: function(categoryURL, postURL, blocks) + { + return new Promise(function(resolve, reject) + { + const pathName = "blogContent/posts/" + categoryURL + "/" + + postURL + ".md"; + var markDown = utils.getFileContents(pathName).toString(); + markDown = markDown.split("(media/").join("(" + "../blogContent/posts/" + + categoryURL + "/media/"); - if(blocks == -1) - resolve(result); + module.exports.convertToHTML(markDown, blocks).then(function(result) + { - const htmlBlocks = result.split("

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

" + htmlBlocks[i]; - } + result = result.split("

").join("
"); - html += "
\n" + - "

\n" + - "
\n"; + //this line prevents older versions of pandoc from including invalid cdm scripts + result = result.split("").join(""); - resolve(html); + if(blocks == -1) + resolve(result); - }).catch(function(error) + const htmlBlocks = result.split("

"); + var html = ""; + for(var i = 0; i < blocks; i++) { - reject(error); - }) + html += "

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

\n" + + "

\n" + + "
\n"; + + resolve(html); + + }).catch(function(error) + { + reject(error); + }) }) }, @@ -131,6 +177,7 @@ module.exports= return "

"; }, + /** * Converts markdown into html. * diff --git a/utils/sql.js b/utils/sql.js index fb86cf5..c2521c0 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -19,7 +19,8 @@ const con = mysql.createConnection({ con.connect(function(err) { - if (err) throw err; + if (err) + console.log(err); });