From 6b4d8484f8a01766521ed0fa3b553a4d307e4ff2 Mon Sep 17 00:00:00 2001 From: andwati Date: Sun, 1 Feb 2026 19:20:21 +0300 Subject: [PATCH] Add script to generate sitemap from content slugs --- scripts/generate-sitemap.mjs | 75 ++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 scripts/generate-sitemap.mjs diff --git a/scripts/generate-sitemap.mjs b/scripts/generate-sitemap.mjs new file mode 100644 index 0000000..6209f93 --- /dev/null +++ b/scripts/generate-sitemap.mjs @@ -0,0 +1,75 @@ +import fs from "fs"; +import path from "path"; + +function getAllBookSlugs() { + const data = JSON.parse( + fs.readFileSync( + path.join(process.cwd(), "content", "books", "index.json"), + "utf8", + ), + ); + return data.map((item) => item.slug); +} + +function getAllWritingSlugs() { + const data = JSON.parse( + fs.readFileSync( + path.join(process.cwd(), "content", "writing", "index.json"), + "utf8", + ), + ); + + return data.filter((item) => !item.external).map((item) => item.url); +} + +function getAllEngineeringSlugs() { + const data = JSON.parse( + fs.readFileSync( + path.join(process.cwd(), "content", "engineering", "index.json"), + "utf8", + ), + ); + + return data.filter((item) => !item.external).map((item) => item.url); +} + +async function main() { + const bookSlugs = getAllBookSlugs(); + const writingSlugs = getAllWritingSlugs(); + const engineeringSlugs = getAllEngineeringSlugs(); + const allSlugs = [...bookSlugs, ...writingSlugs, ...engineeringSlugs]; + + const sitemap = ` + + + https://andwati.xyz + + + https://andwati.xyz/writing + + + https://andwati.xyz/engineering + + + https://andwati.xyz/reading + + + https://andwati.xyz/notes + ${allSlugs + .map((slug) => { + return ` + + ${`https://andwati.xyz${slug}`} + `; + }) + .join("")} +`; + + if (fs.existsSync("public/sitemap.xml")) { + fs.unlinkSync("public/sitemap.xml"); + } + + fs.writeFileSync("public/sitemap.xml", sitemap); +} + +main();