Skip to content

fix(muya): keep an inline raw HTML tag's id intact - #5416

Merged
Jocs merged 3 commits into
marktext:developfrom
Dev-next-gen:fix/raw-html-id-attribute
Sep 16, 2026
Merged

Jocs merged 3 commits into
marktext:developfrom
Dev-next-gen:fix/raw-html-id-attribute

Conversation

@Dev-next-gen

@Dev-next-gen Dev-next-gen commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #5419

Summary

I wrote <a id="v1.2"></a> in a paragraph and a [see below](#v1.2) link elsewhere, and clicking the link did nothing. The rendered anchor was <a id="v1" class="mu-inline-rule mu-raw-html 2">.

The inline raw HTML renderer (packages/muya/src/inlineRenderer/renderer/htmlTag.ts) appended the tag's id to the snabbdom selector, a.mu-inline-rule.mu-raw-html#<id>. That string goes through toHTML, and snabbdom-to-html's selector parser (parse-sel) ends an id at any character outside [A-Za-z0-9_:-]. So v1.2 became id v1 plus a 2 class, and ids with /, # or a space were cut the same way. SCROLL_TO_ANCHOR falls back to getElementById(anchorSlug) for non-heading targets, so it no longer found the element.

The fix sets the id through data.attrs, the same move #5285 made for footnote reference ids. snabbdom-to-html writes an attrs.id over the selector's id and escapes it, so the value comes out verbatim. The tag's classes still go through the selector, so nothing else changes.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added (or explain why not needed)
  • Manually tested on:

packages/muya/src/inlineRenderer/__tests__/rawHtmlId.spec.ts boots a Muya under jsdom and checks the rendered element. The cases are ids v1.2, api/usage, a#b and note 1 (each kept verbatim and found by getElementById), no stray class from the id, and an &quot; in an id that stays inside the attribute.

On develop at 5734f01, with only the test applied, all 6 cases fail (for example expected 'v1' to be 'v1.2'). With the fix they pass. The full muya suite gives 1791 passed and 1 failed. The failure is tableChessboard.spec.ts > is exported from the package entrypoint, which fails the same way without this change and passes when run alone. tsc --noEmit -p packages/muya/tsconfig.json and eslint on the two files are clean.

Notes for reviewers [optional]

Class names go through the same parser, so a class like w-1/2 is still truncated. I left that out on purpose: snabbdom-to-html's class module doesn't escape data.class keys, so moving classes there would need its own handling.


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.

The inline raw HTML renderer put the tag's id into the snabbdom selector
(`a.mu-inline-rule.mu-raw-html#<id>`). The selector parser used by
snabbdom-to-html ends an id at any character outside [A-Za-z0-9_:-], so
`<a id="v1.2">` rendered as `id="v1"` with a stray `2` class, and ids
with `/`, `#` or a space were truncated the same way. A `[link](#v1.2)`
then found nothing with getElementById and did not scroll.

Set the id through `attrs`, as the footnote reference now does (marktext#5285),
so it is written verbatim and escaped.
@Jocs

Jocs commented Sep 16, 2026

Copy link
Copy Markdown
Member

Please open bug issue before submit a FIX PR.

@Dev-next-gen

Copy link
Copy Markdown
Contributor Author

Fair point, a ticket gives the bug a place to live on its own. I opened #5419 with the bug report form and linked it from this PR's description with Fixes #5419.

Before filing it, I re-ran the regression test against the current develop (edd2428, which now includes #5404). Without the fix, all 6 cases fail, for example expected 'v1' to be 'v1.2'. With it, all 6 pass. The issue lists the truncated id for each case.

Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.

Jocs and others added 2 commits September 16, 2026 20:26
Reference marktext#5419 in the describe title instead, like the sibling specs.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Jocs
Jocs merged commit 2a0ad6c into marktext:develop Sep 16, 2026
6 checks passed
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.

[Bug] Inline raw HTML id is truncated at '.', '/', '#' or a space, so links to it do nothing

2 participants