mirror of
https://github.com/andwati/personal-website.git
synced 2026-10-18 02:32:54 +00:00
Add script to generate sitemap from content slugs
This commit is contained in:
@@ -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 = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>https://andwati.xyz</loc>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://andwati.xyz/writing</loc>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://andwati.xyz/engineering</loc>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://andwati.xyz/reading</loc>
|
||||
</url>
|
||||
<url>
|
||||
<loc>https://andwati.xyz/notes</loc>
|
||||
</url>${allSlugs
|
||||
.map((slug) => {
|
||||
return `
|
||||
<url>
|
||||
<loc>${`https://andwati.xyz${slug}`}</loc>
|
||||
</url>`;
|
||||
})
|
||||
.join("")}
|
||||
</urlset>`;
|
||||
|
||||
if (fs.existsSync("public/sitemap.xml")) {
|
||||
fs.unlinkSync("public/sitemap.xml");
|
||||
}
|
||||
|
||||
fs.writeFileSync("public/sitemap.xml", sitemap);
|
||||
}
|
||||
|
||||
main();
|
||||
Reference in New Issue
Block a user