2023-02-09 09:26:51 +00:00
|
|
|
import fs from "fs";
|
|
|
|
|
|
|
|
const base = "https://grammar.lingdocs.com";
|
|
|
|
|
|
|
|
console.log("generating sitemap.xml");
|
|
|
|
|
|
|
|
const indexText = fs.readFileSync("./src/content/index.ts", "utf-8");
|
|
|
|
const contentTreeRaw = indexText.split("/* content-tree */")[1];
|
|
|
|
const safeContentTreeText = contentTreeRaw
|
2023-12-25 18:56:43 +00:00
|
|
|
.split("\n")
|
|
|
|
.filter((l) => !l.includes(`"import":`))
|
|
|
|
.join("\n");
|
2023-02-09 09:26:51 +00:00
|
|
|
const contentTree = JSON.parse(safeContentTreeText);
|
|
|
|
|
2023-12-25 18:56:43 +00:00
|
|
|
const urls = contentTree.reduce(
|
|
|
|
(acc, section) => {
|
2023-02-09 09:26:51 +00:00
|
|
|
if ("slug" in section) {
|
2023-12-25 18:56:43 +00:00
|
|
|
return [...acc, `${base}/${section.slug}/`];
|
2023-02-09 09:26:51 +00:00
|
|
|
} else {
|
2023-12-25 18:56:43 +00:00
|
|
|
return [
|
|
|
|
...acc,
|
|
|
|
...section.chapters.map(
|
|
|
|
(x) => `${base}/${section.subdirectory}/${x.slug}/`
|
|
|
|
),
|
|
|
|
];
|
2023-02-09 09:26:51 +00:00
|
|
|
}
|
2023-12-25 18:56:43 +00:00
|
|
|
},
|
|
|
|
[`${base}`]
|
|
|
|
);
|
2023-02-09 09:26:51 +00:00
|
|
|
|
|
|
|
const siteMap = `<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
2023-12-25 18:56:43 +00:00
|
|
|
${urls
|
|
|
|
.map(
|
|
|
|
(u) => ` <url>
|
2023-02-09 09:26:51 +00:00
|
|
|
<loc>${u}</loc>
|
|
|
|
</url>
|
|
|
|
`
|
2023-12-25 18:56:43 +00:00
|
|
|
)
|
|
|
|
.join("")}
|
|
|
|
</urlset>
|
|
|
|
`;
|
2023-02-09 09:26:51 +00:00
|
|
|
|
|
|
|
fs.writeFileSync("./public/sitemap.xml", siteMap);
|