Browse Source

Updated the home page to use the templating engine.

pull/41/head
jrtechs 6 years ago
parent
commit
d094cac4b2
17 changed files with 128 additions and 188 deletions
  1. +3
    -3
      admin/admin.js
  2. +1
    -1
      admin/adminHome.js
  3. +6
    -6
      admin/posts.js
  4. +3
    -3
      blog/category.js
  5. +27
    -0
      blog/homePage.js
  6. +3
    -2
      blog/posts.js
  7. +70
    -31
      blog/renderBlogPost.js
  8. +4
    -4
      blog/renderNextBar.js
  9. +1
    -1
      blogContent/posts/web-development/node-website-optimization.md
  10. +1
    -1
      includes/contact.js
  11. +0
    -46
      posts/homePage.js
  12. +0
    -55
      posts/renderBatchOfPreviewes.js
  13. +0
    -28
      posts/singlePost.js
  14. +1
    -1
      sites/admin.js
  15. +6
    -4
      sites/blog.js
  16. +1
    -1
      sites/projects.js
  17. +1
    -1
      utils/sql.js

+ 3
- 3
admin/admin.js View File

@ -2,7 +2,7 @@
* Determines what template and controls that will be * Determines what template and controls that will be
* displayed based on the url such as * displayed based on the url such as
* / * /
* /posts
* /blog
* /downloads * /downloads
* *
* For each controls it calls that "pages" associated javascript file * For each controls it calls that "pages" associated javascript file
@ -44,9 +44,9 @@ module.exports=
page = "./adminDownloads.js"; page = "./adminDownloads.js";
console.log("downloads time") 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) require(page).main(postData, templateContext).then(function(template)

+ 1
- 1
admin/adminHome.js View File

@ -75,7 +75,7 @@ const processPost = function(postData)
urls =urls.toLowerCase(); 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 + q += "('" + post.add_post_category + "', '" + post.add_post_picture +
"', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')"; "', '" + post.add_post_date + "', '" + post.add_post_name + "', '" + urls + "')";

+ 6
- 6
admin/posts.js View File

@ -1,5 +1,5 @@
/** Whiskers template file /** 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 TEMPLATE_FILE = "admin/adminPosts.html";
const includes = require('../includes/includes.js'); 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. * in the edit post form.
* *
* @param postData * @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. * the template context for the template renderer.
* *
* @param templateContext * @param templateContext
@ -77,8 +77,8 @@ const fetchPostsInformation = function(templateContext)
module.exports= 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 postData posted by user
* @param templateContext json object used as the template context * @param templateContext json object used as the template context
@ -95,7 +95,7 @@ module.exports=
resolve(template[0]); resolve(template[0]);
}).catch(function(error) }).catch(function(error)
{ {
console.log("error in add admin posts.js");
console.log("error in add admin blog.js");
reject(error); reject(error);
}); });
}); });

posts/category.js → blog/category.js View File

@ -2,10 +2,10 @@
const sql = require('../utils/sql'); const sql = require('../utils/sql');
/** Object used to render blog post previews */ /** 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 * @param resultURL
* @returns {*} * @returns {*}
@ -35,7 +35,7 @@ const renderPosts = function(resultURL, page)
module.exports= 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 requestURL
* @param request * @param request

+ 27
- 0
blog/homePage.js View File

@ -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);
})
});
}
};

posts/posts.js → blog/posts.js View File

@ -22,7 +22,7 @@ const renderPost = function(requestURL)
{ {
if(post != 0) if(post != 0)
{ {
return require("../posts/singlePost.js").renderPost(post);
return require(".//singlePost.js").renderPost(post);
} }
else else
{ {
@ -43,10 +43,11 @@ const renderPost = function(requestURL)
}); });
}; };
module.exports= 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 requestURL
* @returns {Promise|*} * @returns {Promise|*}

utils/renderBlogPost.js → blog/renderBlogPost.js View File

@ -25,10 +25,10 @@ module.exports=
return new Promise(function(resolve, reject) return new Promise(function(resolve, reject)
{ {
Promise.all([module.exports.generateBlogPostHeader(post), 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) }).catch(function(error)
{ {
reject(error); reject(error);
@ -45,23 +45,11 @@ module.exports=
*/ */
generateBlogPostHeader: function(post) generateBlogPostHeader: function(post)
{ {
var htmlHead = "<div class=\"blogPost\">";
//image
if(!(post.picture_url === "n/a"))
{
htmlHead +="<img src=\"/blogContent/headerImages/" + post.picture_url +
"\" alt=\"\" style=\"width:100%; height:10%\">";
}
htmlHead += "<div class=\"p-4\"><div class=\"\">";
//title
htmlHead += "<h3><b>" + post.name + "</b></h3>";
//date
htmlHead += "<h5><span class=\"w3-opacity\">" +
post.published.toDateString() + "</span></h5>";
htmlHead +="</div>" + "<div class=\"\">";
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) 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 + "/" const pathName = "blogContent/posts/" + categoryURL + "/"
+ postURL + ".md"; + postURL + ".md";
var markDown = utils.getFileContents(pathName).toString(); var markDown = utils.getFileContents(pathName).toString();
console.log(pathName);
console.log(markDown);
markDown = markDown.split("(media/").join("(" + "../blogContent/posts/" markDown = markDown.split("(media/").join("(" + "../blogContent/posts/"
+ categoryURL + "/media/"); + categoryURL + "/media/");
@ -160,17 +156,6 @@ module.exports=
}) })
}, },
/** Method to return the footer of the html blog post.
*
* @returns {string}
*/
generateBlogPostFooter: function()
{
return "</div></div></div><br><br>";
},
/** /**
* Converts markdown into html. * 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);
});
});
}
} }

posts/renderNextBar.js → blog/renderNextBar.js View File

@ -2,8 +2,8 @@
* Determines if the requested page is out of bounds * Determines if the requested page is out of bounds
* *
* @param page current page * @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 * @returns {boolean} if this is a valid page
*/ */
const isValidPage = function(page, postsPerPage, totalPosts) const isValidPage = function(page, postsPerPage, totalPosts)
@ -20,8 +20,8 @@ module.exports=
* Used by the home page and categories pages * Used by the home page and categories pages
* @param baseURL -- base url of page being rendered * @param baseURL -- base url of page being rendered
* @param currentPage -- current 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 * @returns {Promise} promise which renders the buttons
*/ */
main: function(baseURL, currentPage, postsPerPage, totalPosts) main: function(baseURL, currentPage, postsPerPage, totalPosts)

+ 1
- 1
blogContent/posts/web-development/node-website-optimization.md View File

@ -131,7 +131,7 @@ Another Good Async Example:
```javascript ```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 * @param requestURL
* @returns {Promise|*} * @returns {Promise|*}

+ 1
- 1
includes/contact.js View File

@ -190,7 +190,7 @@ module.exports =
* Displays the contact page along with the header, sidebar, and footer. * Displays the contact page along with the header, sidebar, and footer.
* This uses the admin header because it doesn't need any minified css * 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 * 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 request -- main express request
* @param result -- renders the html of the contact page * @param result -- renders the html of the contact page

+ 0
- 46
posts/homePage.js View File

@ -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);
});
})
}
};

+ 0
- 55
posts/renderBatchOfPreviewes.js View File

@ -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("<div class='col-md-8'>" + content.join('') + "</div>");
}).catch(function(error)
{
reject(error);
});
});
}
};

+ 0
- 28
posts/singlePost.js View File

@ -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);
}
};

+ 1
- 1
sites/admin.js View File

@ -18,7 +18,7 @@ const whiskers = require('whiskers');
module.exports= 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 requestURL
* @returns {Promise|*} * @returns {Promise|*}

+ 6
- 4
sites/blog.js View File

@ -59,18 +59,18 @@ module.exports=
if (filename === '' || filename === '/') if (filename === '' || filename === '/')
{ {
file = "../posts/homePage.js";
file = "../blog/homePage.js";
} }
else else
{ {
var urlSplit = filename.split("/"); var urlSplit = filename.split("/");
if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page if (urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "../posts/category.js";
file = "../blog/category.js";
else 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 // 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), Promise.all([includes.fetchTemplate(TEMPLATE_FILE),
includes.printHeader(templateContext), includes.printHeader(templateContext),
includes.printFooter(templateContext), includes.printFooter(templateContext),
require(file).main(filename, request, templateContext),
require("../sidebar/sidebar.js").main(templateContext)]) require("../sidebar/sidebar.js").main(templateContext)])
.then(function (content) .then(function (content)
{ {
console.log(templateContext);
result.write(whiskers.render(content[0], templateContext)); result.write(whiskers.render(content[0], templateContext));
result.end(); result.end();
cache.put(filename + "?page=" + page, content.join('')); cache.put(filename + "?page=" + page, content.join(''));

+ 1
- 1
sites/projects.js View File

@ -14,7 +14,7 @@ const contentLoader = require('../includes/staticContentServer.js');
module.exports= 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 requestURL
* @returns {Promise|*} * @returns {Promise|*}

+ 1
- 1
utils/sql.js View File

@ -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 * category from the database
* @param requestURL * @param requestURL
* @return {*|Promise} * @return {*|Promise}

Loading…
Cancel
Save