1+ // @ts -expect-error Node
12import fs from 'fs' ;
23import { mainLocale , baseUrl } from '../config' ;
34import type { SiteConfig } from 'vitepress' ;
45
6+ type RouteMeta = {
7+ title : string ;
8+ description ?: string ;
9+ } ;
10+
511async function createFile ( path : string , content : string ) {
612 const dir = path . replace ( / \/ [ ^ / ] + $ / , '' ) ;
713 await fs . promises . writeFile ( path , content ) . catch ( ( err ) => {
@@ -11,36 +17,61 @@ async function createFile(path: string, content: string) {
1117 } ) ;
1218}
1319
14- export async function genI18nRedirector ( siteConfig : SiteConfig ) {
15- const routes = siteConfig . pages
16- . filter ( ( page ) => page . startsWith ( `${ mainLocale } /` ) )
17- . map ( ( page ) => page . replace ( new RegExp ( `^${ mainLocale } \/` ) , '' ) . replace ( / \. m d $ / , '.html' ) ) ;
20+ export function createGenI18nRedirector ( ) {
21+ const routeMeta = new Map < string , RouteMeta > ( ) ;
22+
23+ function registerRouteMeta ( route : string , meta : RouteMeta ) {
24+ routeMeta . set ( route , meta ) ;
25+ }
26+
27+ async function genI18nRedirector ( siteConfig : SiteConfig ) {
28+ const genStartedAt = performance . now ( ) ;
29+
30+ const routes = siteConfig . pages
31+ . filter ( ( page ) => page . startsWith ( `${ mainLocale } /` ) ) ;
32+
33+ const promises = routes . map ( async ( route ) => {
34+ const routePath = route . replace ( new RegExp ( `^${ mainLocale } \/` ) , '' ) ;
35+ const routePathForWrite = routePath . replace ( / \. m d $ / , '.html' ) ;
36+ const routePathForRender = routePath . replace ( / i n d e x \. ( m d | h t m l ) $ / , '' ) . replace ( / \. m d $ / , siteConfig . cleanUrls ? '' : '.html' ) ;
1837
19- const promises = routes . map ( ( route ) => {
20- const localeNames = Object . keys ( siteConfig . site . locales ) ;
21- const routeForRender = route . replace ( / i n d e x \. h t m l $ / , '' ) ;
22- const linkAlternate = localeNames . map ( ( name ) => `<link rel="alternate" hreflang="${ siteConfig . site . locales [ name ] . lang || name } " href="${ baseUrl } /${ name } /${ routeForRender } ">` ) . join ( '\n ' ) ;
23- const fallbackLinks = localeNames . map ( ( name ) => `<a href="${ baseUrl } /${ name } /${ routeForRender } ">${ siteConfig . site . locales [ name ] . label } </a>` ) . join ( ', ' ) ;
24- const content = `<!DOCTYPE html>
25- <html lang="${ siteConfig . site . locales [ mainLocale ] . lang || 'ja-JP' } ">
38+ let title = 'Redirecting...' ;
39+ let description : string | null = null ;
40+ if ( routeMeta . has ( `${ mainLocale } /${ routePathForRender } ` ) ) {
41+ title = routeMeta . get ( `${ mainLocale } /${ routePathForRender } ` ) ?. title ?? title ;
42+ description = routeMeta . get ( `${ mainLocale } /${ routePathForRender } ` ) ?. description ?? null ;
43+ }
44+
45+ const localeNames = Object . keys ( siteConfig . site . locales ) ;
46+ const linkAlternate = localeNames . map ( ( name ) => `<link rel="alternate" hreflang="${ siteConfig . site . locales [ name ] . lang || name } " href="${ baseUrl } /${ name } /${ routePathForRender } ">` ) . join ( '\n ' ) ;
47+ const fallbackLinks = localeNames . map ( ( name ) => `<a href="${ baseUrl } /${ name } /${ routePathForRender } ">${ siteConfig . site . locales [ name ] . label } </a>` ) . join ( ', ' ) ;
48+ const content = `<!DOCTYPE html>
49+ <html>
2650<head>
2751 <meta charset="UTF-8">
2852 <meta name="viewport" content="width=device-width, initial-scale=1.0">
29- <title>Redirecting... </title>
53+ <title>${ title } </title>${ description ? `\n <meta name="description" content=" ${ description } ">\n` : '' }
3054 ${ linkAlternate }
31- <link rel="alternate" hreflang="x-default" href="${ baseUrl } /${ mainLocale } /${ routeForRender } ">
32- <link rel="canonical" href="${ baseUrl } /${ mainLocale } /${ routeForRender } ">
55+ <link rel="alternate" hreflang="x-default" href="${ baseUrl } /${ mainLocale } /${ routePathForRender } ">
56+ <link rel="canonical" href="${ baseUrl } /${ mainLocale } /${ routePathForRender } ">
3357 <script type="text/javascript">const s = ${ JSON . stringify ( localeNames ) } ; const d = localStorage.getItem('ais:locale'); if (d) { location.replace('/' + d + location.pathname + location.search + location.hash); } else if (s.includes(navigator.language.split("-")[0])) { location.replace('/' + navigator.language.split("-")[0] + location.pathname + location.search + location.hash); } else { location.replace('/ja' + location.pathname + location.search + location.hash); }</script>
3458</head>
3559<body>
3660 <noscript>${ fallbackLinks } </noscript>
3761</body>
3862</html>
3963` ;
40- return createFile ( `${ siteConfig . outDir } /${ route } ` , content ) ;
41- } ) ;
42-
43- await Promise . allSettled ( promises ) ;
64+ await createFile ( `${ siteConfig . outDir } /${ routePathForWrite } ` , content ) ;
65+ } ) ;
66+
67+ await Promise . allSettled ( promises ) ;
68+
69+ const genFinishedAt = performance . now ( ) ;
70+ console . log ( `I18n redirector generated in ${ Math . round ( ( genFinishedAt - genStartedAt ) / 10 ) / 100 } s` ) ;
71+ }
4472
45- console . log ( 'I18n redirector generated' ) ;
73+ return {
74+ registerRouteMeta,
75+ genI18nRedirector,
76+ } ;
4677}
0 commit comments