Skip to content

Commit 17e441a

Browse files
committed
fix(table-spans): sort and merge spans correctly when level-2 table precedes level-1
The two-pass scan in parseTableSpans appended level-1 spans first, then level-2 spans. addSpan assumed ascending order, so a level-2 span starting before an existing level-1 span was silently dropped by the overlap merge. Replace addSpan with a collect-then-merge approach: gather all candidates unsorted, sort by start, then merge overlapping ranges in a single pass.
1 parent d416409 commit 17e441a

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

‎src/markdown/table-spans.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,18 @@ describe("parseTableSpans", () => {
174174
expect(spans).toHaveLength(1);
175175
expect(spans[0]).toEqual({ start: 0, end: text.length });
176176
});
177+
178+
it("detects level-2 table before level-1 table (addSpan ordering bug)", () => {
179+
const level2 = ["| a | b |", "| c | d |"].join("\n");
180+
const gap = "\n\nSome text.\n\n";
181+
const level1 = ["| H1 | H2 |", "|----|----|", "| v1 | v2 |"].join("\n");
182+
const text = level2 + gap + level1;
183+
184+
const spans = spansOf(text);
185+
expect(spans).toHaveLength(2);
186+
expect(spans[0]?.start).toBe(0);
187+
expect(spans[0]?.end).toBe(level2.length);
188+
expect(spans[1]?.start).toBe(level2.length + gap.length);
189+
});
177190
});
178191
});

‎src/markdown/table-spans.ts‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,29 @@ function isInsideFence(offset: number, fenceSpans: { start: number; end: number
3434
return false;
3535
}
3636

37-
function addSpan(spans: TableSpan[], start: number, end: number) {
38-
if (spans.length > 0) {
39-
const last = spans[spans.length - 1];
40-
if (last && start <= last.end) {
41-
last.end = Math.max(last.end, end);
42-
return;
37+
function mergeSpans(candidates: TableSpan[]): TableSpan[] {
38+
if (candidates.length === 0) {
39+
return [];
40+
}
41+
const sorted = candidates.toSorted((a, b) => a.start - b.start);
42+
const merged: TableSpan[] = [{ start: sorted[0].start, end: sorted[0].end }];
43+
for (let i = 1; i < sorted.length; i++) {
44+
const last = merged[merged.length - 1];
45+
const cur = sorted[i];
46+
if (cur.start <= last.end) {
47+
last.end = Math.max(last.end, cur.end);
48+
} else {
49+
merged.push({ start: cur.start, end: cur.end });
4350
}
4451
}
45-
spans.push({ start, end });
52+
return merged;
4653
}
4754

4855
export function parseTableSpans(
4956
buffer: string,
5057
fenceSpans: { start: number; end: number }[] = [],
5158
): TableSpan[] {
52-
const spans: TableSpan[] = [];
59+
const candidates: TableSpan[] = [];
5360
const lines = buildLineIndex(buffer);
5461

5562
const firstLevelUsed = new Set<number>();
@@ -87,7 +94,7 @@ export function parseTableSpans(
8794
firstLevelUsed.add(k);
8895
}
8996

90-
addSpan(spans, lines[headerStart].start, lines[bodyEnd].end);
97+
candidates.push({ start: lines[headerStart].start, end: lines[bodyEnd].end });
9198
i = bodyEnd;
9299
}
93100

@@ -122,12 +129,12 @@ export function parseTableSpans(
122129
for (let k = i; k <= groupEnd; k++) {
123130
firstLevelUsed.add(k);
124131
}
125-
addSpan(spans, lines[i].start, lines[groupEnd].end);
132+
candidates.push({ start: lines[i].start, end: lines[groupEnd].end });
126133
i = groupEnd;
127134
}
128135
}
129136

130-
return spans;
137+
return mergeSpans(candidates);
131138
}
132139

133140
function buildLineIndex(buffer: string): Array<{ start: number; end: number; text: string }> {

0 commit comments

Comments
 (0)