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 (_, data) { // this can probably be done a lot easier but regex is hard ok var template = data.toString().replace(/([^](?!))*/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 += 'Back to Main Page'; if (i > 0) nav += 'Previous Post'; if (i < json.length - 1) nav += 'Next Post'; document.getElementById("nav-items").innerHTML = nav; var c = ""; c += '
'; c += '
' c += '

' + post.name + '

'; c += '
' c += converter.makeHtml(content.toString()); c += '
'; c += '' + post.date + ""; var discussLink = post.discuss; if (discussLink) c += 'Discuss this post' c += '
'; document.getElementById("main").innerHTML = c; let html = dom.serialize(); fs.writeFile(folder + "blog-" + post.id + ".html", html, function (_, _) {}); }); } }); });