fix(utils): escape every heading id token on a line#12315
Open
spokodev wants to merge 1 commit into
Open
Conversation
escapeMarkdownHeadingIds used String.prototype.replace, which replaces
only the first occurrence, so a heading that contains more than one {#…}
token had only the first escaped. parseMarkdownHeadingId officially
supports multiple ids on a heading (e.g. `## heading {#id1} {#id2}`), so
the leftover unescaped {# defeats the function's purpose and makes MDX v2
fail to compile the heading (mdx1Compat.headingIds is on by default).
Use replaceAll so every {# on the line is escaped.
✅ [V2]Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
escapeMarkdownHeadingIdsescapes MDX-breaking{#…}tokens in headings, butuses
String.prototype.replace, which replaces only the first occurrence:So a heading containing more than one
{#…}token gets only the first escaped:Multiple ids on a heading is a supported input:
parseMarkdownHeadingIdhas anexplicit test for it (
## Some heading {#id1} {#id2}→ text## Some heading {#id1}, idid2), so the leading{#id1}stays as literal heading textalongside the real anchor. The leftover unescaped
{#then defeats thisfunction's sole purpose —
mdx1Compat.headingIdsis on by default, and MDX v2treats the unescaped
{as a JS expression → "Could not parse expression withacorn" build failure, exactly the error this function exists to prevent.
Fix
Use
replaceAllso every{#on the heading line is escaped.Test
Added a case asserting
## Some heading {#id1} {#id2}escapes both tokens.Fails on
main; passes with the fix. The existing 58 tests (including thealready-escaped idempotency case and the multi-heading realistic example) still
pass.