@@ -31,7 +31,7 @@ function removePythonPrefixes(code: string): string {
3131 }
3232
3333 // 检查是否包含前缀
34- const hasPythonPrefix = line . includes ( '>>>' ) || line . includes ( '>>>' ) || line . includes ( '...' ) ;
34+ const hasPythonPrefix = line . startsWith ( '>>>' ) || line . startsWith ( '>>>' ) || line . startsWith ( '...' ) ;
3535
3636 // 输出行:前面不含>>>或者...的行
3737 if ( ! hasPythonPrefix ) {
@@ -42,19 +42,15 @@ function removePythonPrefixes(code: string): string {
4242 let cleanedLine = line ;
4343
4444 // 不使用正则表达式处理前缀,保留前缀后的缩进
45- if ( line . includes ( '>>>' ) && ! line . includes ( '>>>' ) ) {
45+ if ( line . startsWith ( '>>>' ) && ! line . startsWith ( '>>>' ) ) {
4646 // 处理 >>> 前缀
47- const prefixIndex = line . indexOf ( '>>>' ) ;
48- // 从prefixIndex+3开始,获取前缀后的内容
49- cleanedLine = line . substring ( prefixIndex + 3 ) ;
50- } else if ( line . includes ( '>>>' ) ) {
47+ cleanedLine = line . substring ( 3 ) ;
48+ } else if ( line . startsWith ( '>>>' ) ) {
5149 // 处理HTML转义的 >>> 前缀
52- const prefixIndex = line . indexOf ( '>>>' ) ;
53- cleanedLine = line . substring ( prefixIndex + 10 ) ; // '>>>'长度为10
54- } else if ( line . includes ( '...' ) ) {
50+ cleanedLine = line . substring ( 10 ) ; // '>>>'长度为10
51+ } else if ( line . startsWith ( '...' ) ) {
5552 // 处理 ... 前缀
56- const prefixIndex = line . indexOf ( '...' ) ;
57- cleanedLine = line . substring ( prefixIndex + 3 ) ;
53+ cleanedLine = line . substring ( 3 ) ;
5854 }
5955
6056 // 去除行首可能存在的多余空格
@@ -189,9 +185,8 @@ function toggleCodePrefix(blockId: string): void {
189185 const currentText = codeBlock . textContent || '' ;
190186
191187 // 判断当前状态并切换(检查更多可能的前缀模式)
192- if ( currentText . includes ( ' >>>' ) || currentText . includes ( ' >>>' ) || currentText . includes ( '...' ) ) {
188+ if ( currentText . startsWith ( " >>>" ) || currentText . startsWith ( " >>>" ) ) {
193189 codeBlock . textContent = removePythonPrefixes ( originalCode ) ;
194- console . log ( removePythonPrefixes ( originalCode ) )
195190 } else {
196191 codeBlock . textContent = originalCode ;
197192 }
0 commit comments