33 * 支持根据语言代码加载对应的 Markdown 文件
44 */
55
6+ import { getAssetPath } from './pathUtils'
7+
68/**
79 * 加载多语言 Markdown 文件
810 * @param {string } basePath - 基础路径(不含语言后缀),如 '/content/pages/about-site'
911 * @param {string } language - 语言代码,如 'zh' 或 'en'
1012 * @param {string } fallbackLanguage - 后备语言代码,默认 'zh'
1113 * @returns {Promise<string|null> } Markdown 内容,如果加载失败返回 null
1214 */
13- export async function loadI18nMarkdown ( basePath , language , fallbackLanguage = 'zh' ) {
15+ export async function loadI18nMarkdown (
16+ basePath ,
17+ language ,
18+ fallbackLanguage = 'zh'
19+ ) {
1420 // 尝试加载指定语言的文件
1521 const paths = [
16- `${ basePath } .${ language } .md` , // 如: /content/pages/about-site.zh.md
17- `${ basePath } .md` , // 如: /content/pages/about-site.md (无语言后缀)
18- ] ;
22+ `${ basePath } .${ language } .md` , // 如: /content/pages/about-site.zh.md
23+ `${ basePath } .md` , // 如: /content/pages/about-site.md (无语言后缀)
24+ ]
1925
2026 // 如果指定语言不是后备语言,也尝试后备语言
2127 if ( language !== fallbackLanguage ) {
22- paths . push ( `${ basePath } .${ fallbackLanguage } .md` ) ;
28+ paths . push ( `${ basePath } .${ fallbackLanguage } .md` )
2329 }
2430
2531 for ( const path of paths ) {
2632 try {
27- const response = await fetch ( path ) ;
33+ // 使用 getAssetPath 自动适配部署路径
34+ const fullPath = getAssetPath ( path )
35+ const response = await fetch ( fullPath )
2836 if ( response . ok ) {
29- const content = await response . text ( ) ;
30- console . log ( `成功加载 Markdown: ${ path } ` ) ;
31- return content ;
37+ const content = await response . text ( )
38+ console . log ( `成功加载 Markdown: ${ path } ` )
39+ return content
3240 }
3341 } catch ( error ) {
34- console . warn ( `无法加载 ${ path } :` , error . message ) ;
42+ console . warn ( `无法加载 ${ path } :` , error . message )
3543 }
3644 }
3745
38- console . warn ( `所有路径都加载失败: ${ paths . join ( ', ' ) } ` ) ;
39- return null ;
46+ console . warn ( `所有路径都加载失败: ${ paths . join ( ', ' ) } ` )
47+ return null
4048}
4149
4250/**
@@ -46,18 +54,18 @@ export async function loadI18nMarkdown(basePath, language, fallbackLanguage = 'z
4654 * @returns {Promise<Object> } 语言代码到内容的映射
4755 */
4856export async function preloadI18nMarkdown ( basePath , languages = [ 'zh' , 'en' ] ) {
49- const result = { } ;
50-
57+ const result = { }
58+
5159 await Promise . all (
52- languages . map ( async ( lang ) => {
53- const content = await loadI18nMarkdown ( basePath , lang ) ;
60+ languages . map ( async lang => {
61+ const content = await loadI18nMarkdown ( basePath , lang )
5462 if ( content ) {
55- result [ lang ] = content ;
63+ result [ lang ] = content
5664 }
5765 } )
58- ) ;
59-
60- return result ;
66+ )
67+
68+ return result
6169}
6270
6371/**
@@ -67,8 +75,8 @@ export async function preloadI18nMarkdown(basePath, languages = ['zh', 'en']) {
6775 */
6876export function extractLanguageFromPath ( path ) {
6977 // 匹配 .zh.md, .en.md 等模式
70- const match = path . match ( / \. ( z h | e n ) \. m d $ / ) ;
71- return match ? match [ 1 ] : null ;
78+ const match = path . match ( / \. ( z h | e n ) \. m d $ / )
79+ return match ? match [ 1 ] : null
7280}
7381
7482/**
@@ -78,7 +86,7 @@ export function extractLanguageFromPath(path) {
7886 */
7987export function getBasePath ( path ) {
8088 // 移除语言后缀(如果存在)
81- return path . replace ( / \. ( z h | e n ) \. m d $ / , '' ) ;
89+ return path . replace ( / \. ( z h | e n ) \. m d $ / , '' )
8290}
8391
8492/**
@@ -88,60 +96,64 @@ export function getBasePath(path) {
8896 * 2. 如果没有当前语言版本,选择无语言后缀的文件(如 about.md)
8997 * 3. 如果都没有,使用后备语言版本
9098 * 4. 确保每个基础文件名只出现一次
91- *
99+ *
92100 * @param {Array } files - 文件列表,每个文件包含 path 属性
93101 * @param {string } currentLanguage - 当前语言代码
94102 * @param {string } fallbackLanguage - 后备语言代码
95103 * @returns {Array } 过滤后的文件列表
96104 */
97- export function filterMarkdownByLanguage ( files , currentLanguage = 'zh' , fallbackLanguage = 'zh' ) {
105+ export function filterMarkdownByLanguage (
106+ files ,
107+ currentLanguage = 'zh' ,
108+ fallbackLanguage = 'zh'
109+ ) {
98110 // 按基础路径分组
99- const fileGroups = new Map ( ) ;
100-
111+ const fileGroups = new Map ( )
112+
101113 files . forEach ( file => {
102- const basePath = getBasePath ( file . path ) ;
103- const lang = extractLanguageFromPath ( file . path ) ;
104-
114+ const basePath = getBasePath ( file . path )
115+ const lang = extractLanguageFromPath ( file . path )
116+
105117 if ( ! fileGroups . has ( basePath ) ) {
106- fileGroups . set ( basePath , [ ] ) ;
118+ fileGroups . set ( basePath , [ ] )
107119 }
108-
120+
109121 fileGroups . get ( basePath ) . push ( {
110122 ...file ,
111123 language : lang ,
112- basePath
113- } ) ;
114- } ) ;
115-
124+ basePath,
125+ } )
126+ } )
127+
116128 // 为每个基础路径选择最合适的版本
117- const result = [ ] ;
118-
129+ const result = [ ]
130+
119131 fileGroups . forEach ( ( group , basePath ) => {
120132 // 优先级:当前语言 > 无语言后缀 > 后备语言 > 其他
121- let selected = null ;
122-
133+ let selected = null
134+
123135 // 1. 尝试找到当前语言版本
124- selected = group . find ( f => f . language === currentLanguage ) ;
125-
136+ selected = group . find ( f => f . language === currentLanguage )
137+
126138 // 2. 如果没有,尝试找到无语言后缀版本
127139 if ( ! selected ) {
128- selected = group . find ( f => f . language === null ) ;
140+ selected = group . find ( f => f . language === null )
129141 }
130-
142+
131143 // 3. 如果没有,尝试找到后备语言版本
132144 if ( ! selected && currentLanguage !== fallbackLanguage ) {
133- selected = group . find ( f => f . language === fallbackLanguage ) ;
145+ selected = group . find ( f => f . language === fallbackLanguage )
134146 }
135-
147+
136148 // 4. 如果都没有,使用第一个可用的文件
137149 if ( ! selected && group . length > 0 ) {
138- selected = group [ 0 ] ;
150+ selected = group [ 0 ]
139151 }
140-
152+
141153 if ( selected ) {
142- result . push ( selected ) ;
154+ result . push ( selected )
143155 }
144- } ) ;
145-
146- return result ;
156+ } )
157+
158+ return result
147159}
0 commit comments