Web/node/sitemap.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

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...");
2019-10-04 23:20:47 +02:00
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) {
2019-10-04 23:33:45 +02:00
let json = JSON.parse(data);
2019-10-04 23:20:47 +02:00
2019-10-04 23:33:45 +02:00
for (let post of json) {
2019-10-04 23:20:47 +02:00
sitemap.add({
2019-10-09 15:51:43 +02:00
url: "/blog-" + post["id"],
2019-10-04 23:20:47 +02:00
priority: 0.4
});
}
fs.writeFile(folder + "/sitemap.xml", sitemap.toXML(), function (_, _) {});
2019-10-04 23:20:47 +02:00
});
});
2019-10-04 23:20:47 +02:00
}