Web/node/sitemap.js

52 lines
1.4 KiB
JavaScript

const {
createSitemap
} = require('sitemap');
const fs = require("fs");
module.exports = function () {
let folder = __dirname + "/../";
fs.watchFile(folder + "blog/posts.json", function (curr, prev) {
if (curr.mtime == prev.mtime)
return;
console.log("Refreshing sitemap...");
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.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 (_, _) {});
});
});
}