@@ -845,7 +845,7 @@ function escapeRegExp(s) {
845845 return s . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ;
846846}
847847
848- // ===[ NEW ]=== 安全拼接:确保块与块之间至少保留一个换行,避免跨块黏连
848+ // 安全拼接:确保块与块之间至少保留一个换行,避免跨块黏连
849849function joinChunksPreservingNewlines ( chunks ) {
850850 if ( ! Array . isArray ( chunks ) || chunks . length === 0 ) return '' ;
851851 let out = '' ;
@@ -1048,7 +1048,7 @@ async function translateDocumentChunks(chunks, targetLang, filePath) {
10481048 }
10491049 }
10501050
1051- // ===[ NEW ]=== 兜底: 整文行数与原文一致性检查(按当前文件内容)
1051+ // 整文行数与原文一致性检查(按当前文件内容)
10521052 try {
10531053 const originalTotalLines = ( await fs . readFile ( filePath , 'utf8' ) )
10541054 . toString ( )
@@ -1057,7 +1057,7 @@ async function translateDocumentChunks(chunks, targetLang, filePath) {
10571057 let finalTotalLines = finalContent . split ( '\n' ) . length ;
10581058 if ( finalTotalLines !== originalTotalLines ) {
10591059 console . warn ( `⚠️ 拼接后行数不一致: 原文 ${ originalTotalLines } , 译文 ${ finalTotalLines } 。尝试更保守的换行拼接。` ) ;
1060- // 极限兜底: 强制在块之间都插入换行(即使已有换行也不去掉)
1060+ // 强制在块之间都插入换行(即使已有换行也不去掉)
10611061 finalContent = translatedChunks . join ( '\n' ) ;
10621062 finalTotalLines = finalContent . split ( '\n' ) . length ;
10631063 console . log ( `🧾 兜底后行数: 译文 ${ finalTotalLines } ` ) ;
@@ -1085,10 +1085,9 @@ async function translateFile(filePath, targetLang) {
10851085 console . log ( `📝 翻译文件: ${ filePath } -> ${ targetLang } ` ) ;
10861086 translationStatus . total ++ ;
10871087
1088- - const content = await fs . readFile ( filePath , 'utf8' ) ;
1089- + let content = await fs . readFile ( filePath , 'utf8' ) ;
1090- + // 统一换行为 LF,避免 CR 残留导致围栏/解析异常
1091- + content = content . replace ( / \r \n / g, '\n' ) . replace ( / \r / g, '\n' ) ;
1088+ let content = await fs . readFile ( filePath , 'utf8' ) ;
1089+ // 统一换行为 LF,避免 CR 残留导致围栏/解析异常
1090+ content = content . replace ( / \r \n / g, '\n' ) . replace ( / \r / g, '\n' ) ;
10921091 console . log ( `🔍 文件大小: ${ content . length } 字符` ) ;
10931092
10941093 // Front Matter 跳过规则判断(仅 md/mdx 有意义)
0 commit comments