-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgenerate-sitemap.js
40 lines (34 loc) · 1.29 KB
/
generate-sitemap.js
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
const fs = require('fs');
const globby = require('globby');
const prettier = require('prettier');
(async () => {
const prettierConfig = await prettier.resolveConfig('./.prettierrc.js');
// Ignore Next.js specific files (e.g., _app.js) and API routes.
const pages = await globby(['posts/*.md', 'pages/**/*.tsx', '!pages/_*{.jsx,.tsx}']);
const sitemap = `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${pages
.map((page) => {
const path = page
.replace('pages', '')
.replace('posts', '/blog')
.replace('.tsx', '')
.replace('.md', '');
const route = path === '/index' ? '' : path;
return `
<url>
<loc>${`${process.env.BASE_URL}${route}`}</loc>
</url>
`;
})
.join('')}
</urlset>
`;
// If you're not using Prettier, you can remove this.
const formatted = prettier.format(sitemap, {
...prettierConfig,
parser: 'html'
});
fs.writeFileSync('public/sitemap.xml', formatted);
})();