Browse Source

Updated single page view to work with template engine.

pull/41/head
jrtechs 6 years ago
parent
commit
c41a72c4d7
7 changed files with 110 additions and 66 deletions
  1. +0
    -1
      blog/category.js
  2. +27
    -49
      blog/posts.js
  3. +1
    -4
      blog/renderBlogPost.js
  4. +4
    -4
      sites/blog.js
  5. +41
    -0
      templates/blog/blogMain.html
  6. +36
    -0
      templates/blog/sideBar.html
  7. +1
    -8
      utils/sql.js

+ 0
- 1
blog/category.js View File

@ -28,7 +28,6 @@ module.exports=
{ {
blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext).then(function() blogBodyRenderer.renderBatchOfPosts(requestURL, posts, page, 5, templateContext).then(function()
{ {
console.log("good");
resolve(); resolve();
}); });
}).catch(function() }).catch(function()

+ 27
- 49
blog/posts.js View File

@ -1,48 +1,8 @@
/** DB queries */ /** DB queries */
const sql = require('../utils/sql'); 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("<div class='col-md-8'>" + html + "</div>");
}).catch(function(error)
{
reject(error);
})
}
else
{
reject("Page Not Found");
}
});
};
/** Object used to render blog post previews */
const blogBodyRenderer = require('./renderBlogPost');
module.exports= module.exports=
{ {
@ -52,18 +12,36 @@ module.exports=
* @param requestURL * @param requestURL
* @returns {Promise|*} * @returns {Promise|*}
*/ */
main: function(requestURL, request)
main: function(requestURL, request, templateContext)
{ {
return new Promise(function(resolve, reject) 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();
}
}); });
} }
}; };

+ 1
- 4
blog/renderBlogPost.js View File

@ -94,9 +94,6 @@ module.exports=
+ 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/");
@ -223,7 +220,7 @@ module.exports=
{ {
promises.push(new Promise(function(res, rej) 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); res(tempContext);
}).catch(function(error) }).catch(function(error)

+ 4
- 4
sites/blog.js View File

@ -69,7 +69,7 @@ module.exports=
file = "../blog/category.js"; file = "../blog/category.js";
else else
{ {
file = "../blog/blog.js";
file = "../blog/posts.js";
page = 1; // all blog are single page, everyone must be one to ensure 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
} }
@ -82,10 +82,10 @@ module.exports=
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));
var html = whiskers.render(content[0], templateContext);
result.write(html);
result.end(); result.end();
cache.put(filename + "?page=" + page, content.join(''));
cache.put(filename + "?page=" + page, html);
}).catch(function (err) }).catch(function (err)
{ {
console.log(err); console.log(err);

+ 41
- 0
templates/blog/blogMain.html View File

@ -0,0 +1,41 @@
{header}
<div class="container">
<div class="row">
<div class="col-md-8 col-12">
{for post in posts}
<div class="blogPost">
{if post.hasPicture}
<img src="/blogContent/headerImages/{post.picture_url}" style="width:100%;">
{/if}
<div class="p-4">
<h3><b>{post.name}</b></h3>
<h5>
<span class="w3-opacity">{post.published}</span>
</h5>
{post.blogBody}
</div>
</div>
<br><br>
{else}
<div class="row p-lg-0">
<h1 class="align-content-center">Page Not Found</h1>
<div class="align-content-center">
<img src="/includes/img/404.jpg" alt="Page not found" width="70%" />
</div>
</div>
<br><br>
{/for}
</div>
<div class="col-md-4 col-4">
{>sideBar}
</div>
</div>
</div>
{footer}

+ 36
- 0
templates/blog/sideBar.html View File

@ -0,0 +1,36 @@
<div class="container">
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<h5 class="mb-1">Project Sites</h5>
</a>
<a class="list-group-item" href='https://jrtechs.net/steam/'>Steam Graph Analysis<br></a>
<a class="list-group-item" href='https://jrtechs.me/'>Portfolio<br></a>
<a class="list-group-item" href='https://clubpanda.jrtechs.net/'>Club Panda<br></a>
</div>
</div>
<br>
<div class="container">
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<h5 class="mb-1">Recent Posts</h5>
</a>
{for recentPost in recentPosts}
<a class="list-group-item" href='{recentPost.url}'>{recentPost.name}<br></a>
{/for}
</div>
</div>
<br>
<div class="container">
<div class="list-group">
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start active">
<h5 class="mb-1">Categories</h5>
</a>
{for cat in categories}
<a class="list-group-item" href='{cat.url}'>{cat.name}<br></a>
{/for}
</div>
</div>
<br>

+ 1
- 8
utils/sql.js View File

@ -130,14 +130,7 @@ module.exports=
fetch(q2).then(function (result_posts) fetch(q2).then(function (result_posts)
{ {
if(result_posts != 0)
{
resolve(result_posts[0]);
}
else
{
resolve(0);
}
resolve(result_posts);
}); });
} }
else else

Loading…
Cancel
Save