Skip to content

FIX: Always escape setext heading underlines - #82

Merged
gschlager merged 1 commit into
mainfrom
escape-setext-underlines-always
Sep 3, 2026
Merged

FIX: Always escape setext heading underlines#82
gschlager merged 1 commit into
mainfrom
escape-setext-underlines-always

Conversation

@gschlager

@gschlager gschlager commented Sep 3, 2026

Copy link
Copy Markdown
Member

This replaces #81. Threading an after_line_break flag through RenderContext, the renderer and both escapers fixed only the <br> case. A plain newline after inline markup like [b]Body[/b]\n=== still cooked as a heading, so I fixed the root cause instead.

The escaper only sees one text fragment and assumed the first line never follows a paragraph line. The renderer joins fragments afterwards, so that is often wrong. Every other block construct is already escaped without looking at surrounding lines. Now =-only lines are too.

  • Removes the paragraph tracking (prev_was_paragraph, paragraph_line?, block_construct?, SETEXT_UNDERLINE_DASH), about 60 lines.
  • The - half of that guard was already dead: single dashes hit the bullet rule, -- the ndash rule, --- the thematic break rule.
  • I tested the over-escaped output in Discourse. \= renders as a literal = everywhere, so the only cost is \=\=\=\= in the raw Markdown.
  • Benchmarks are unchanged, mutant is at 100%.

Supersedes #81.

A text line that contains only `=` was escaped only when the escaper
saw a paragraph line directly in front of it in the same text node.
For the first line of a fragment it assumed there was no paragraph
line before it.

That assumption does not hold. The escaper works on one text fragment
at a time and the renderer joins the fragments afterwards, so a
fragment starting with `===` can end up right after a paragraph line:
`Text("Body") LineBreak Text("===")` renders as `Body\n===`, and so
does `[b]Body[/b]\n===`. Discourse cooks both as an `<h1>`.

Every other block construct is escaped without looking at the
surrounding lines. The setext rule was the only exception, so this
drops the paragraph tracking (`prev_was_paragraph`, `paragraph_line?`,
`block_construct?`) and escapes a `=`-only line in every position.

The `-` half of the guard was already dead code: a line with a single
dash is caught by the bullet list rule, two dashes by the ndash pair
in `escape_inline`, and three or more by the thematic break rule. The
`SETEXT_UNDERLINE_DASH` regex is gone with it.

The cost is cosmetic. I checked the over-escaped output in a Discourse
instance: `\=` renders as a literal `=` at paragraph start, after a
blank line, in list items and in blockquotes.
@gschlager
gschlager force-pushed the escape-setext-underlines-always branch from 040aa49 to 2e8758b Compare September 3, 2026 17:55
@gschlager
gschlager merged commit 14a6a30 into main Sep 3, 2026
8 checks passed
@gschlager
gschlager deleted the escape-setext-underlines-always branch September 3, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants