diff --git a/src/converters/html-to-markdown.ts b/src/converters/html-to-markdown.ts index 61e1d9a..7d919b5 100644 --- a/src/converters/html-to-markdown.ts +++ b/src/converters/html-to-markdown.ts @@ -194,7 +194,10 @@ function createTurndownService(): TurndownService { const text = content.replace(/[\r\n]+/g, " ").replace(/\|/g, "\\|").trim(); const index = directCellIndex(node); const prefix = index === 0 ? "| " : " "; - return prefix + text + " |"; + const rawSpan = parseInt((node as HTMLElement).getAttribute?.("colspan") ?? "1", 10); + const span = Number.isFinite(rawSpan) && rawSpan > 1 ? rawSpan : 1; + const extra = span > 1 ? " |".repeat(span - 1) : ""; + return prefix + text + " |" + extra; }, }); diff --git a/tests/converters/html-to-markdown.test.ts b/tests/converters/html-to-markdown.test.ts index e4865f4..48f57ab 100644 --- a/tests/converters/html-to-markdown.test.ts +++ b/tests/converters/html-to-markdown.test.ts @@ -288,7 +288,7 @@ describe("htmlToMarkdown", () => { expect(result).toContain("| Magic Number | 17 |"); }); - it("separator column count respects colspan on first-row cells", () => { + it("colspan cells emit extra empty columns to keep table width consistent", () => { const html = ` @@ -296,7 +296,7 @@ describe("htmlToMarkdown", () => {
Header
`; const result = htmlToMarkdown(html); - expect(result).toContain("| Header |"); + expect(result).toContain("| Header | |"); expect(result).toContain("| --- | --- |"); expect(result).toContain("| Left | Right |"); }); @@ -351,7 +351,7 @@ describe("htmlToMarkdown", () => { `; const result = htmlToMarkdown(html); - expect(result).toContain("| someuser |"); + expect(result).toContain("| someuser | |"); expect(result).toContain("| --- | --- |"); expect(result).toContain("| Magic Number | 17 |"); expect(result).toContain("| Job | Celebrity Nobody |");