Skip to content
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

Merged
merged 1 commit into from Mar 5, 2022

Conversation

Josh-Cena
Copy link
Collaborator

Motivation

Complete a TODO. This probably doesn't happen a lot, but better fix the edge cases

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

The test case is updated

@Josh-Cena Josh-Cena added the pr: bug fix This PR fixes a bug in a past release. label Mar 5, 2022
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Mar 5, 2022
@@ -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(/^-+/, '')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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 slugger.slug function by first filtering out the illegal characters

Comment on lines +56 to 65
// 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);
}
});
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 😅

@netlify
Copy link

netlify bot commented Mar 5, 2022

✔️ [V2]

🔨 Explore the source changes: a69ce38

🔍 Inspect the deploy log: https://app.netlify.com/sites/docusaurus-2/deploys/6223295b1a436f0007084b41

😎 Browse the preview: https://deploy-preview-6849--docusaurus-2.netlify.app

@github-actions
Copy link

github-actions bot commented Mar 5, 2022

⚡️ Lighthouse report for the changes in this PR:

Category Score
🟠 Performance 60
🟢 Accessibility 100
🟢 Best practices 92
🟢 SEO 100
🟢 PWA 90

Lighthouse ran on https://deploy-preview-6849--docusaurus-2.netlify.app/

@github-actions
Copy link

github-actions bot commented Mar 5, 2022

Size Change: 0 B

Total Size: 791 kB

ℹ️ View Unchanged
Filename Size
website/.docusaurus/globalData.json 49.9 kB
website/build/assets/css/styles.********.css 105 kB
website/build/assets/js/main.********.js 597 kB
website/build/index.html 38.4 kB

compressed-size-action

@Josh-Cena Josh-Cena merged commit a756ddb into main Mar 5, 2022
@Josh-Cena Josh-Cena deleted the jc/fix-write-id branch March 5, 2022 09:25
Copy link
Collaborator

@slorber slorber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

const expected = `

# Ignored title

## abc {#abc}
## abc {#abc-1}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 🥇

Comment on lines +56 to 65
// 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);
}
});
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants