@@ -96,49 +96,47 @@ export class TitleSyntaxConverter {
9696 // 移除 return 关键字
9797 result = result . replace ( / ^ r e t u r n \s + / , '' ) ;
9898
99- // 按expression长度降序排序
100- const sortedMappings = [ ...mappings ] . sort (
101- ( a , b ) => b . expression . length - a . expression . length
102- ) ;
103-
104- // 将 expression 替换为 ${label}
105- for ( const mapping of sortedMappings ) {
106- const escapedExpr = this . escapeRegex ( mapping . expression ) ;
107-
108- // 处理各种模式
109- // 1. "text" + expression + "text" (最常见)
110- result = result . replace (
111- new RegExp ( `"([^"]*)"\\s*\\+\\s*${ escapedExpr } \\s*\\+\\s*"([^"]*)"` , 'g' ) ,
112- ( match , before , after ) => {
113- return `${ before } \${${ mapping . label } }${ after } ` ;
114- }
115- ) ;
116-
117- // 2. expression + "text"
118- result = result . replace (
119- new RegExp ( `^${ escapedExpr } \\s*\\+\\s*"([^"]*)"$` , 'g' ) ,
120- ( _ , after ) => `\${${ mapping . label } }${ after } `
121- ) ;
122-
123- // 3. "text" + expression
124- result = result . replace (
125- new RegExp ( `^"([^"]*)"\\s*\\+\\s*${ escapedExpr } $` , 'g' ) ,
126- ( _ , before ) => `${ before } \${${ mapping . label } }`
127- ) ;
128-
129- // 4. 单独的 expression
130- result = result . replace (
131- new RegExp ( `^${ escapedExpr } $` , 'g' ) ,
132- `\${${ mapping . label } }`
133- ) ;
134-
135- // 5. " + expression + " (空字符串)
136- result = result . replace (
137- new RegExp ( `"\\s*\\+\\s*${ escapedExpr } \\s*\\+\\s*"` , 'g' ) ,
138- `\${${ mapping . label } }`
139- ) ;
99+ // 创建表达式到标签的映射
100+ const exprToLabel = new Map < string , string > ( ) ;
101+ for ( const mapping of mappings ) {
102+ exprToLabel . set ( mapping . expression , mapping . label ) ;
140103 }
141104
105+ // 按expression长度降序排序(优先匹配长表达式)
106+ const sortedExprs = Array . from ( exprToLabel . keys ( ) ) . sort ( ( a , b ) => b . length - a . length ) ;
107+
108+ // 使用占位符方法处理表达式替换
109+ let placeholders = 0 ;
110+ const placeholderMap = new Map < string , string > ( ) ;
111+
112+ // 首先替换所有表达式为占位符
113+ for ( const expr of sortedExprs ) {
114+ const escaped = this . escapeRegex ( expr ) ;
115+ const placeholder = `___EXPR_PLACEHOLDER_${ placeholders } ___` ;
116+
117+ result = result . replace ( new RegExp ( escaped , 'g' ) , placeholder ) ;
118+ placeholderMap . set ( placeholder , exprToLabel . get ( expr ) || expr ) ;
119+ placeholders ++ ;
120+ }
121+
122+ // 现在处理字符串拼接,移除引号和 +
123+ // 移除所有 " + " (带空格的字符串拼接)
124+ result = result . replace ( / \s * \+ \s * / g, '' ) ;
125+
126+ // 移除引号,但保留占位符
127+ result = result . replace ( / ^ " ( [ ^ " ] * ) " $ / g, '$1' ) ; // 单独的字符串
128+ result = result . replace ( / ^ " ( [ ^ " ] * ) " ( .* ) " $ / g, '$1$2' ) ; // 开头的引号
129+ result = result . replace ( / ^ ( .* ?) " ( [ ^ " ] * ) " $ / g, '$1$2' ) ; // 结尾的引号
130+
131+ // 还有一些情况需要处理,让我们用更简单的方法
132+ // 移除所有剩余的引号
133+ result = result . replace ( / " / g, '' ) ;
134+
135+ // 还原占位符为 ${label}
136+ placeholderMap . forEach ( ( label , placeholder ) => {
137+ result = result . replace ( new RegExp ( this . escapeRegex ( placeholder ) , 'g' ) , `\${${ label } }` ) ;
138+ } ) ;
139+
142140 return result ;
143141 } catch ( e ) {
144142 console . error ( 'Failed to parse label expression:' , e ) ;
0 commit comments