forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap.ts
30 lines (25 loc) · 1.12 KB
/
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
import AstroSitemap from '@astrojs/sitemap';
import type { AstroIntegration } from 'astro';
import { normalizeLangTag } from '../src/i18n/bcp-normalize';
import languages from '../src/i18n/languages';
const langTags = Object.keys(languages);
/** Set matching our `/[lang]/something.astro` redirect routes. */
const blocklist = new Set([
...langTags.map((lang) => `/${lang}/`),
...langTags.map((lang) => `/${lang}/install/`),
...langTags.map((lang) => `/${lang}/tutorial/`),
]);
/** Match a pathname starting with “lighthouse” or one of our language tags. */
const ValidRouteRE = new RegExp(`^/(lighthouse|${langTags.join('|')})/`);
/** Test a pathname is not in our blocklist and starts with a valid prefix. */
const isValidPath = (path: string) => !blocklist.has(path) && ValidRouteRE.test(path);
/** Get a preconfigured instance of the `@astrojs/sitemap` integration. */
export function sitemap(): AstroIntegration {
return AstroSitemap({
filter: (page) => isValidPath(new URL(page).pathname),
i18n: {
defaultLocale: 'en',
locales: Object.fromEntries(langTags.map((lang) => [lang, normalizeLangTag(lang)])),
},
});
}