made feeds, blog and sitemap be created on script startup too

This commit is contained in:
Ellpeck 2019-10-10 12:04:08 +02:00
parent 61b5707057
commit 7731646970
3 changed files with 104 additions and 86 deletions

View file

@ -14,59 +14,65 @@ const converter = new showdown.Converter({
module.exports = function () { module.exports = function () {
let folder = __dirname + "/../"; let folder = __dirname + "/../";
createBlogPages(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) { fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime) if (curr.mtime == prev.mtime)
return; return;
console.log("Refreshing blog sub-sites..."); createBlogPages(folder);
});
}
fs.readFile(folder + "index.html", function (_, data) { function createBlogPages(folder) {
// set up the template console.log("Refreshing blog sub-sites...");
let templateDom = new JSDOM(data);
var templateDoc = templateDom.window.document;
templateDoc.getElementById("main").innerHTML = "";
let template = templateDom.serialize();
fs.readFile(folder + "blog/posts.json", function (_, data) { fs.readFile(folder + "index.html", function (_, data) {
let json = JSON.parse(data); // set up the template
for (let i = 0; i < json.length; i++) { let templateDom = new JSDOM(data);
let post = json[i]; var templateDoc = templateDom.window.document;
let id = post["id"]; templateDoc.getElementById("main").innerHTML = "";
fs.readFile(folder + "blog/" + id + ".md", function (_, content) { let template = templateDom.serialize();
let dom = new JSDOM(template);
var document = dom.window.document;
document.title += " - " + post["name"]; fs.readFile(folder + "blog/posts.json", function (_, data) {
document.querySelector('meta[property="og:title"]').setAttribute("content", post["name"]); let json = JSON.parse(data);
document.querySelector('meta[name="description"]').setAttribute("content", post["summary"]); for (let i = 0; i < json.length; i++) {
document.querySelector('meta[property="og:description"]').setAttribute("content", post["summary"]); let post = json[i];
let id = post["id"];
fs.readFile(folder + "blog/" + id + ".md", function (_, content) {
let dom = new JSDOM(template);
var document = dom.window.document;
var nav = ""; document.title += " - " + post["name"];
nav += '<a class="nav-item nav-link" href="/#blog">Back to Main Page</a>'; document.querySelector('meta[property="og:title"]').setAttribute("content", post["name"]);
if (i > 0) document.querySelector('meta[name="description"]').setAttribute("content", post["summary"]);
nav += '<a class="nav-item nav-link" href="/blog-' + json[i - 1]["id"] + '">Previous Post</a>'; document.querySelector('meta[property="og:description"]').setAttribute("content", post["summary"]);
if (i < json.length - 1)
nav += '<a class="nav-item nav-link" href="/blog-' + json[i + 1]["id"] + '">Next Post</a>';
document.getElementById("nav-items").innerHTML = nav;
var c = ""; var nav = "";
c += '<div class="list-display rounded">'; nav += '<a class="nav-item nav-link" href="/#blog">Back to Main Page</a>';
c += '<div class="blog-isolated">' if (i > 0)
c += '<h1>' + post["name"] + '</h1>'; nav += '<a class="nav-item nav-link" href="/blog-' + json[i - 1]["id"] + '">Previous Post</a>';
c += '<div id="blog-post-' + id + '">' if (i < json.length - 1)
c += converter.makeHtml(content.toString()); nav += '<a class="nav-item nav-link" href="/blog-' + json[i + 1]["id"] + '">Next Post</a>';
c += '</div>'; document.getElementById("nav-items").innerHTML = nav;
c += '<span class="text-muted project-status blog-isolated-status">' + post["date"] + "</span>";
var discussLink = post["discuss"];
if (discussLink)
c += '<a href="' + discussLink + '" class="blog-discuss" id="blog-discuss-' + id + '">Discuss this post</a>'
c += '</div></div>';
document.getElementById("main").innerHTML = c;
let html = dom.serialize(); var c = "";
fs.writeFile(folder + "blog-" + id + ".html", html, function (_, _) {}); c += '<div class="list-display rounded">';
}); c += '<div class="blog-isolated">'
} c += '<h1>' + post["name"] + '</h1>';
}); c += '<div id="blog-post-' + id + '">'
c += converter.makeHtml(content.toString());
c += '</div>';
c += '<span class="text-muted project-status blog-isolated-status">' + post["date"] + "</span>";
var discussLink = post["discuss"];
if (discussLink)
c += '<a href="' + discussLink + '" class="blog-discuss" id="blog-discuss-' + id + '">Discuss this post</a>'
c += '</div></div>';
document.getElementById("main").innerHTML = c;
let html = dom.serialize();
fs.writeFile(folder + "blog-" + id + ".html", html, function (_, _) {});
});
}
}); });
}); });
} }

View file

@ -13,15 +13,21 @@ const converter = new showdown.Converter({
module.exports = function () { module.exports = function () {
let folder = __dirname + "/../"; let folder = __dirname + "/../";
createFeeds(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) { fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime) if (curr.mtime == prev.mtime)
return; return;
console.log("Refreshing feeds..."); createFeeds(folder);
createFeed(function (feed) { });
fs.writeFile(folder + "feed.json", feed.json1(), function (_, _) {}); }
fs.writeFile(folder + "rss.xml", feed.rss2(), function (_, _) {});
fs.writeFile(folder + "atom.xml", feed.atom1(), function (_, _) {}); function createFeeds(folder) {
}); console.log("Refreshing feeds...");
createFeed(function (feed) {
fs.writeFile(folder + "feed.json", feed.json1(), function (_, _) {});
fs.writeFile(folder + "rss.xml", feed.rss2(), function (_, _) {});
fs.writeFile(folder + "atom.xml", feed.atom1(), function (_, _) {});
}); });
} }

View file

@ -5,48 +5,54 @@ const fs = require("fs");
module.exports = function () { module.exports = function () {
let folder = __dirname + "/../"; let folder = __dirname + "/../";
refreshSitemap(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) { fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime) if (curr.mtime == prev.mtime)
return; return;
console.log("Refreshing sitemap..."); refreshSitemap(folder);
});
let sitemap = createSitemap({ }
hostname: 'https://ellpeck.de',
urls: [{ function refreshSitemap(folder) {
url: '/', console.log("Refreshing sitemap...");
priority: 0.8
}, let sitemap = createSitemap({
{ hostname: 'https://ellpeck.de',
url: '/#projects', urls: [{
changefreq: 'monthly' url: '/',
}, priority: 0.8
{ },
url: '/#social', {
changefreq: 'yearly' url: '/#projects',
}, changefreq: 'monthly'
{ },
url: '/#about', {
changefreq: 'monthly' url: '/#social',
}, changefreq: 'yearly'
{ },
url: '/#blog', {
changefreq: 'weekly', url: '/#about',
priority: 0.6 changefreq: 'monthly'
} },
] {
}); url: '/#blog',
changefreq: 'weekly',
fs.readFile(folder + "blog/posts.json", function (_, data) { priority: 0.6
let json = JSON.parse(data); }
]
for (let post of json) { });
sitemap.add({
url: "/blog-" + post["id"], fs.readFile(folder + "blog/posts.json", function (_, data) {
priority: 0.4 let json = JSON.parse(data);
});
} for (let post of json) {
sitemap.add({
fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {}); url: "/blog-" + post["id"],
}); priority: 0.4
});
}
fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {});
}); });
} }