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();