const { JSDOM } = require("jsdom"); const fs = require("fs"); const util = require("./util"); const converter = util.showdown(2); const folder = `${__dirname}/../`; console.log("Refreshing blog sub-sites..."); fs.readFile(`${folder}index.html`, function (_, html) { let templateDom = new JSDOM(html); // 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]); // 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]; util.replaceRelativeLinks(element, "src"); util.replaceRelativeLinks(element, "href"); } const template = templateDom.serialize(); fs.readFile(`${folder}blog/src/posts.json`, function (_, data) { const json = JSON.parse(data.toString()); for (let i = 0; i < json.length; i++) { const post = json[i]; const index = i; const last = getAdjacentPost(json, index, -1); const next = getAdjacentPost(json, index, 1); let extension = post.book ? "html" : "md"; fs.readFile(`${folder}blog/src/${post.id}.${extension}`, function (_, file) { 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); document.getElementById("nav-items").innerHTML = /*html*/ ` Back to Main Page ${last ? /*html*/ `Previous ${post.cat[0]} Post` : ""} ${next ? /*html*/ `Next ${post.cat[0]} Post` : ""} `; let content; if (post.book) { document.head.innerHTML += /*html*/ ``; content = util.extractBookData(file, post, true); } else { content = converter.makeHtml(file.toString()); } document.getElementById("main").innerHTML = /*html*/ `

${post.name}

${post.archived ? /*html*/ `

This post has been archived.

` : ""} ${content} ${post.date} ${post.discuss ? /*html*/ `Discuss this post` : ""}
`; let ret = dom.serialize(); fs.mkdir(`${folder}blog`, function () { fs.writeFile(`${folder}blog/${post.id}.html`, ret, function () {}); }); }); } }); }); function getAdjacentPost(json, index, move) { let cat = json[index].cat[0]; while (true) { index += move; let post = json[index]; if (!post) break; // only link to posts with the same main category if (post.cat[0] === cat && !post.archived) return post; } }