@@ -135,6 +135,34 @@ function replacePrettierIgnore(md: string) {
135135 return md . replace ( / < ! - - \s * p r e t t i e r - i g n o r e \s * - - > \s * \n ? / g, '' ) ;
136136}
137137
138+ /**
139+ * 为中文原始 Markdown 中的站内链接补充语言后缀。
140+ * 网页中的链接由 LocaleLink 处理,原始 Markdown 则需要在构建时显式指定中文路由。
141+ */
142+ function replaceLocaleLinks ( md : string , context : ContentFilterContext ) {
143+ if ( detectDocLocale ( context . file ) !== 'zh-CN' ) {
144+ return md ;
145+ }
146+
147+ return md . replace ( / (?< ! ! ) ( \[ [ ^ \] \n ] + \] \( \s * ) ( \/ [ ^ ) \s ] * ) / g, ( link , prefix , url ) => {
148+ const suffixIndex = url . search ( / [ ? # ] / ) ;
149+ const pathname = suffixIndex === - 1 ? url : url . slice ( 0 , suffixIndex ) ;
150+ const suffix = suffixIndex === - 1 ? '' : url . slice ( suffixIndex ) ;
151+ const trailingSlash = pathname . endsWith ( '/' ) ;
152+ const basePath = trailingSlash ? pathname . slice ( 0 , - 1 ) : pathname ;
153+
154+ if ( ! basePath ) {
155+ return `${ prefix } /index-cn${ suffix } ` ;
156+ }
157+
158+ if ( basePath . endsWith ( '-cn' ) ) {
159+ return link ;
160+ }
161+
162+ return `${ prefix } ${ basePath } -cn${ trailingSlash ? '/' : '' } ${ suffix } ` ;
163+ } ) ;
164+ }
165+
138166/**
139167 * 替换 markdown 中的 "Semantic DOM" 部分的 code 标签为指向生成的 semantic.md 文件的链接
140168 *
@@ -570,6 +598,9 @@ function emitRawMd(api: IApi) {
570598 content = replaceCodeSrcToMarkdown ( content , file , PLUGIN_OPTIONS . codeAppend ) ;
571599 }
572600
601+ // 5. 为中文原始 Markdown 中的站内链接补充语言后缀
602+ content = replaceLocaleLinks ( content , filterContext ) ;
603+
573604 const outMd = path . join ( outRoot , `${ relPath } .md` ) ;
574605 fs . mkdirSync ( path . dirname ( outMd ) , { recursive : true } ) ;
575606 fs . writeFileSync ( outMd , content , 'utf-8' ) ;
0 commit comments