const { createHash } = require("crypto");
const {
readdirSync,
readFileSync,
statSync,
writeFileSync,
existsSync,
} = require("fs");
const { basename, extname, join, relative } = require("path");
const { author, description } = require("../package.json");
const BLOG_EXTENSION = ".whtml";
const PUBLIC_PATH = "public";
const feedFiles = [];
const getFeedFiles = (path) =>
existsSync(path) &&
readdirSync(path).forEach((entry) => {
const fullPath = join(path, entry);
const stats = statSync(fullPath);
if (stats.isDirectory()) {
getFeedFiles(fullPath);
} else if (extname(entry).toLowerCase() === BLOG_EXTENSION) {
feedFiles.push([fullPath, stats]);
}
});
let lastBuildDate;
const createRssFeedItems = (url) => {
feedFiles.length = 0;
getFeedFiles(join(PUBLIC_PATH, url));
feedFiles.sort(([, aStats], [, bStats]) => bStats.mtimeMs - aStats.mtimeMs);
lastBuildDate = feedFiles?.[0]?.[1]?.mtime.toUTCString();
return feedFiles.map(([link, stats]) => {
const fileData = readFileSync(link);
const fileDataString = fileData.toString("utf8");
const itemDescription = fileDataString
.replace(/<[^>]*>?/gm, "")
.replace(/ /g, " ")
.substring(0, 100)
.trim();
return [
"- ",
`${basename(link, BLOG_EXTENSION)}`,
`${encodeURI(`${author.url}/?url=${relative(PUBLIC_PATH, link).replace(/\\/g, "/")}`)}`,
`${itemDescription}${fileDataString.trim().length > 100 ? "..." : ""}`,
`${createHash("md5").update(fileData).digest("hex")}`,
`${stats.mtime.toUTCString()}`,
"
",
].join("");
});
};
const name = "daedalOS";
const rss = [
'',
'',
"",
``,
`${name}`,
`${author.url}`,
`${description}`,
`${lastBuildDate || new Date().toUTCString()}`,
...createRssFeedItems("Users/Public/Documents/Blog Posts"),
"",
"",
].join("");
writeFileSync(join(PUBLIC_PATH, "rss.xml"), rss, { flag: "w" });