Skip to content

Commit 2e88753

Browse files
committedNov 29, 2024·
feat(seo): canonical url
1 parent 5df19d5 commit 2e88753

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed
 

‎.vitepress/config/index.ts

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,47 @@
11
import { defineConfig } from 'vitepress';
2+
import type { LocaleConfig, DefaultTheme, HeadConfig } from 'vitepress';
23
import { shared } from './shared';
34
import { ja } from './ja';
45
import { en } from './en';
56

7+
const locales: LocaleConfig<DefaultTheme.Config> = {
8+
ja: { label: '日本語', ...ja },
9+
en: { label: 'English', ...en },
10+
};
11+
12+
const baseUrl = 'https://aiscript-dev.github.io';
13+
614
export default defineConfig({
715
...shared,
8-
locales: {
9-
ja: { label: '日本語', ...ja },
10-
en: { label: 'English', ...en },
16+
locales,
17+
transformHead(context) {
18+
const head: HeadConfig[] = [ ...context.head ];
19+
if (!context.pageData.isNotFound) {
20+
const localesRegex = new RegExp(`^/(${Object.keys(locales).join('|')})`);
21+
const canonical = '/' + context.page.replace(/(index)?\.md$/, '');
22+
23+
for (const locale of Object.keys(locales)) {
24+
const localePath = canonical.replace(localesRegex, `/${locale}`);
25+
26+
head.push([
27+
'link',
28+
{
29+
rel: 'alternate',
30+
hreflang: locales[locale].lang || locale,
31+
href: baseUrl + localePath,
32+
},
33+
]);
34+
}
35+
36+
head.push([
37+
'link',
38+
{
39+
rel: 'canonical',
40+
href: baseUrl + canonical,
41+
}
42+
]);
43+
}
44+
45+
return head;
1146
},
1247
});

0 commit comments

Comments
 (0)
Please sign in to comment.