2019-10-09 15:51:43 +02:00
|
|
|
const {
|
|
|
|
JSDOM
|
|
|
|
} = require("jsdom");
|
|
|
|
const fs = require("fs");
|
2019-10-20 15:56:22 +02:00
|
|
|
const converter = require("./showdown")(2);
|
2019-10-09 15:51:43 +02:00
|
|
|
|
|
|
|
module.exports = function () {
|
|
|
|
let folder = __dirname + "/../";
|
2019-10-10 12:04:08 +02:00
|
|
|
createBlogPages(folder);
|
|
|
|
|
2019-10-09 15:51:43 +02:00
|
|
|
fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
|
|
|
|
if (curr.mtime == prev.mtime)
|
|
|
|
return;
|
2019-10-10 12:04:08 +02:00
|
|
|
createBlogPages(folder);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function createBlogPages(folder) {
|
|
|
|
console.log("Refreshing blog sub-sites...");
|
2019-10-09 15:51:43 +02:00
|
|
|
|
2019-10-10 12:04:08 +02:00
|
|
|
fs.readFile(folder + "index.html", function (_, data) {
|
|
|
|
// set up the template
|
|
|
|
let templateDom = new JSDOM(data);
|
|
|
|
var templateDoc = templateDom.window.document;
|
|
|
|
templateDoc.getElementById("main").innerHTML = "";
|
|
|
|
let template = templateDom.serialize();
|
2019-10-09 15:51:43 +02:00
|
|
|
|
2019-10-10 12:04:08 +02:00
|
|
|
fs.readFile(folder + "blog/posts.json", function (_, data) {
|
|
|
|
let json = JSON.parse(data);
|
|
|
|
for (let i = 0; i < json.length; i++) {
|
|
|
|
let post = json[i];
|
2019-11-20 22:17:58 +01:00
|
|
|
fs.readFile(folder + "blog/" + post.id + ".md", function (_, content) {
|
2019-10-10 12:04:08 +02:00
|
|
|
let dom = new JSDOM(template);
|
|
|
|
var document = dom.window.document;
|
2019-10-09 15:51:43 +02:00
|
|
|
|
2019-11-20 22:17:58 +01:00
|
|
|
document.title += " - " + post.name;
|
|
|
|
document.querySelector('meta[property="og:title"]').setAttribute("content", post.name);
|
|
|
|
document.querySelector('meta[name="description"]').setAttribute("content", post.summary);
|
|
|
|
document.querySelector('meta[property="og:description"]').setAttribute("content", post.summary);
|
2019-10-09 23:22:47 +02:00
|
|
|
|
2019-10-10 12:04:08 +02:00
|
|
|
var nav = "";
|
|
|
|
nav += '<a class="nav-item nav-link" href="/#blog">Back to Main Page</a>';
|
2019-10-20 16:07:28 +02:00
|
|
|
if (i > 0)
|
2019-11-20 22:17:58 +01:00
|
|
|
nav += '<a class="nav-item nav-link" href="/blog-' + json[i - 1].id + '">Previous Post</a>';
|
2019-10-20 16:07:28 +02:00
|
|
|
if (i < json.length - 1)
|
2019-11-20 22:17:58 +01:00
|
|
|
nav += '<a class="nav-item nav-link" href="/blog-' + json[i + 1].id + '">Next Post</a>';
|
2019-10-10 12:04:08 +02:00
|
|
|
document.getElementById("nav-items").innerHTML = nav;
|
2019-10-09 15:51:43 +02:00
|
|
|
|
2019-10-10 12:04:08 +02:00
|
|
|
var c = "";
|
|
|
|
c += '<div class="list-display rounded">';
|
|
|
|
c += '<div class="blog-isolated">'
|
2019-11-20 22:17:58 +01:00
|
|
|
c += '<h1>' + post.name + '</h1>';
|
|
|
|
c += '<div id="blog-post-' + post.id + '">'
|
2019-10-10 12:04:08 +02:00
|
|
|
c += converter.makeHtml(content.toString());
|
|
|
|
c += '</div>';
|
2019-11-20 22:17:58 +01:00
|
|
|
c += '<span class="text-muted project-status blog-isolated-status">' + post.date + "</span>";
|
|
|
|
var discussLink = post.discuss;
|
2019-10-10 12:04:08 +02:00
|
|
|
if (discussLink)
|
2019-11-20 22:17:58 +01:00
|
|
|
c += '<a href="' + discussLink + '" class="blog-discuss" id="blog-discuss-' + post.id + '">Discuss this post</a>'
|
2019-10-10 12:04:08 +02:00
|
|
|
c += '</div></div>';
|
|
|
|
document.getElementById("main").innerHTML = c;
|
2019-10-09 15:51:43 +02:00
|
|
|
|
2019-10-10 12:04:08 +02:00
|
|
|
let html = dom.serialize();
|
2019-11-20 22:17:58 +01:00
|
|
|
fs.writeFile(folder + "blog-" + post.id + ".html", html, function (_, _) {});
|
2019-10-10 12:04:08 +02:00
|
|
|
});
|
|
|
|
}
|
2019-10-09 15:51:43 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|