Skip to content

Commit 107d182

Browse files
committed
fix: fix prefix not function properly
1 parent f7923b1 commit 107d182

4 files changed

Lines changed: 12 additions & 15 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
.idea/
22

33
__pycache__/
4+
/test_env/generated/*
5+
!/test_env/generated/.gitkeep
46

57
package-lock.json
68
node_modules/

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ Fork me on [GitHub](https://github.com/pynickle/python-cheatsheet-redefined).
199199
... """
200200
>>> text1_lines = text1.splitlines()
201201
>>> text2_lines = text2.splitlines()
202-
>>> with open("HtmlDiff.html", "w", encoding="utf-8") as f: # make it a html file
203-
... HtmlDiff = d.make_file(text1_lines, text2_lines)
202+
>>> with open("generated/HtmlDiff.html", "w", encoding="utf-8") as f:
203+
... HtmlDiff = d.make_file(text1_lines, text2_lines) # make it a html file
204204
... f.write(HtmlDiff)
205205
...
206206
3331

test_env/generated/.gitkeep

Whitespace-only changes.

web/src/main.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)