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

Add option to maintain case for generated heading ids #7946

Closed
2 tasks done
elylucas opened this issue Aug 11, 2022 · 4 comments · Fixed by #10064
Closed
2 tasks done

Add option to maintain case for generated heading ids #7946

elylucas opened this issue Aug 11, 2022 · 4 comments · Fixed by #10064
Labels
apprentice Issues that are good candidates to be handled by a Docusaurus apprentice / trainee domain: markdown Related to Markdown parsing or syntax feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.

Comments

@elylucas
Copy link

Have you read the Contributing Guidelines on issues?

Description

We are migrating from a custom docs implementation to Docusaurus and need to maintain the case consistency of our current headings in our docs for external links currently pointed at our docs.

We thought about using the custom header ids, but we have a lot and it adds a lot of cruft to the md files and will have to added to each new header moving forward to maintain consistency.

It would be nice to have an option specified in docusaurus.config.js that will pass in maintainCase to the github slugger to preserve the casing of the heading anchors.

Has this been requested on Canny?

https://docusaurus.io/feature-requests/p/add-option-to-keep-capitalization-in-generated-heading-ids (older one but pretty much same request)

Motivation

Its been requested before (link in the canny inpuyt), and the answer was the custom header ids. That feature is great for a handful of custom links, but it would be great to turn this option on and it would eliminate the need to have to specify a custom id on each of our headings.

API design

Have a config option named maintainHeaderCasing that will be specified in the config file. This option will need to be passed down and passed as an option to the slugger here:

https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-mdx-loader/src/remark/headings/index.ts#L39

Have you tried building it?

Yes, we've patched it locally for our current needs by hard coding maintainCasing in the options to true here:

https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-mdx-loader/src/remark/headings/index.ts#L39

I'd be willing to contribute a fully working solution passing in the config option as well. I'll just need to figure out how to access the config from here.

Self-service

  • I'd be willing to contribute this feature to Docusaurus myself.
@elylucas elylucas added feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future. status: needs triage This issue has not been triaged by maintainers labels Aug 11, 2022
@Josh-Cena Josh-Cena added domain: markdown Related to Markdown parsing or syntax and removed status: needs triage This issue has not been triaged by maintainers labels Aug 12, 2022
@Josh-Cena
Copy link
Collaborator

I think we can do it, as part of our Markdown infrastructure upgrade in v3. ref #5999

@slorber slorber added the apprentice Issues that are good candidates to be handled by a Docusaurus apprentice / trainee label Sep 25, 2023
@iAdramelk
Copy link
Contributor

What is the status of this issue? Need this feature too and ready to make a PR if you give me some guidance about where this options should be placed.

@slorber
Copy link
Collaborator

slorber commented Apr 18, 2024

It's already implemented for the docusaurus write-heading-ids CLI

https://docusaurus.io/docs/cli#docusaurus-write-heading-ids-sitedir

Now that we have siteConfig.markdown, we could also add an option to let you customize the generated anchors (when an explicit id is not provided)


I'm not sure what the public API should look like. Do we only want to allow maintaining case, or even give more freedom to the users to provide their own anchorText -> anchorId logic?

If we only want to maintain the case, I propose:

const siteConfig = {
  markdown: {
    anchors: {maintainCase: true}
  },
}

We could start by providing that option, and then see if there's a need for something more powerful (not requested yet). Doing site config breaking changes are not too painful.

The place to make the change is likely here:

https://github.com/facebook/docusaurus/blob/main/packages/docusaurus-mdx-loader/src/remark/headings/index.ts#L41

id = parsedHeading.id ?? slugs.slug(heading,{maintainCase: markdownConfig.anchors.maintainCase});

@iAdramelk
Copy link
Contributor

@slorber write-heading-ids is updating existing mdx files which is something. It can be run before build, but it is not very convenient. It's not the information I want to commit to the repo.

For my use case just adding maintainCase to the config is enough. I am actually just patching the exact line above right now to make it work as I want.

Do you want me to make PR with this option?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apprentice Issues that are good candidates to be handled by a Docusaurus apprentice / trainee domain: markdown Related to Markdown parsing or syntax feature This is not a bug or issue with Docusausus, per se. It is a feature request for the future.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants