69 lines
No EOL
3 KiB
JavaScript
69 lines
No EOL
3 KiB
JavaScript
const {
|
|
JSDOM
|
|
} = require("jsdom");
|
|
const fs = require("fs");
|
|
const converter = require("./showdown")(2);
|
|
|
|
module.exports = function () {
|
|
let folder = __dirname + "/../";
|
|
createBlogPages(folder);
|
|
|
|
fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
|
|
if (curr.mtime == prev.mtime)
|
|
return;
|
|
createBlogPages(folder);
|
|
});
|
|
}
|
|
|
|
function createBlogPages(folder) {
|
|
console.log("Refreshing blog sub-sites...");
|
|
|
|
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();
|
|
|
|
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];
|
|
fs.readFile(folder + "blog/" + post.id + ".md", function (_, content) {
|
|
let dom = new JSDOM(template);
|
|
var document = dom.window.document;
|
|
|
|
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);
|
|
|
|
var nav = "";
|
|
nav += '<a class="nav-item nav-link" href="/#blog">Back to Main Page</a>';
|
|
if (i > 0)
|
|
nav += '<a class="nav-item nav-link" href="/blog-' + json[i - 1].id + '">Previous Post</a>';
|
|
if (i < json.length - 1)
|
|
nav += '<a class="nav-item nav-link" href="/blog-' + json[i + 1].id + '">Next Post</a>';
|
|
document.getElementById("nav-items").innerHTML = nav;
|
|
|
|
var c = "";
|
|
c += '<div class="list-display rounded">';
|
|
c += '<div class="blog-isolated">'
|
|
c += '<h1>' + post.name + '</h1>';
|
|
c += '<div id="blog-post-' + post.id + '">'
|
|
c += converter.makeHtml(content.toString());
|
|
c += '</div>';
|
|
c += '<span class="text-muted project-status blog-isolated-status">' + post.date + "</span>";
|
|
var discussLink = post.discuss;
|
|
if (discussLink)
|
|
c += '<a href="' + discussLink + '" class="blog-discuss" id="blog-discuss-' + post.id + '">Discuss this post</a>'
|
|
c += '</div></div>';
|
|
document.getElementById("main").innerHTML = c;
|
|
|
|
let html = dom.serialize();
|
|
fs.writeFile(folder + "blog-" + post.id + ".html", html, function (_, _) {});
|
|
});
|
|
}
|
|
});
|
|
});
|
|
} |