-
-
Notifications
You must be signed in to change notification settings - Fork 8k
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
fix(cli): write-heading-id should not generate colliding slugs when not overwriting #6849
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 |
---|---|---|
|
@@ -38,77 +38,60 @@ function addHeadingId( | |
|
||
const headingText = line.slice(headingLevel).trimEnd(); | ||
const headingHashes = line.slice(0, headingLevel); | ||
const slug = slugger | ||
.slug(unwrapMarkdownLinks(headingText).trim(), {maintainCase}) | ||
.replace(/^-+/, '') | ||
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. This also has the possibility of generating colliding slugs, so it's removed. We would probably fix it inside the |
||
.replace(/-+$/, ''); | ||
const slug = slugger.slug(unwrapMarkdownLinks(headingText).trim(), { | ||
maintainCase, | ||
}); | ||
|
||
return `${headingHashes}${headingText} {#${slug}}`; | ||
} | ||
|
||
export function transformMarkdownHeadingLine( | ||
line: string, | ||
slugger: Slugger, | ||
export function transformMarkdownContent( | ||
content: string, | ||
options: Options = {maintainCase: false, overwrite: false}, | ||
): string { | ||
const {maintainCase = false, overwrite = false} = options; | ||
if (!line.startsWith('#')) { | ||
throw new Error(`Line is not a Markdown heading: ${line}.`); | ||
} | ||
|
||
const parsedHeading = parseMarkdownHeadingId(line); | ||
const lines = content.split('\n'); | ||
const slugger = createSlugger(); | ||
|
||
// Do not process if id is already there | ||
if (parsedHeading.id && !overwrite) { | ||
return line; | ||
// 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) { | ||
lines.forEach((line) => { | ||
const parsedHeading = parseMarkdownHeadingId(line); | ||
if (parsedHeading.id) { | ||
slugger.slug(parsedHeading.id); | ||
} | ||
}); | ||
} | ||
Comment on lines
+56
to
65
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. Most of this is refactor and removing excess layers of abstraction, and this double-pass slugification is the only fix I made 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. 👍 nice trick also wanted to use something similar unfortunately afaik the slugger does not offer an API to register forbidden slugs or something 😅 |
||
return addHeadingId(parsedHeading.text, slugger, maintainCase); | ||
} | ||
|
||
function transformMarkdownLine( | ||
line: string, | ||
slugger: Slugger, | ||
options?: Options, | ||
): string { | ||
// Ignore h1 headings on purpose, as we don't create anchor links for those | ||
if (line.startsWith('##')) { | ||
return transformMarkdownHeadingLine(line, slugger, options); | ||
} | ||
return line; | ||
} | ||
|
||
function transformMarkdownLines(lines: string[], options?: Options): string[] { | ||
let inCode = false; | ||
const slugger = createSlugger(); | ||
|
||
return lines.map((line) => { | ||
if (line.startsWith('```')) { | ||
inCode = !inCode; | ||
return line; | ||
} | ||
if (inCode) { | ||
return line; | ||
} | ||
return transformMarkdownLine(line, slugger, options); | ||
}); | ||
} | ||
|
||
export function transformMarkdownContent( | ||
content: string, | ||
options?: Options, | ||
): string { | ||
return transformMarkdownLines(content.split('\n'), options).join('\n'); | ||
return lines | ||
.map((line) => { | ||
if (line.startsWith('```')) { | ||
inCode = !inCode; | ||
return line; | ||
} | ||
// Ignore h1 headings, as we don't create anchor links for those | ||
if (inCode || !line.startsWith('##')) { | ||
return line; | ||
} | ||
const parsedHeading = parseMarkdownHeadingId(line); | ||
|
||
// Do not process if id is already there | ||
if (parsedHeading.id && !overwrite) { | ||
return line; | ||
} | ||
return addHeadingId(parsedHeading.text, slugger, maintainCase); | ||
}) | ||
.join('\n'); | ||
} | ||
|
||
async function transformMarkdownFile( | ||
filepath: string, | ||
options?: Options, | ||
): Promise<string | undefined> { | ||
const content = await fs.readFile(filepath, 'utf8'); | ||
const updatedContent = transformMarkdownLines( | ||
content.split('\n'), | ||
options, | ||
).join('\n'); | ||
const updatedContent = transformMarkdownContent(content, options); | ||
if (content !== updatedContent) { | ||
await fs.writeFile(filepath, updatedContent); | ||
return filepath; | ||
|
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.
👍 🥇