Repository navigation
fix(utils): correctly ignore tilde and longer fenced code blocks in writeMarkdownHeadingId #13108
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 |
|---|---|---|
|
|
@@ -118,6 +118,47 @@ function addHeadingId( | |
| return `${headingHashes}${headingContent} ${headingIdSuffix}`; | ||
| } | ||
|
|
||
| function parseOpeningCodeFence( | ||
| line: string, | ||
| ): {char: string; length: number} | null { | ||
| const trimmed = line.replace(/^ {0,3}/, ''); | ||
| const char = trimmed[0]; | ||
| if (char !== '`' && char !== '~') { | ||
| return null; | ||
| } | ||
| let length = 0; | ||
| while (length < trimmed.length && trimmed[length] === char) { | ||
| length += 1; | ||
| } | ||
| if (length < 3) { | ||
| return null; | ||
| } | ||
| const rest = trimmed.slice(length); | ||
| if (char === '`' && rest.includes('`')) { | ||
| return null; | ||
| } | ||
| return {char, length}; | ||
| } | ||
|
|
||
| function isClosingCodeFence( | ||
| line: string, | ||
| openFence: {char: string; length: number}, | ||
| ): boolean { | ||
| const trimmed = line.replace(/^ {0,3}/, ''); | ||
| if (!trimmed.startsWith(openFence.char)) { | ||
| return false; | ||
| } | ||
| let length = 0; | ||
| while (length < trimmed.length && trimmed[length] === openFence.char) { | ||
| length += 1; | ||
| } | ||
| if (length < openFence.length) { | ||
| return false; | ||
| } | ||
| const rest = trimmed.slice(length).trim(); | ||
|
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. Restrict closing-fence suffixes to spaces and tabs.
Check only spaces and tabs. Handle the trailing Based on learnings, closing fences must follow CommonMark suffix rules.
|
||
| return rest === ''; | ||
| } | ||
|
|
||
| export type WriteHeadingIDOptions = SluggerOptions & { | ||
| /** The target syntax to use for heading IDs. */ | ||
| syntax?: HeadingIdSyntax; | ||
|
|
@@ -166,23 +207,44 @@ export function writeMarkdownHeadingId( | |
| // If we can't overwrite existing slugs, make sure other headings don't | ||
| // generate colliding slugs by first marking these slugs as occupied | ||
| if (!overwrite) { | ||
| let openFence: {char: string; length: number} | null = null; | ||
| lines.forEach((line) => { | ||
| if (!openFence) { | ||
| const fence = parseOpeningCodeFence(line); | ||
| if (fence) { | ||
| openFence = fence; | ||
| return; | ||
| } | ||
| } else { | ||
| if (isClosingCodeFence(line, openFence)) { | ||
| openFence = null; | ||
| } | ||
| return; | ||
| } | ||
| const parsedHeading = parseHeadingIdAnySyntax(line); | ||
| if (parsedHeading.id) { | ||
| slugger.slug(parsedHeading.id); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| let inCode = false; | ||
| let openFence: {char: string; length: number} | null = null; | ||
| return lines | ||
| .map((line) => { | ||
| if (line.startsWith('```')) { | ||
| inCode = !inCode; | ||
| if (!openFence) { | ||
| const fence = parseOpeningCodeFence(line); | ||
| if (fence) { | ||
| openFence = fence; | ||
| return line; | ||
| } | ||
| } else { | ||
| if (isClosingCodeFence(line, openFence)) { | ||
| openFence = null; | ||
| } | ||
| return line; | ||
| } | ||
| // Ignore h1 headings, as we don't create anchor links for those | ||
| if (inCode || !line.startsWith('##')) { | ||
| if (!line.startsWith('##')) { | ||
| return line; | ||
| } | ||
| const parsedHeading = parseHeadingIdAnySyntax(line); | ||
|
|
||
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.
Preserve list-container boundaries when recognizing indented fences.
This valid Markdown input desynchronizes both scans:
The parser rejects the list-prefixed opener. It then accepts the indented closing fence as a new opener. The writer skips
## Real, treats the next top-level opener as a closer, and adds an ID to## Exampleinside its code block. The previous scanner preserved that example. (spec.commonmark.org)Use container-aware fence boundaries in both scans. Preserve support for indented top-level fences.