const { JSDOM } = require("jsdom"); const fs = require("fs"); const converter = require("./showdown")(2); let folder = __dirname + "/../"; console.log("Refreshing blog sub-sites..."); fs.readFile(folder + "index.html", function (_, html) { let templateDom = new JSDOM(html); let noBlog = templateDom.window.document.getElementsByClassName("no-blog"); while (noBlog.length > 0) noBlog[0].parentNode.removeChild(noBlog[0]); 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); let 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); let nav = ""; nav += 'Back to Main Page'; let last = getAdjacentPost(json, i, -1); if (last) nav += 'Previous Post'; let next = getAdjacentPost(json, i, 1); if (next) nav += 'Next Post'; document.getElementById("nav-items").innerHTML = nav; let c = ""; c += '
'; c += '
' c += '

' + post.name + '

'; c += '
' if (post.archived) c += "

This post has been archived.

" c += converter.makeHtml(content.toString()); c += '
'; c += '' + post.date + ""; if (post.discuss) c += 'Discuss this post' c += '
'; document.getElementById("main").innerHTML = c; let ret = dom.serialize(); fs.writeFile(folder + "blog-" + post.id + ".html", ret, function (_, _) {}); }); } }); }); function getAdjacentPost(json, index, move) { while (true) { index += move; let post = json[index]; if (!post) break; if (!post.archived) return post; } }