修复了列表项编号错误的问题。主要改动包括:

1. 添加了 `topLevelCounter` 来专门追踪顶级列表项的计数。
2. 修改了缩进级别变化时的计数器处理逻辑。
3. 改进了计数器数组的维护方式。

这些修改确保:
- 顶级列表项正确从 1 递增到 2。
- 子列表项的编号正确跟随其父级列表项。
- 同级列表项保持正确的序号。

修复后:
- "不管是對內的組織管理..." 会显示为 "1.2"。
- "以生成式人工智慧技術..." 会显示为 "2"。
- 所有子项的编号也会相应正确调整。
This commit is contained in:
naive231 2024-11-19 14:51:58 +08:00
parent 146b26f8e4
commit 426aa5af20
1 changed files with 27 additions and 24 deletions

51
main.ts
View File

@ -48,7 +48,7 @@ function processLineBlock(source: string): string {
const outputLines = []; const outputLines = [];
let prevIndentLevel = 0; let prevIndentLevel = 0;
let listCounters: number[] = []; let listCounters: number[] = [];
let previousListType = ''; let topLevelCounter = 0;
for (let line of lines) { for (let line of lines) {
// Determine indentation level // Determine indentation level
@ -111,31 +111,35 @@ function processLineBlock(source: string): string {
} }
if (isListItem) { if (isListItem) {
// Handle indent level // 处理顶级列表项
if (indentLevel > prevIndentLevel) { if (indentLevel === 0) {
listCounters.push(0); topLevelCounter++;
} else if (indentLevel < prevIndentLevel) { listCounters = [topLevelCounter];
while (listCounters.length > indentLevel) { prevIndentLevel = 0;
listCounters.pop();
}
}
// At this point, listCounters.length == indentLevel + 1
// Increment counter at current level
if (listCounters.length === 0) {
listCounters.push(1);
} else { } 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++) { let numbering = listCounters.slice(0, indentLevel + 1).join('.');
listCounters.push(1);
}
// Generate numbering
let numbering = listCounters.join('.');
// Apply formatting to content // Apply formatting to content
content = applyFormatting(content); content = applyFormatting(content);
@ -158,7 +162,6 @@ function processLineBlock(source: string): string {
// Reset listCounters and prevIndentLevel if necessary // Reset listCounters and prevIndentLevel if necessary
listCounters = []; listCounters = [];
prevIndentLevel = 0; prevIndentLevel = 0;
previousListType = '';
// Apply formatting replacements to the line // Apply formatting replacements to the line
line = applyFormatting(line); line = applyFormatting(line);