From c41a72c4d7799db8980d9a1d05dde1e856da8058 Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 14 Jan 2019 21:34:21 -0500 Subject: [PATCH] Updated single page view to work with template engine. --- blog/category.js | 1 - blog/posts.js | 76 +++++++++++++----------------------- blog/renderBlogPost.js | 5 +-- sites/blog.js | 8 ++-- templates/blog/blogMain.html | 41 +++++++++++++++++++ templates/blog/sideBar.html | 36 +++++++++++++++++ utils/sql.js | 9 +---- 7 files changed, 110 insertions(+), 66 deletions(-) create mode 100644 templates/blog/blogMain.html create mode 100644 templates/blog/sideBar.html diff --git a/blog/category.js b/blog/category.js index 8795c28..fcc2b02 100644 --- a/blog/category.js +++ b/blog/category.js @@ -28,7 +28,6 @@ module.exports= { blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext).then(function() { - console.log("good"); resolve(); }); }).catch(function() diff --git a/blog/posts.js b/blog/posts.js index 52ecd03..ff1fd48 100644 --- a/blog/posts.js +++ b/blog/posts.js @@ -1,48 +1,8 @@ /** DB queries */ const sql = require('../utils/sql'); - -/** - * Function responsible for calling the appropriate sql requests to query - * database and serve correct blog post - * - * @param requestURL url requested from client - * @return {*|Promise} returns a resolved promise to preserve execution order - */ -const renderPost = function(requestURL) -{ - return new Promise(function(resolve, reject) - { - const splitURL = requestURL.split("/"); - - //user entered /category/name/ or /category/name - if(splitURL.length == 3 || splitURL.length == 4) - { - sql.getPost(requestURL).then(function(post) - { - if(post != 0) - { - return require(".//singlePost.js").renderPost(post); - } - else - { - reject("Page Not Found"); - } - }).then(function(html) - { - resolve("
" + html + "
"); - }).catch(function(error) - { - reject(error); - }) - } - else - { - reject("Page Not Found"); - } - }); -}; - +/** Object used to render blog post previews */ +const blogBodyRenderer = require('./renderBlogPost'); module.exports= { @@ -52,18 +12,36 @@ module.exports= * @param requestURL * @returns {Promise|*} */ - main: function(requestURL, request) + main: function(requestURL, request, templateContext) { return new Promise(function(resolve, reject) { - Promise.all([renderPost(requestURL), - require("../sidebar/sidebar.js").main()]).then(function(content) + const splitURL = requestURL.split("/"); + + //user entered /category/name/ or /category/name + if(splitURL.length == 3 || splitURL.length == 4) { - resolve(content.join('')); - }).catch(function(error) + sql.getPost(requestURL).then(function(posts) + { + if(posts.length != 0) + { + blogBodyRenderer.renderBatchOfPosts(requestURL, posts, 1, 1, templateContext).then(function() + { + resolve(); + }); + } + else + { + resolve(); + } + + }) + } + else { - reject(error); - }) + //404 will print + resolve(); + } }); } }; \ No newline at end of file diff --git a/blog/renderBlogPost.js b/blog/renderBlogPost.js index 042b2b5..84be023 100644 --- a/blog/renderBlogPost.js +++ b/blog/renderBlogPost.js @@ -94,9 +94,6 @@ module.exports= + postURL + ".md"; var markDown = utils.getFileContents(pathName).toString(); - console.log(pathName); - // console.log(markDown); - markDown = markDown.split("(media/").join("(" + "../blogContent/posts/" + categoryURL + "/media/"); @@ -223,7 +220,7 @@ module.exports= { promises.push(new Promise(function(res, rej) { - module.exports.generateBlogPost(posts[i], 3).then(function(tempContext) + module.exports.generateBlogPost(posts[i], posts.length === 1 ? -1: 3).then(function(tempContext) { res(tempContext); }).catch(function(error) diff --git a/sites/blog.js b/sites/blog.js index a54eec7..430dfae 100644 --- a/sites/blog.js +++ b/sites/blog.js @@ -69,7 +69,7 @@ module.exports= file = "../blog/category.js"; else { - file = "../blog/blog.js"; + file = "../blog/posts.js"; page = 1; // all blog are single page, everyone must be one to ensure // cache is not tricked into storing same blog post a ton of times } @@ -82,10 +82,10 @@ module.exports= require("../sidebar/sidebar.js").main(templateContext)]) .then(function (content) { - console.log(templateContext); - result.write(whiskers.render(content[0], templateContext)); + var html = whiskers.render(content[0], templateContext); + result.write(html); result.end(); - cache.put(filename + "?page=" + page, content.join('')); + cache.put(filename + "?page=" + page, html); }).catch(function (err) { console.log(err); diff --git a/templates/blog/blogMain.html b/templates/blog/blogMain.html new file mode 100644 index 0000000..07188fd --- /dev/null +++ b/templates/blog/blogMain.html @@ -0,0 +1,41 @@ +{header} + +
+ +
+
+ {for post in posts} +
+ {if post.hasPicture} + + {/if} + +
+

{post.name}

+
+ {post.published} +
+ + {post.blogBody} +
+ +
+

+ {else} +
+

Page Not Found

+
+ Page not found +
+
+

+ {/for} +
+
+ {>sideBar} +
+
+ +
+ +{footer} \ No newline at end of file diff --git a/templates/blog/sideBar.html b/templates/blog/sideBar.html new file mode 100644 index 0000000..e79eb2f --- /dev/null +++ b/templates/blog/sideBar.html @@ -0,0 +1,36 @@ +
+ +
+
+ +
+
+ +
Recent Posts
+
+ {for recentPost in recentPosts} + {recentPost.name}
+ {/for} +
+
+
+ +
+
+ +
Categories
+
+ {for cat in categories} + {cat.name}
+ {/for} +
+
+
+ diff --git a/utils/sql.js b/utils/sql.js index 6fad667..183eaeb 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -130,14 +130,7 @@ module.exports= fetch(q2).then(function (result_posts) { - if(result_posts != 0) - { - resolve(result_posts[0]); - } - else - { - resolve(0); - } + resolve(result_posts); }); } else