From d094cac4b23cf1764075318cb00c6b3d1b147dba Mon Sep 17 00:00:00 2001 From: jrtechs Date: Mon, 14 Jan 2019 20:21:53 -0500 Subject: [PATCH] Updated the home page to use the templating engine. --- admin/admin.js | 6 +- admin/adminHome.js | 2 +- admin/posts.js | 12 +-- {posts => blog}/category.js | 6 +- blog/homePage.js | 27 +++++ {posts => blog}/posts.js | 5 +- {utils => blog}/renderBlogPost.js | 101 ++++++++++++------ {posts => blog}/renderNextBar.js | 8 +- .../node-website-optimization.md | 2 +- includes/contact.js | 2 +- posts/homePage.js | 46 -------- posts/renderBatchOfPreviewes.js | 55 ---------- posts/singlePost.js | 28 ----- sites/admin.js | 2 +- sites/blog.js | 10 +- sites/projects.js | 2 +- utils/sql.js | 2 +- 17 files changed, 128 insertions(+), 188 deletions(-) rename {posts => blog}/category.js (87%) create mode 100644 blog/homePage.js rename {posts => blog}/posts.js (91%) rename {utils => blog}/renderBlogPost.js (72%) rename {posts => blog}/renderNextBar.js (89%) delete mode 100644 posts/homePage.js delete mode 100644 posts/renderBatchOfPreviewes.js delete mode 100644 posts/singlePost.js diff --git a/admin/admin.js b/admin/admin.js index 5a08579..b453855 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -2,7 +2,7 @@ * Determines what template and controls that will be * displayed based on the url such as * / - * /posts + * /blog * /downloads * * For each controls it calls that "pages" associated javascript file @@ -44,9 +44,9 @@ module.exports= page = "./adminDownloads.js"; console.log("downloads time") } - else if(filename.includes("/posts")) + else if(filename.includes("/blog")) { - page = "./posts.js"; + page = "./blog.js"; } require(page).main(postData, templateContext).then(function(template) diff --git a/admin/adminHome.js b/admin/adminHome.js index c1dbdd0..2bc4e4c 100644 --- a/admin/adminHome.js +++ b/admin/adminHome.js @@ -75,7 +75,7 @@ const processPost = function(postData) urls =urls.toLowerCase(); - var q = "insert into posts (category_id, picture_url, published, name, url) values "; + var q = "insert into blog (category_id, picture_url, published, name, url) values "; q += "('" + post.add_post_category + "', '" + post.add_post_picture + "', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')"; diff --git a/admin/posts.js b/admin/posts.js index 3a98894..019fb14 100644 --- a/admin/posts.js +++ b/admin/posts.js @@ -1,5 +1,5 @@ /** Whiskers template file - * this has stuff for both editing posts and viewing a list of posts*/ + * this has stuff for both editing blog and viewing a list of blog*/ const TEMPLATE_FILE = "admin/adminPosts.html"; const includes = require('../includes/includes.js'); @@ -11,7 +11,7 @@ const qs = require('querystring'); /** - * Detects if the post data came from the edit form in posts table or edit post + * Detects if the post data came from the edit form in blog table or edit post * in the edit post form. * * @param postData @@ -53,7 +53,7 @@ const processPostData = function(postData, renderContext) /** - * Grabs and appends the list of posts from the SQL database to + * Grabs and appends the list of blog from the SQL database to * the template context for the template renderer. * * @param templateContext @@ -77,8 +77,8 @@ const fetchPostsInformation = function(templateContext) module.exports= { /** - * Fetches context information for the admin posts page and handles post - * data sent regarding editing posts. + * Fetches context information for the admin blog page and handles post + * data sent regarding editing blog. * * @param postData posted by user * @param templateContext json object used as the template context @@ -95,7 +95,7 @@ module.exports= resolve(template[0]); }).catch(function(error) { - console.log("error in add admin posts.js"); + console.log("error in add admin blog.js"); reject(error); }); }); diff --git a/posts/category.js b/blog/category.js similarity index 87% rename from posts/category.js rename to blog/category.js index d30e7b2..1c408c4 100644 --- a/posts/category.js +++ b/blog/category.js @@ -2,10 +2,10 @@ const sql = require('../utils/sql'); /** Object used to render blog post previews */ -const batchPreview = require('../posts/renderBatchOfPreviewes'); +const batchPreview = require('.//renderBatchOfPreviewes'); /** - * Renders all posts in a single category + * Renders all blog in a single category * * @param resultURL * @returns {*} @@ -35,7 +35,7 @@ const renderPosts = function(resultURL, page) module.exports= { /** - * Calls posts and sidebar modules to render blog contents in order + * Calls blog and sidebar modules to render blog contents in order * * @param requestURL * @param request diff --git a/blog/homePage.js b/blog/homePage.js new file mode 100644 index 0000000..fe55503 --- /dev/null +++ b/blog/homePage.js @@ -0,0 +1,27 @@ +const sql = require('../utils/sql'); + +const blogPostRenderer = require('./renderBlogPost.js'); + +module.exports= + { + /** + * Renders the previews of recent blog blog and the side bar + * + * @param res + * @param fileName request url + */ + main: function(requestURL, request, templateContext) + { + var page = request.query.page; + return new Promise(function(resolve, reject) + { + sql.getRecentPostSQL().then(function(posts) + { + resolve(blogPostRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext)); + }).catch(function(error) + { + reject(error); + }) + }); + } + }; \ No newline at end of file diff --git a/posts/posts.js b/blog/posts.js similarity index 91% rename from posts/posts.js rename to blog/posts.js index 9a2da59..52ecd03 100644 --- a/posts/posts.js +++ b/blog/posts.js @@ -22,7 +22,7 @@ const renderPost = function(requestURL) { if(post != 0) { - return require("../posts/singlePost.js").renderPost(post); + return require(".//singlePost.js").renderPost(post); } else { @@ -43,10 +43,11 @@ const renderPost = function(requestURL) }); }; + module.exports= { /** - * Calls posts and sidebar modules to render blog contents in order + * Calls blog and sidebar modules to render blog contents in order * * @param requestURL * @returns {Promise|*} diff --git a/utils/renderBlogPost.js b/blog/renderBlogPost.js similarity index 72% rename from utils/renderBlogPost.js rename to blog/renderBlogPost.js index 36832b2..0c2f073 100644 --- a/utils/renderBlogPost.js +++ b/blog/renderBlogPost.js @@ -25,10 +25,10 @@ module.exports= return new Promise(function(resolve, reject) { Promise.all([module.exports.generateBlogPostHeader(post), - module.exports.generateBlogPostBody(post, blocks), - module.exports.generateBlogPostFooter()]).then(function(content) + module.exports.generateBlogPostBody(post, blocks)]) + .then(function() { - resolve(content.join('')); + resolve(post); }).catch(function(error) { reject(error); @@ -45,23 +45,11 @@ module.exports= */ generateBlogPostHeader: function(post) { - var htmlHead = "
"; - //image - if(!(post.picture_url === "n/a")) - { - htmlHead +="\"\""; - } - - htmlHead += "
"; - //title - htmlHead += "

" + post.name + "

"; - //date - htmlHead += "
" + - post.published.toDateString() + "
"; - htmlHead +="
" + "
"; + if(post.picture_url !== "n/a") + post. hasPicture = true; - return htmlHead; + post.published = post.published.toDateString(); + return; }, @@ -79,7 +67,11 @@ module.exports= { sql.getCategory(post.category_id).then(function(category) { - resolve(module.exports.generateBlogPostComponent(category[0].url, post.url, blocks)); + module.exports.generateBlogPostComponent(category[0].url, post.url, blocks).then(function(html) + { + post.blogBody = html; + resolve(); + }); }); }) }, @@ -101,6 +93,10 @@ module.exports= const pathName = "blogContent/posts/" + categoryURL + "/" + postURL + ".md"; var markDown = utils.getFileContents(pathName).toString(); + + console.log(pathName); + console.log(markDown); + markDown = markDown.split("(media/").join("(" + "../blogContent/posts/" + categoryURL + "/media/"); @@ -160,17 +156,6 @@ module.exports= }) }, - - /** Method to return the footer of the html blog post. - * - * @returns {string} - */ - generateBlogPostFooter: function() - { - return "


"; - }, - - /** * Converts markdown into html. * @@ -207,4 +192,58 @@ module.exports= } }); }, + + + /** + * Renders a bunch of blog post previews to the user + * + * @param baseURL-- url of the page + * @param posts -- sql data about the blog to render + * @param currentPage -- the current page to render + * @param numOfPosts -- number of blog to render + * @returns {Promise} renders the html of the blog + */ + renderBatchOfPosts: function(baseURL, posts, currentPage, numOfPosts, templateContext) + { + if(typeof currentPage == "undefined") + { + currentPage = 1; + } + else + { + currentPage = Number(currentPage); + } + + return new Promise(function(resolve, reject) + { + const promises = []; + for(var i = (currentPage-1) * numOfPosts; i < (currentPage-1) * numOfPosts + numOfPosts; i++) + { + if(i < posts.length) + { + promises.push(new Promise(function(res, rej) + { + module.exports.generateBlogPost(posts[i], 3).then(function(tempContext) + { + res(tempContext); + }).catch(function(error) + { + rej(); + }) + })); + } + } + + //promises.push(require('../blog/renderNextBar').main(baseURL, currentPage, numOfPosts, blog.length)); + + Promise.all(promises).then(function(posts) + { + templateContext.posts = posts; + resolve(); + }).catch(function(error) + { + reject(error); + }); + }); + } } diff --git a/posts/renderNextBar.js b/blog/renderNextBar.js similarity index 89% rename from posts/renderNextBar.js rename to blog/renderNextBar.js index ff232b2..d2d3f7e 100644 --- a/posts/renderNextBar.js +++ b/blog/renderNextBar.js @@ -2,8 +2,8 @@ * Determines if the requested page is out of bounds * * @param page current page - * @param postsPerPage - number of posts rendered on each page - * @param totalPosts - total posts in this category/total + * @param postsPerPage - number of blog rendered on each page + * @param totalPosts - total blog in this category/total * @returns {boolean} if this is a valid page */ const isValidPage = function(page, postsPerPage, totalPosts) @@ -20,8 +20,8 @@ module.exports= * Used by the home page and categories pages * @param baseURL -- base url of page being rendered * @param currentPage -- current page being rendered - * @param postsPerPage -- number of posts on each page - * @param totalPosts -- total amount of posts in the category + * @param postsPerPage -- number of blog on each page + * @param totalPosts -- total amount of blog in the category * @returns {Promise} promise which renders the buttons */ main: function(baseURL, currentPage, postsPerPage, totalPosts) diff --git a/blogContent/posts/web-development/node-website-optimization.md b/blogContent/posts/web-development/node-website-optimization.md index cac48d5..78a3b02 100644 --- a/blogContent/posts/web-development/node-website-optimization.md +++ b/blogContent/posts/web-development/node-website-optimization.md @@ -131,7 +131,7 @@ Another Good Async Example: ```javascript /** - * Calls posts and sidebar modules to render blog contents in order + * Calls blog and sidebar modules to render blog contents in order * * @param requestURL * @returns {Promise|*} diff --git a/includes/contact.js b/includes/contact.js index 05099fe..d4cfe75 100644 --- a/includes/contact.js +++ b/includes/contact.js @@ -190,7 +190,7 @@ module.exports = * Displays the contact page along with the header, sidebar, and footer. * This uses the admin header because it doesn't need any minified css * which has been purged of some css classes which are not used in any - * of the blog posts. + * of the blog blog. * * @param request -- main express request * @param result -- renders the html of the contact page diff --git a/posts/homePage.js b/posts/homePage.js deleted file mode 100644 index 3497ae6..0000000 --- a/posts/homePage.js +++ /dev/null @@ -1,46 +0,0 @@ -const sql = require('../utils/sql'); - -const batchPreview = require('../posts/renderBatchOfPreviewes'); - -/**Renders each recent post for the homepage of the website - * - * @param result - * @returns {*|Promise} - */ -var renderRecentPosts = function(baseURL, page) -{ - return new Promise(function(resolve, reject) - { - sql.getRecentPostSQL().then(function(posts) - { - resolve(batchPreview.main(baseURL, posts, page, 5)); - }).catch(function(error) - { - reject(error); - }) - }); -}; - -module.exports= - { - /** - * Renders the previews of recent blog posts and the side bar - * - * @param res - * @param fileName request url - */ - main: function(requestURL, request) - { - var page = request.query.page; - return new Promise(function(resolve, reject) - { - Promise.all([renderRecentPosts(requestURL, page), require("../sidebar/sidebar.js").main()]).then(function(content) - { - resolve(content.join('')); - }).catch(function(error) - { - reject(error); - }); - }) - } - }; \ No newline at end of file diff --git a/posts/renderBatchOfPreviewes.js b/posts/renderBatchOfPreviewes.js deleted file mode 100644 index 6c69419..0000000 --- a/posts/renderBatchOfPreviewes.js +++ /dev/null @@ -1,55 +0,0 @@ -module.exports= - { - /** - * Renders a bunch of blog post previews to the user - * - * @param baseURL-- url of the page - * @param posts -- sql data about the posts to render - * @param currentPage -- the current page to render - * @param numOfPosts -- number of posts to render - * @returns {Promise} renders the html of the posts - */ - main: function(baseURL, posts, currentPage, numOfPosts) - { - if(typeof currentPage == "undefined") - { - currentPage = 1; - } - else - { - currentPage = Number(currentPage); - } - - return new Promise(function(resolve, reject) - { - const promises = []; - for(var i = (currentPage-1) * numOfPosts; i < (currentPage-1) * numOfPosts + numOfPosts; i++) - { - if(i < posts.length) - { - promises.push(new Promise(function(res, rej) - { - require("../posts/singlePost.js") - .renderPreview(posts[i]).then(function(html) - { - res(html); - }).catch(function(error) - { - reject(error) - }) - })); - } - } - - promises.push(require('../posts/renderNextBar').main(baseURL, currentPage, numOfPosts, posts.length)); - - Promise.all(promises).then(function(content) - { - resolve("
" + content.join('') + "
"); - }).catch(function(error) - { - reject(error); - }); - }); - } - }; \ No newline at end of file diff --git a/posts/singlePost.js b/posts/singlePost.js deleted file mode 100644 index 97ef5c7..0000000 --- a/posts/singlePost.js +++ /dev/null @@ -1,28 +0,0 @@ -const postGenerator = require('../utils/renderBlogPost.js'); - - -module.exports= -{ - /** - * Renders a preview of the post with a link to view more - * - * @param res - * @param post - */ - renderPreview: function(post) - { - return postGenerator.generateBlogPost(post, 3); - }, - - /** - * 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 postGenerator.generateBlogPost(post, -1); - } -}; \ No newline at end of file diff --git a/sites/admin.js b/sites/admin.js index e6551d4..2abc64a 100644 --- a/sites/admin.js +++ b/sites/admin.js @@ -18,7 +18,7 @@ const whiskers = require('whiskers'); module.exports= { /** - * Calls posts and sidebar modules to render blog contents in order + * Calls blog and sidebar modules to render blog contents in order * * @param requestURL * @returns {Promise|*} diff --git a/sites/blog.js b/sites/blog.js index 319139a..a54eec7 100644 --- a/sites/blog.js +++ b/sites/blog.js @@ -59,18 +59,18 @@ module.exports= if (filename === '' || filename === '/') { - file = "../posts/homePage.js"; + file = "../blog/homePage.js"; } else { var urlSplit = filename.split("/"); if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page - file = "../posts/category.js"; + file = "../blog/category.js"; else { - file = "../posts/posts.js"; - page = 1; // all posts are single page, everyone must be one to ensure + file = "../blog/blog.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 } } @@ -78,9 +78,11 @@ module.exports= Promise.all([includes.fetchTemplate(TEMPLATE_FILE), includes.printHeader(templateContext), includes.printFooter(templateContext), + require(file).main(filename, request, templateContext), require("../sidebar/sidebar.js").main(templateContext)]) .then(function (content) { + console.log(templateContext); result.write(whiskers.render(content[0], templateContext)); result.end(); cache.put(filename + "?page=" + page, content.join('')); diff --git a/sites/projects.js b/sites/projects.js index d4c7b0f..366ba47 100644 --- a/sites/projects.js +++ b/sites/projects.js @@ -14,7 +14,7 @@ const contentLoader = require('../includes/staticContentServer.js'); module.exports= { /** - * Calls posts and sidebar modules to render blog contents in order + * Calls blog and sidebar modules to render blog contents in order * * @param requestURL * @returns {Promise|*} diff --git a/utils/sql.js b/utils/sql.js index a9e4ccb..6fad667 100644 --- a/utils/sql.js +++ b/utils/sql.js @@ -163,7 +163,7 @@ module.exports= /** - * Function which currently returns all posts of a particular + * Function which currently returns all blog of a particular * category from the database * @param requestURL * @return {*|Promise}