diff --git a/.vitepress/config/index.ts b/.vitepress/config/index.ts index 59556ca..e23b4c4 100644 --- a/.vitepress/config/index.ts +++ b/.vitepress/config/index.ts @@ -1,15 +1,20 @@ import { defineConfig } from 'vitepress'; import type { LocaleConfig, DefaultTheme, HeadConfig } from 'vitepress'; + import { shared } from './shared'; import { ja } from './ja'; import { en } from './en'; -const locales: LocaleConfig = { +import { genI18nRedirector } from '../scripts/gen-i18n-redirector'; + +const locales = { ja: { label: '日本語', ...ja }, en: { label: 'English', ...en }, -}; +} as const satisfies LocaleConfig; + +export const mainLocale = 'ja' as const satisfies keyof typeof locales; -const baseUrl = 'https://aiscript-dev.github.io'; +export const baseUrl = 'https://aiscript-dev.github.io'; export default defineConfig({ ...shared, @@ -44,4 +49,5 @@ export default defineConfig({ return head; }, + buildEnd: genI18nRedirector, }); diff --git a/.vitepress/scripts/gen-i18n-redirector.ts b/.vitepress/scripts/gen-i18n-redirector.ts new file mode 100644 index 0000000..94c88c8 --- /dev/null +++ b/.vitepress/scripts/gen-i18n-redirector.ts @@ -0,0 +1,47 @@ +//@ts-expect-error Node +import fs from 'fs'; +import { mainLocale, baseUrl } from '../config'; +import type { SiteConfig } from 'vitepress'; + +async function createFile(path: string, content: string) { + const dir = path.replace(/\/[^/]+$/, ''); + await fs.promises.writeFile(path, content).catch((err) => { + if (err.code === 'ENOENT') { + fs.promises.mkdir(dir, { recursive: true }).then(() => createFile(path, content)); + } + }); +} + +export async function genI18nRedirector(siteConfig: SiteConfig) { + const routes = siteConfig.pages + .filter((page) => page.startsWith(`${mainLocale}/`)) + .map((page) => page.replace(new RegExp(`^${mainLocale}\/`), '').replace(/\.md$/, '.html')); + + const promises = routes.map((route) => { + const localeNames = Object.keys(siteConfig.site.locales); + const routeForRender = route.replace(/index\.html$/, ''); + const linkAlternate = localeNames.map((name) => ``).join('\n '); + const fallbackLinks = localeNames.map((name) => `${siteConfig.site.locales[name].label}`).join(', '); + const content = ` + + + + + Redirecting... + ${linkAlternate} + + + + + + + + +`; + return createFile(`${siteConfig.outDir}/${route}`, content); + }); + + await Promise.allSettled(promises); + + console.log('I18n redirector generated'); +} diff --git a/.vitepress/theme/Layout.vue b/.vitepress/theme/Layout.vue index 23e32c6..9510424 100644 --- a/.vitepress/theme/Layout.vue +++ b/.vitepress/theme/Layout.vue @@ -1,36 +1,13 @@ - - \ No newline at end of file