Web/node/blog.js

67 lines
2.9 KiB
JavaScript
Raw Normal View History

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
2020-04-02 21:54:20 +02:00
let folder = __dirname + "/../";
console.log("Refreshing blog sub-sites...");
fs.readFile(folder + "index.html", function (_, data) {
// this can probably be done a lot easier but regex is hard ok
var template = data.toString().replace(/<!-- ?NOBLOG ?-->([^](?!<!-- ?NOBLOG ?-->))*<!-- ?\/NOBLOG ?-->/g, "");
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>';
2020-05-06 19:54:21 +02:00
let last = getAdjacentPost(json, i, -1);
if (last)
nav += '<a class="nav-item nav-link" href="/blog-' + last.id + '">Previous Post</a>';
let next = getAdjacentPost(json, i, 1);
if (next)
nav += '<a class="nav-item nav-link" href="/blog-' + next.id + '">Next Post</a>';
2020-04-02 21:54:20 +02:00
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 + '">'
2020-05-06 19:54:21 +02:00
if (post.archived)
c += "<p><i>This post has been archived.</i></p>"
2020-04-02 21:54:20 +02:00
c += converter.makeHtml(content.toString());
c += '</div>';
c += '<span class="text-muted project-status blog-isolated-status">' + post.date + "</span>";
2020-05-06 19:54:21 +02:00
if (post.discuss)
c += '<a href="' + post.discuss + '" class="blog-discuss" id="blog-discuss-' + post.id + '">Discuss this post</a>'
2020-04-02 21:54:20 +02:00
c += '</div></div>';
document.getElementById("main").innerHTML = c;
let html = dom.serialize();
fs.writeFile(folder + "blog-" + post.id + ".html", html, function (_, _) {});
});
}
2019-10-09 15:51:43 +02:00
});
2020-05-06 19:54:21 +02:00
});
function getAdjacentPost(json, index, move) {
while (true) {
index += move;
let post = json[index];
if (!post)
break;
if (!post.archived)
return post;
}
}