|
1 | 1 | import { defineConfig } from 'vitepress';
|
| 2 | +import type { LocaleConfig, DefaultTheme, HeadConfig } from 'vitepress'; |
2 | 3 | import { shared } from './shared';
|
3 | 4 | import { ja } from './ja';
|
4 | 5 | import { en } from './en';
|
5 | 6 |
|
| 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 | + |
6 | 14 | export default defineConfig({
|
7 | 15 | ...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; |
11 | 46 | },
|
12 | 47 | });
|
0 commit comments