Web/node/blog.js
Ell 8a14c03989
All checks were successful
Web/pipeline/head This commit looks good
some code style improvements
2021-03-19 12:02:31 +01:00

82 lines
No EOL
3.7 KiB
JavaScript

const {
JSDOM
} = require("jsdom");
const fs = require("fs");
const converter = require("./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];
if (element.src && element.src.startsWith("./"))
element.src = `../${element.src.substring(2)}`;
if (element.href && element.href.startsWith("./"))
element.href = `../${element.href.substring(2)}`;
}
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);
fs.readFile(`${folder}blog/src/${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);
document.getElementById("nav-items").innerHTML = /*html*/ `
<a class="nav-item nav-link" href="/#blog">Back to Main Page</a>
${last ? /*html*/ `<a class="nav-item nav-link" href="/blog/${last.id}">Previous ${post.cat} Post</a>` : ""}
${next ? /*html*/ `<a class="nav-item nav-link" href="/blog/${next.id}">Next ${post.cat} Post</a>` : ""}
`;
document.getElementById("main").innerHTML = /*html*/ `
<div class="list-display rounded">
<div class="blog-isolated">
<h1>${post.name}</h1>
<div id="blog-post-${post.id}">
${post.archived ? /*html*/ `<p><i>This post has been archived.</i></p>` : ""}
${converter.makeHtml(content.toString())}
</div>
<span class="text-muted project-status blog-isolated-status">${post.date}</span>
${post.discuss ? /*html*/ `<a href="${post.discuss}" class="blog-discuss" id="blog-discuss-${post.id}">Discuss this post</a>` : ""}
</div>
</div>
`;
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;
while (true) {
index += move;
let post = json[index];
if (!post)
break;
if (post.cat === cat && !post.archived)
return post;
}
}