update sitemap

This commit is contained in:
adueck 2023-12-25 22:56:43 +04:00
parent 18ecac9990
commit d104f0292b
2 changed files with 23 additions and 16 deletions

View File

@ -1,3 +1,4 @@
# https://www.robotstxt.org/robotstxt.html # https://www.robotstxt.org/robotstxt.html
User-agent: * User-agent: *
Disallow: Disallow:
Sitemap: https://grammar.lingdocs.com/sitemap.xml

View File

@ -8,31 +8,37 @@ const indexText = fs.readFileSync("./src/content/index.ts", "utf-8");
const contentTreeRaw = indexText.split("/* content-tree */")[1]; const contentTreeRaw = indexText.split("/* content-tree */")[1];
const safeContentTreeText = contentTreeRaw const safeContentTreeText = contentTreeRaw
.split("\n") .split("\n")
.filter(l => !l.includes(`"import":`)) .filter((l) => !l.includes(`"import":`))
.join("\n"); .join("\n");
const contentTree = JSON.parse(safeContentTreeText); const contentTree = JSON.parse(safeContentTreeText);
const urls = contentTree.reduce((acc, section) => { const urls = contentTree.reduce(
(acc, section) => {
if ("slug" in section) { if ("slug" in section) {
return [ return [...acc, `${base}/${section.slug}/`];
...acc,
`${base}/${section.slug}`,
];
} else { } else {
return [ return [
...acc, ...acc,
...section.chapters.map(x => `${base}/${section.subdirectory}/${x.slug}`), ...section.chapters.map(
(x) => `${base}/${section.subdirectory}/${x.slug}/`
),
]; ];
} }
}, [base]); },
[`${base}`]
);
const siteMap = `<?xml version="1.0" encoding="UTF-8"?> const siteMap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls.map(u => ` <url> ${urls
.map(
(u) => ` <url>
<loc>${u}</loc> <loc>${u}</loc>
</url> </url>
`).join("")}
</urlset>
` `
)
.join("")}
</urlset>
`;
fs.writeFileSync("./public/sitemap.xml", siteMap); fs.writeFileSync("./public/sitemap.xml", siteMap);