Browse Source

Generated previews of the blog posts for display on home page and categories page

pull/4/head
jrtechs 6 years ago
parent
commit
b8be296ff4
6 changed files with 182 additions and 21 deletions
  1. +5
    -6
      posts/category.js
  2. +73
    -0
      posts/homePage.js
  3. +1
    -1
      posts/posts.js
  4. +81
    -4
      posts/singlePost.js
  5. +13
    -10
      server.js
  6. +9
    -0
      utils/sql.js

+ 5
- 6
posts/category.js View File

@ -21,7 +21,7 @@ var renderPosts = function(result, resultURL)
promises.push(new Promise(function(res, rej)
{
require("../posts/singlePost.js")
.renderPost(result, p).then(function()
.renderPreview(result, p).then(function()
{
res();
});
@ -46,11 +46,6 @@ var renderPosts = function(result, resultURL)
module.exports=
{
renderPostPreview: function(result, postSQLData)
{
},
/**
* Calls posts and sidebar modules to render blog contents in order
*
@ -67,6 +62,10 @@ module.exports=
}).then(function ()
{
resolve();
}).catch(function(error)
{
console.log(error);
reject(error);
})
});
}

+ 73
- 0
posts/homePage.js View File

@ -0,0 +1,73 @@
const sql = require('../utils/sql');
const utils = require('../utils/utils.js');
const Promise = require('promise');
const postRenderer = require('../posts/singlePost.js');
var renderRecentPosts = function(result)
{
return new Promise(function(resolve, reject)
{
sql.getRecentPostSQL().then(function(posts)
{
var postPromises = [];
result.write("<div class='w3-col l8 s12'>");
posts.forEach(function(post)
{
postPromises.push(new Promise(function(res, rej)
{
postRenderer.renderPreview(result, post).then(function()
{
res();
}).catch(function(error)
{
rej(error);
})
}));
});
Promise.all(postPromises).then(function()
{
result.write("</div>");
resolve();
}).catch(function(error)
{
reject(error);
})
}).catch(function(error)
{
console.log(error);
reject(error);
})
});
};
module.exports=
{
/**
* Renders the previews of recent blog posts and the side bar
*
* @param res
* @param fileName request url
*/
main: function(result, requestURL, request)
{
return new Promise(function(resolve, reject)
{
renderRecentPosts(result).then(function()
{
return require("../sidebar/sidebar.js").main(result);
}).then(function()
{
resolve();
}).catch(function(error)
{
console.log(error);
reject(error);
})
})
}
};

+ 1
- 1
posts/posts.js View File

@ -62,7 +62,7 @@ module.exports=
{
renderPost(res, requestURL).then(function()
{
return require("../sidebar/sidebar.js").main(res)
return require("../sidebar/sidebar.js").main(res);
}).then(function ()
{
resolve();

+ 81
- 4
posts/singlePost.js View File

@ -30,6 +30,79 @@ var md = new Remarkable({
module.exports=
{
/**
* Renders a preview of the post with a link to view more
*
* @param res
* @param post
*/
renderPreview: function(res, post)
{
return new Promise(function(resolve, reject)
{
var html = "<div class=\"w3-card-4 w3-margin w3-white\">";
//image
if(!(post.picture_url === "n/a"))
{
html +="<img src=\"/img/posts/" + post.picture_url +
"\" alt=\"\" style=\"width:100%\">";
}
html += "<div class=\"w3-container\">";
//title
html += "<h3><b>" + post.name + "</b></h3>";
//date
html += "<h5><span class=\"w3-opacity\">" +
post.published.toDateString() + "</span></h5>";
html +="</div>";
html += "<div class=\"w3-container\">";
try
{
sql.getCategory(post.category_id).then(function(category)
{
var pathName = "entries/" + category[0].url + "/"
+ post.url + ".md";
var markDown = utils.getFileContents(pathName).toString();
markDown = markDown.split("![](media/").join("![](" + "../entries/"
+ category[0].url + "/media/");
var htmlPost = md.render(markDown).split("<p>");
for(var i = 0; i < 3; i++)
{
html+= "<p>" + htmlPost[i];
}
html = html.split("<img").join("<img style=\"max-width: 100%;\" ");
html += " <div class=\"w3-row\">\n" +
" <p class='w3-center'><button class=\"w3-button " +
"w3-padding-large w3-white w3-border\" onclick=\"location.href='" +
"http://jrtechs.net/" + category[0].url + "/" + post.url +
"'\"><b>READ MORE &raquo;</b></button></p>\n" +
" </div>\n";
html += "</div></div>";
res.write(html);
resolve()
}).catch(function(error)
{
console.log(error);
reject(error);
});
}
catch(ex)
{
reject(ex);
console.log(ex);
}
});
},
/**
* renderPost() displays a single blog post in it's entirety
*
@ -45,14 +118,16 @@ module.exports=
//image
if(!(post.picture_url === "n/a"))
{
html +="<img src=\"/img/posts/" + post.picture_url + "\" alt=\"\" style=\"width:100%\">";
html +="<img src=\"/img/posts/" + post.picture_url +
"\" alt=\"\" style=\"width:100%\">";
}
html += "<div class=\"w3-container\">";
//title
html += "<h3><b>" + post.name + "</b></h3>";
//date
html += "<h5><span class=\"w3-opacity\">" + post.published.toDateString() + "</span></h5>";
html += "<h5><span class=\"w3-opacity\">" +
post.published.toDateString() + "</span></h5>";
html +="</div>";
html += "<div class=\"w3-container\">";
@ -60,9 +135,11 @@ module.exports=
{
sql.getCategory(post.category_id).then(function(category)
{
var pathName = "entries/" + category[0].url + "/" + post.url + ".md";
var pathName = "entries/" + category[0].url + "/"
+ post.url + ".md";
var markDown = utils.getFileContents(pathName).toString();
markDown = markDown.split("![](media/").join("![](" + "../entries/" + category[0].url + "/media/");
markDown = markDown.split("![](media/").join("![](" + "../entries/"
+ category[0].url + "/media/");
html += md.render(markDown);
html = html.split("<img").join("<img style=\"max-width: 100%;\" ");

+ 13
- 10
server.js View File

@ -49,19 +49,22 @@ app.use(function(request, res)
var file = "";
if(filename === '' || filename === '/')
filename = '/programming/using-english-conventions-to-write-clean-code/';
var urlSplit = filename.split("/");
if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "./posts/category.js";
{
file="./posts/homePage.js";
}
else
{
var urlSplit = filename.split("/");
else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
file = "./admin/admin.js";
if(urlSplit.length >= 2 && urlSplit[1] === 'category') //single category page
file = "./posts/category.js";
else
file = "./posts/posts.js";
else if(urlSplit.length >= 2 && urlSplit[1] === 'admin') //top secret admin page
file = "./admin/admin.js";
else
file = "./posts/posts.js";
}
includes.printHeader(res).then(function()
{

+ 9
- 0
utils/sql.js View File

@ -173,6 +173,12 @@ module.exports=
});
},
getRecentPostSQL: function()
{
return fetch("select * from posts order by post_id desc limit 10");
},
/**
* Helper method which returns a list of objects which contains the url
* and name of thee ten most recent posts
@ -213,6 +219,9 @@ module.exports=
});
});
},
getPopularPosts: function()
{
return new Promise(function(resolve, reject)

Loading…
Cancel
Save