Skip to content

fix: drop empty links and unwrap semantic-only tags in clean flavors - #442

Merged
mysticmind merged 1 commit into
masterfrom
fix/empty-links-and-semantic-only-tags
Nov 27, 2026
Merged

mysticmind merged 1 commit into
masterfrom
fix/empty-links-and-semantic-only-tags

Conversation

@mysticmind

Copy link
Copy Markdown
Owner

Follow-up to #440 (fixed in #441), covering the two leftovers noted there.

Empty links

<p><a href="…"><div>One</div><div>Two</div></a> after</p> gives separate paragraphs because the HTML parser closes the <p> at the <div> and copies the link into each <div>, exactly as browsers do. That part is correct. The defect was the empty link the parser leaves in the closed <p>, written as [](href). Named anchors had the same problem: <a name="top"></a> and <a id="x"></a> became []().

In AnchorReader, for flavors that do not keep raw HTML:

  • An <a> without href keeps only its content.
  • A link with nothing visible inside (no text, image, code, raw HTML, math) is dropped; its whitespace and line breaks are kept.
  • A link around only an image is still a link.
Input Before After
<p><a href="x"><div>One</div><div>Two</div></a> after</p> [](x) + [One](x) + [Two](x) + after [One](x) + [Two](x) + after
<p>x <a href="x"></a> y</p> x [](x) y x y
<p>Top <a name="top"></a> here</p> Top []() here Top here

Semantic-only tags

time, data, bdi and bdo change nothing visible, but in the Default flavor they fell through to Tags.Unknown (PassThrough) and stayed as raw HTML, e.g. inside news-teaser links. In flavors that do not keep raw HTML they now keep only their text:

Input Before After
<a href="n"><h3>Dimorphos</h3><time datetime="…">vor 22 Minuten</time><div>heise online</div></a> [Dimorphos <time datetime="…">vor 22 Minuten</time> heise online](n) [Dimorphos vor 22 Minuten heise online](n)

Tags.PassThrough, Tags.Replacer and custom readers still take precedence. Tags with a visible effect (mark, u, small, kbd, q, …) are unchanged. docs/configuration.md notes the four tags under Tags.Unknown.

Unchanged

CommonMark, GitHub and MultiMarkdown still keep all of the above as raw HTML.

Behavior change

Both changes alter default output for HTML that relied on the old behavior, so they are worth a line in the release notes.

Tests

10 new cases in Issue440Tests, including CommonMark raw passthrough and an explicit Tags.PassThrough override. The full suite (535 tests) passes on net8.0, net9.0 and net10.0 with no existing expected output changed.

In flavors that do not keep raw HTML:

- An <a> without href (a named target) keeps only its content instead
  of becoming []().
- A link with nothing visible inside is dropped, keeping its whitespace.
  This removes the [](href) left behind when a <div> inside a link
  closes the surrounding <p>.
- time, data, bdi and bdo change nothing visible, so they keep only
  their text instead of passing through as unknown tags.

CommonMark, GitHub and MultiMarkdown still keep all of these as raw HTML.

Follow-up to #440
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant