Skip to content

Commit 18c9357

Browse files
committed
refactor: review
1 parent 8d07613 commit 18c9357

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/generators/sitemap/index.mjs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { writeFile } from 'node:fs/promises';
1+
import { readFile, writeFile } from 'node:fs/promises';
22
import { join } from 'node:path';
33

4+
import dedent from 'dedent';
5+
46
import { BASE_URL } from '../../constants.mjs';
57

68
/**
@@ -27,6 +29,8 @@ export default {
2729
* @returns {Promise<string>}
2830
*/
2931
async generate(entries, { output }) {
32+
const lastmod = new Date().toISOString().split('T')[0];
33+
3034
const apiPages = entries
3135
.filter(entry => entry.heading.depth === 1)
3236
.map(entry => {
@@ -35,7 +39,7 @@ export default {
3539

3640
return {
3741
loc: url,
38-
lastmod: new Date().toISOString().split('T')[0],
42+
lastmod,
3943
changefreq: 'weekly',
4044
priority: '0.8',
4145
};
@@ -44,27 +48,33 @@ export default {
4448
const mainPages = [
4549
{
4650
loc: new URL('/docs/latest/api/', BASE_URL).href,
47-
lastmod: new Date().toISOString().split('T')[0],
51+
lastmod,
4852
changefreq: 'daily',
4953
priority: '1.0',
5054
},
5155
];
5256

5357
const allPages = [...mainPages, ...apiPages];
5458

55-
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
56-
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
57-
${allPages
58-
.map(
59-
page => ` <url>
60-
<loc>${page.loc}</loc>
61-
<lastmod>${page.lastmod}</lastmod>
62-
<changefreq>${page.changefreq}</changefreq>
63-
<priority>${page.priority}</priority>
64-
</url>`
65-
)
66-
.join('\n')}
67-
</urlset>`;
59+
const template = await readFile(
60+
join(import.meta.dirname, 'template.xml'),
61+
'utf-8'
62+
);
63+
64+
const urlset = allPages
65+
.map(
66+
page => dedent`
67+
<url>
68+
<loc>${page.loc}</loc>
69+
<lastmod>${page.lastmod}</lastmod>
70+
<changefreq>${page.changefreq}</changefreq>
71+
<priority>${page.priority}</priority>
72+
</url>
73+
`
74+
)
75+
.join('\n');
76+
77+
const sitemap = template.replace('__URLSET__', urlset);
6878

6979
if (output) {
7080
await writeFile(join(output, 'sitemap.xml'), sitemap, 'utf-8');
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3+
__URLSET__
4+
</urlset>

0 commit comments

Comments
 (0)