-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-sitemap.ts
70 lines (57 loc) · 1.83 KB
/
generate-sitemap.ts
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import * as fs from "fs";
import * as path from "path";
// Configuration
const BASE_URL = "http://localhost:3000/documentation";
const CONTENT_DIR = path.join(__dirname, "content");
const OUTPUT_FILE = path.join(__dirname, "public", "sitemap.xml");
const VALID_EXTENSIONS = [".md", ".mdx"];
function getUrlPathFromFile(filePath: string): string {
const relativePath = path.relative(CONTENT_DIR, filePath);
let urlPath = relativePath.replace(/\\/g, "/"); // Normalize Windows paths
for (const ext of VALID_EXTENSIONS) {
if (urlPath.endsWith(ext)) {
urlPath = urlPath.slice(0, -ext.length);
break;
}
}
if (urlPath.endsWith("/index")) {
urlPath = urlPath.slice(0, -"index".length - 1);
}
return `${BASE_URL}${urlPath ? "/" + urlPath : ""}`;
}
function walk(dir: string): string[] {
let results: string[] = [];
const list = fs.readdirSync(dir);
list.forEach((file) => {
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat && stat.isDirectory()) {
results = results.concat(walk(fullPath));
} else if (VALID_EXTENSIONS.includes(path.extname(fullPath))) {
results.push(fullPath);
}
});
return results;
}
function generateSitemap() {
const files = walk(CONTENT_DIR);
const urls = files.map(getUrlPathFromFile);
const timestamp = new Date().toISOString();
const xmlEntries = urls
.map((url) => {
return `
<url>
<loc>${url}</loc>
<lastmod>${timestamp}</lastmod>
</url>`;
})
.join("");
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${xmlEntries}
</urlset>`;
fs.mkdirSync(path.dirname(OUTPUT_FILE), { recursive: true });
fs.writeFileSync(OUTPUT_FILE, sitemap.trim());
console.log(`✅ Sitemap generated at ${OUTPUT_FILE}`);
}
generateSitemap();