-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathsitemap-generator.mjs
More file actions
47 lines (40 loc) · 1.44 KB
/
sitemap-generator.mjs
File metadata and controls
47 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import fs from "fs";
import glob from "glob";
import corndocsConfig from "../corndocs.config.js";
function addPage(page) {
page =
page[page.length - 1] === "/" ? page.substring(0, page.length - 1) : page;
return `<url>
<loc>${`${corndocsConfig.project.url}/${page}`}</loc>
<lastmod>${new Date().toISOString()}</lastmod>
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>`;
}
async function generateSitemap() {
console.log("--- Generating Sitemap ---");
const blogsDir = "_posts/**/*.*";
let blogPaths = await glob.sync(blogsDir);
const pagesDir = "pages/**/*.tsx";
let pagesPaths = await glob.sync(pagesDir);
pagesPaths = pagesPaths
.filter((path) => !path.includes("["))
.filter((path) => !path.includes("api"))
.filter((path) => !path.includes("/_"))
.filter((path) => !path.includes("404"));
blogPaths = blogPaths.map(
(rawBlogName) =>
"Docs" + rawBlogName.replace("_posts", "").replace(".mdx", "")
);
pagesPaths = pagesPaths.map((rawPageName) =>
rawPageName.replace("pages/", "").replace(".tsx", "").replace("index", "")
);
const allPages = [...pagesPaths, ...blogPaths];
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${allPages.map(addPage).join("\n")}
</urlset>`;
fs.writeFileSync("public/sitemap.xml", sitemap);
console.log("--- Finished Generating Sitemap ---");
}
generateSitemap();