Web/node/blog.js

84 lines
3.7 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 (_, html) {
let templateDom = new JSDOM(html);
2021-03-11 03:21:23 +01:00
// remove all non-blog stuff from the template (which is just our index.html)
let noBlog = templateDom.window.document.getElementsByClassName("no-blog");
while (noBlog.length > 0)
noBlog[0].parentNode.removeChild(noBlog[0]);
2021-03-11 03:21:23 +01:00
// change all relative links to move to the parent directory (since we're in blog/)
let elements = templateDom.window.document.getElementsByTagName("*");
for (let i = 0; i < elements.length; i++) {
let element = elements[i];
if (element.src && element.src.startsWith("./"))
element.src = "../" + element.src.substring(2);
if (element.href && element.href.startsWith("./"))
element.href = "../" + element.href.substring(2);
}
let template = templateDom.serialize();
2021-03-11 03:44:50 +01:00
fs.readFile(folder + "blog/src/posts.json", function (_, data) {
2020-04-02 21:54:20 +02:00
let json = JSON.parse(data);
for (let i = 0; i < json.length; i++) {
let post = json[i];
2021-03-11 03:44:50 +01:00
fs.readFile(folder + "blog/src/" + post.id + ".md", function (_, content) {
2020-04-02 21:54:20 +02:00
let dom = new JSDOM(template);
let document = dom.window.document;
2020-04-02 21:54:20 +02: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);
let nav = "";
2020-04-02 21:54:20 +02:00
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)
2021-03-17 03:07:12 +01:00
nav += '<a class="nav-item nav-link" href="/blog/' + last.id + '">Previous ' + post.cat + ' Post</a>';
2020-05-06 19:54:21 +02:00
let next = getAdjacentPost(json, i, 1);
if (next)
2021-03-17 03:07:12 +01:00
nav += '<a class="nav-item nav-link" href="/blog/' + next.id + '">Next ' + post.cat + ' Post</a>';
2020-04-02 21:54:20 +02:00
document.getElementById("nav-items").innerHTML = nav;
let c = "";
2020-04-02 21:54:20 +02:00
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 ret = dom.serialize();
2021-03-11 03:21:23 +01:00
fs.mkdir(folder + "blog", function () {
fs.writeFile(folder + "blog/" + post.id + ".html", ret, function (_, _) {});
});
2020-04-02 21:54:20 +02:00
});
}
2019-10-09 15:51:43 +02:00
});
2020-05-06 19:54:21 +02:00
});
function getAdjacentPost(json, index, move) {
2021-03-17 03:07:12 +01:00
let cat = json[index].cat;
2020-05-06 19:54:21 +02:00
while (true) {
index += move;
let post = json[index];
if (!post)
break;
2021-03-17 03:07:12 +01:00
if (post.cat == cat && !post.archived)
2020-05-06 19:54:21 +02:00
return post;
}
}