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 () {
let folder = __dirname + "/../";
createBlogPages(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime)
return;
console.log("Refreshing blog sub-sites...");
createBlogPages(folder);
});
}
fs.readFile(folder + "index.html", function (_, data) {
// set up the template
let templateDom = new JSDOM(data);
var templateDoc = templateDom.window.document;
templateDoc.getElementById("main").innerHTML = "";
let template = templateDom.serialize();
function createBlogPages(folder) {
console.log("Refreshing blog sub-sites...");
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];
let id = post["id"];
fs.readFile(folder + "blog/" + id + ".md", function (_, content) {
let dom = new JSDOM(template);
var document = dom.window.document;
fs.readFile(folder + "index.html", function (_, data) {
// set up the template
let templateDom = new JSDOM(data);
var templateDoc = templateDom.window.document;
templateDoc.getElementById("main").innerHTML = "";
let template = templateDom.serialize();
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"]);
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];
let id = post["id"];
fs.readFile(folder + "blog/" + id + ".md", function (_, content) {
let dom = new JSDOM(template);
var document = dom.window.document;
var nav = "";
nav += '<a class="nav-item nav-link" href="/#blog">Back to Main Page</a>';
if (i > 0)
nav += '<a class="nav-item nav-link" href="/blog-' + json[i - 1]["id"] + '">Previous Post</a>';
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;
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 c = "";
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;
var nav = "";
nav += '<a class="nav-item nav-link" href="/#blog">Back to Main Page</a>';
if (i > 0)
nav += '<a class="nav-item nav-link" href="/blog-' + json[i - 1]["id"] + '">Previous Post</a>';
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;
let html = dom.serialize();
fs.writeFile(folder + "blog-" + id + ".html", html, function (_, _) {});
});
}
});
var c = "";
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 () {
let folder = __dirname + "/../";
createFeeds(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime)
return;
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 (_, _) {});
});
createFeeds(folder);
});
}
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 () {
let folder = __dirname + "/../";
refreshSitemap(folder);
fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime)
return;
console.log("Refreshing sitemap...");
refreshSitemap(folder);
});
}
let sitemap = createSitemap({
hostname: 'https://ellpeck.de',
urls: [{
url: '/',
priority: 0.8
},
{
url: '/#projects',
changefreq: 'monthly'
},
{
url: '/#social',
changefreq: 'yearly'
},
{
url: '/#about',
changefreq: 'monthly'
},
{
url: '/#blog',
changefreq: 'weekly',
priority: 0.6
}
]
});
function refreshSitemap(folder) {
console.log("Refreshing sitemap...");
fs.readFile(folder + "blog/posts.json", function (_, data) {
let json = JSON.parse(data);
for (let post of json) {
sitemap.add({
url: "/blog-" + post["id"],
priority: 0.4
});
let sitemap = createSitemap({
hostname: 'https://ellpeck.de',
urls: [{
url: '/',
priority: 0.8
},
{
url: '/#projects',
changefreq: 'monthly'
},
{
url: '/#social',
changefreq: 'yearly'
},
{
url: '/#about',
changefreq: 'monthly'
},
{
url: '/#blog',
changefreq: 'weekly',
priority: 0.6
}
]
});
fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {});
});
fs.readFile(folder + "blog/posts.json", function (_, data) {
let json = JSON.parse(data);
for (let post of json) {
sitemap.add({
url: "/blog-" + post["id"],
priority: 0.4
});
}
fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {});
});
}