Restore the lost blank line before Markdown tables

Merged
JE
3mo ago
+106-4

Bug

Every top-level block gets the <p><br></p> separator when the Markdown put a blank line before it — except tables:

Intro.

| a | b |
|---|---|

rendered as <p>Intro.</p>\n<table>… — no separator, table jammed against the paragraph — while the separator after a table worked fine.

Why: goldmark's table extension builds the Table node inside a paragraph transformer that replaces the source paragraph without carrying HasBlankPreviousLines over, so the flag is always false on tables and the phase-2 TrixBreak insertion skips them. Pre-existing since #498; surfaced while verifying #637 (its "no bare top-level breaks" invariant test couldn't include a table without tripping over this).

Fix

Recover the answer from the source. Table.Pos() is the start of the replaced paragraph's first line, so the table was blank-line-separated exactly when the line above that position is blank (precedingLineIsBlank, CRLF-aware). The one case the false flag is right about — a table interrupting a paragraph mid-flight (Intro.\n| a | b |…), where the transformer leaves the leading lines behind as a paragraph — is detected by that leftover paragraph sharing the table's Pos, and stays attached. X.\n\nIntro.\n| a |… still separates before the paragraph only.

Tests

  • New TestMarkdownToHTMLTableSeparators: blank-separated (before+after), interrupting (attached), blank-before-interrupted-paragraph, consecutive tables, after-heading, first-block, CRLF.
  • TestMarkdownToHTMLEmitsNoBareTopLevelBreaks now includes a table block and <table> in its block list — the separator count invariant holds for all 8 block kinds.
  • The issue-#405 fixture encoded the flag-loss behavior; it now expects the same separator every other block gets.

bin/ci green locally.


Summary by cubic

Restores the blank-line <p><br></p> separator before Markdown tables. Previously, tables lost the leading separator and rendered flush with the prior block; now tables get the same spacing as other blocks, while tables that interrupt a paragraph remain attached.

  • Adds a hasBlankPreviousLines check that recovers table separation from the source text, including CRLF handling; preserves the interrupting-table case by detecting a preceding paragraph with the same Pos.
  • Expands tests to cover table separators and updates the issue-405 fixture; includes <table> in the “no bare top-level breaks” invariant.
  • Changes are limited to internal/richtext/richtext.go and its tests; behavior for non-table blocks is unchanged.