Repository navigation
fix(utils): escape every heading id token on a line #12315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,9 +73,9 @@ export function escapeMarkdownHeadingIds(content: string): string { | |
| return content.replaceAll(markdownHeadingRegexp, (substring) => | ||
| // TODO probably not the most efficient impl... | ||
| substring | ||
| .replace('{#', '\\{#') | ||
| .replaceAll('{#', '\\{#') | ||
| // prevent duplicate escaping | ||
| .replace('\\\\{#', '\\{#'), | ||
| .replaceAll('\\\\{#', '\\{#'), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Escape
In CommonMark, Replace both calls with one replacement for each
|
||
| ); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep heading-like lines inside fenced code blocks unchanged.
escapeMarkdownHeadingIdsrewrites every{#marker on any line that starts with#, including lines inside fenced code blocks. The regex does not track fence state. For a fenced line such as# \{#first} {#second}, the newreplaceAlladds a backslash before{#second}. The code sample then shows and copies a changed line. The original code left this line unchanged, because it escaped only the first marker.Fence-unaware rewriting predates this change. The base code already modifies the first marker on a fenced line such as
# {#first}. This PR extends the same defect to later markers. Escaping every marker is still correct for heading lines outside code blocks.Track fenced code blocks while processing the content. Leave every line inside a fence unchanged, and keep the marker escaping for heading lines outside fences.
Keep
{#markers inside inline code unchanged.Heading lines can contain code spans. For example,
## Use `{#id}` syntax {#real-id}contains a code-span marker and a real heading ID. The newreplaceAllescapes every{#on the line, including the marker inside the code span. CommonMark does not process backslash escapes in code spans, so the rendered code text changes from{#id}to\{#id}.Before this change, only the first
{#on a heading line was escaped. A code-span marker that appeared first already had this problem. The change extends it to every marker on the line.Escaping must skip code spans on heading lines for all markers. Markers outside code spans, including the trailing heading ID, must still be escaped for MDX.