@@ -262,38 +262,63 @@ export class TCVB {
262262 public static autoFixTCVBContent ( original : string ) : string {
263263 let content = original ;
264264
265- // Remove leading spaces before ## directives
265+ // 移除##指令前的空格
266266 content = content . replace ( / ^ ( \s + ) # # / gm, '##' ) ;
267-
268- // Close unclosed code blocks, inspired by the file-ending reference regex
269- content = content . replace ( / ( ` ` ` [ ^ \n ] * \n [ \s \S ] * ?) (? = ^ # # | \n # # E N D _ T C V B | (? ! [ \s \S ] ) ) / gm, '$1\n```' ) ;
270-
271- // Ensure END_TCVB is present
267+
268+ // 规则4/10:修复代码块包裹问题(精确处理需要代码块的操作)
269+ content = content . replace (
270+ / ( # # (?: O L D _ C O N T E N T | N E W _ C O N T E N T | O P E R A T I O N : C R E A T E ) \n ) ( [ \s \S ] * ?) (? = \n # # | \n # # E N D _ T C V B | $ ) / gis,
271+ ( match , directive , codeSection ) => {
272+ // 检测代码块是否被正确包裹
273+ const trimmed = codeSection . trim ( ) ;
274+ const hasStart = trimmed . startsWith ( '```' ) ;
275+ const hasEnd = trimmed . endsWith ( '```' ) ;
276+
277+ // 情况1:完全未包裹的代码块
278+ if ( ! hasStart && ! hasEnd ) {
279+ return `${ directive } \n\`\`\`\n${ codeSection . trim ( ) } \n\`\`\`` ;
280+ }
281+
282+ // 情况2:只有开始标记
283+ if ( hasStart && ! hasEnd ) {
284+ return `${ directive } \n${ codeSection } \n\`\`\`` ;
285+ }
286+
287+ // 情况3:只有结束标记(异常情况)
288+ if ( ! hasStart && hasEnd ) {
289+ return `${ directive } \n\`\`\`\n${ codeSection } ` ;
290+ }
291+
292+ return match ;
293+ }
294+ ) ;
295+
296+ // 确保存在END_TCVB标记
272297 if ( ! / ^ # # \s * E N D _ T C V B \s * $ / m. test ( content ) ) {
273- content += '\n## END_TCVB' ;
298+ content += '\n## END_TCVB' ;
274299 }
275-
276- // Fix GLOBAL-REPLACE operations
300+
301+ // 严格标准化END_TCVB标记(处理中间/前后空格)
302+ content = content . replace ( / ^ # # \s * E N D _ T C V B \s * $ / gm, '## END_TCVB' ) ;
303+
304+ // 修复GLOBAL-REPLACE操作
277305 content = content . replace (
278- / # # O P E R A T I O N : G L O B A L - R E P L A C E \n ( [ \s \S ] * ?) (? = \n # # O P E R A T I O N : | \n # # F I L E : | \n # # E N D _ T C V B | $ ) / g,
279- ( match ) => {
280- const hasOldContent = / # # O L D _ C O N T E N T / . test ( match ) ;
281- const hasNewContent = / # # N E W _ C O N T E N T / . test ( match ) ;
306+ / # # O P E R A T I O N : G L O B A L - R E P L A C E \n ( [ \s \S ] * ?) (? = \n # # O P E R A T I O N : | \n # # F I L E : | \n # # E N D _ T C V B | $ ) / g,
307+ ( match ) => {
308+ const hasOldContent = / # # O L D _ C O N T E N T / . test ( match ) ;
309+ const hasNewContent = / # # N E W _ C O N T E N T / . test ( match ) ;
282310
283- if ( ! hasOldContent && hasNewContent ) {
284- // Convert to CREATE if only NEW_CONTENT is present
285- const newContentMatch = match . match ( / # # N E W _ C O N T E N T \n ` ` ` ( [ \s \S ] * ? ) ` ` ` / ) ;
286- const newContentCode = newContentMatch ? newContentMatch [ 1 ] : '' ;
287- return `## OPERATION:CREATE\n\`\`\`\n ${ newContentCode } \n\`\`\`` ;
288- } else if ( hasOldContent && ! hasNewContent ) {
289- // Add empty NEW_CONTENT if only OLD_CONTENT is present
290- return match + '\n## NEW_CONTENT\n```\n```' ;
311+ if ( ! hasOldContent && hasNewContent ) {
312+ const newContentMatch = match . match ( / # # N E W _ C O N T E N T \n ` ` ` ( [ \s \S ] * ? ) ` ` ` / ) ;
313+ const newContentCode = newContentMatch ? newContentMatch [ 1 ] : '' ;
314+ return `## OPERATION:CREATE\n\`\`\`\n ${ newContentCode } \n\`\`\`` ;
315+ } else if ( hasOldContent && ! hasNewContent ) {
316+ return match + '\n## NEW_CONTENT\n```\n```' ;
317+ }
318+ return match ;
291319 }
292- // Return unchanged if both are present or both are missing (assuming minimal valid structure)
293- return match ;
294- }
295320 ) ;
296-
321+
297322 return content ;
298323 }
299324
0 commit comments