From 426aa5af2065ad2e8cf9965369132a7bbcb4799f Mon Sep 17 00:00:00 2001 From: naive231 Date: Tue, 19 Nov 2024 14:51:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E9=A1=B9=E7=BC=96=E5=8F=B7=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E3=80=82=E4=B8=BB=E8=A6=81=E6=94=B9=E5=8A=A8=E5=8C=85?= =?UTF-8?q?=E6=8B=AC=EF=BC=9A=201.=20=E6=B7=BB=E5=8A=A0=E4=BA=86=20`topLev?= =?UTF-8?q?elCounter`=20=E6=9D=A5=E4=B8=93=E9=97=A8=E8=BF=BD=E8=B8=AA?= =?UTF-8?q?=E9=A1=B6=E7=BA=A7=E5=88=97=E8=A1=A8=E9=A1=B9=E7=9A=84=E8=AE=A1?= =?UTF-8?q?=E6=95=B0=E3=80=82=202.=20=E4=BF=AE=E6=94=B9=E4=BA=86=E7=BC=A9?= =?UTF-8?q?=E8=BF=9B=E7=BA=A7=E5=88=AB=E5=8F=98=E5=8C=96=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E8=AE=A1=E6=95=B0=E5=99=A8=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E3=80=82=203.=20=E6=94=B9=E8=BF=9B=E4=BA=86=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E5=99=A8=E6=95=B0=E7=BB=84=E7=9A=84=E7=BB=B4=E6=8A=A4=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 这些修改确保: - 顶级列表项正确从 1 递增到 2。 - 子列表项的编号正确跟随其父级列表项。 - 同级列表项保持正确的序号。 修复后: - "不管是對內的組織管理..." 会显示为 "1.2"。 - "以生成式人工智慧技術..." 会显示为 "2"。 - 所有子项的编号也会相应正确调整。 --- main.ts | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/main.ts b/main.ts index 8265984..c711e10 100644 --- a/main.ts +++ b/main.ts @@ -48,7 +48,7 @@ function processLineBlock(source: string): string { const outputLines = []; let prevIndentLevel = 0; let listCounters: number[] = []; - let previousListType = ''; + let topLevelCounter = 0; for (let line of lines) { // Determine indentation level @@ -111,31 +111,35 @@ function processLineBlock(source: string): string { } if (isListItem) { - // Handle indent level - if (indentLevel > prevIndentLevel) { - listCounters.push(0); - } else if (indentLevel < prevIndentLevel) { - while (listCounters.length > indentLevel) { - listCounters.pop(); - } - } - - // At this point, listCounters.length == indentLevel + 1 - - // Increment counter at current level - if (listCounters.length === 0) { - listCounters.push(1); + // 处理顶级列表项 + if (indentLevel === 0) { + topLevelCounter++; + listCounters = [topLevelCounter]; + prevIndentLevel = 0; } else { - listCounters[listCounters.length - 1]++; + // 处理子列表项 + // 确保 listCounters 数组长度与当前缩进级别匹配 + while (listCounters.length <= indentLevel) { + listCounters.push(0); + } + + // 如果缩进级别改变,调整计数器 + if (indentLevel !== prevIndentLevel) { + // 如果缩进更深,添加新的计数器 + if (indentLevel > prevIndentLevel) { + listCounters[indentLevel] = 0; + } else { + // 如果缩进更浅,截断数组 + listCounters = listCounters.slice(0, indentLevel + 1); + } + } + + // 增加当前级别的计数 + listCounters[indentLevel]++; } - // Reset counters for deeper levels - for (let i = listCounters.length; i < indentLevel + 1; i++) { - listCounters.push(1); - } - - // Generate numbering - let numbering = listCounters.join('.'); + // 生成编号 + let numbering = listCounters.slice(0, indentLevel + 1).join('.'); // Apply formatting to content content = applyFormatting(content); @@ -158,7 +162,6 @@ function processLineBlock(source: string): string { // Reset listCounters and prevIndentLevel if necessary listCounters = []; prevIndentLevel = 0; - previousListType = ''; // Apply formatting replacements to the line line = applyFormatting(line);