-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsitemap.ts
More file actions
71 lines (61 loc) · 2.3 KB
/
sitemap.ts
File metadata and controls
71 lines (61 loc) · 2.3 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { paths } from './data/paths';
import { Cssville } from "cssville-generators/build/cssville";
const path = require('path');
const fs = require('fs');
interface SitemapEntry {
loc: string;
lastmod: string;
}
function traversePaths(obj: any, basePath: string = ''): SitemapEntry[] {
let entries: SitemapEntry[] = [];
Object.keys(obj).forEach((key) => {
const value = obj[key];
if (typeof value === 'string') {
// If the value is a string, it's a path
// Add /docs prefix for components and css-classes paths
const needsDocsPrefix = (basePath === 'components' || value.startsWith('components/') ||
value.startsWith('css-classes/') || basePath === 'intro' ||
value.startsWith('intro/'));
const prefix = needsDocsPrefix ? '/docs' : '';
entries.push({
loc: `${prefix}/${value}`.replace(/\/+/g, '/'), // Add prefix, leading slash and normalize
lastmod: new Date().toISOString().split('T')[0],
});
} else if (typeof value === 'function' && key === 'cssClasses') {
// Special case for dynamic paths, such as cssClasses
entries.push(
...Cssville.generators.map((g) => ({
loc: `/docs/${value(g.name)}`.replace(/\/+/g, '/'), // Add /docs prefix, leading slash and normalize
lastmod: new Date().toISOString().split('T')[0],
}))
);
} else if (typeof value === 'object') {
// If the value is an object, recurse
entries = entries.concat(traversePaths(value, key));
}
});
return entries;
}
// Generate the sitemap entries from the paths
function buildSitemapEntries(): SitemapEntry[] {
return traversePaths(paths);
}
// Generate the sitemap entries
const urls: SitemapEntry[] = buildSitemapEntries();
const domain = "cssville.xyz";
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${urls
.map(
(url) => `
<url>
<loc>${`https://${domain}${url.loc}`}</loc>
<lastmod>${url.lastmod}</lastmod>
</url>`
)
.join('')}
</urlset>`;
// Write the sitemap.xml file to the public directory
const sitemapPath = path.resolve(__dirname, 'sitemap.xml');
fs.writeFileSync(sitemapPath, xml);
console.log('sitemap.xml has been generated at', sitemapPath);