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

TOC links broken when there are special characters in headings fix #64

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sambrista
Copy link

When there are special characters in headings (. / $ + ( ) [ ] # @ & % =) the anchor in toc links are different from the headings ids, where they are deleted.

As a result, links to headings such as "1. Introduction" or "3.2. Male / Female connectors" don't work.

This fix removes these characters from the TOC link anchor.

When there are special characters in headings (. / $ + ( ) [ ] # @ & % =) the anchor in toc links are different from the headings ids, where they are deleted.

As a result, links to headings such as "1. Introduction" or "3.2. Male / Female connectors" don't work.

This fix removes these characters from the TOC link anchor.
@sambrista sambrista changed the title TOC links broken when special chars in headings fix TOC links broken when there are special characters in headings fix Nov 1, 2024
@cmaas
Copy link
Owner

cmaas commented Nov 4, 2024

@sambrista I'm not sure what you mean. The TOC does not generate ids for headings. It only generates ids for anchor links and does this with the same code as markdwn-it-anchor, see: https://github.com/valeriangalliat/markdown-it-anchor/blob/957d7129a6a84bfa3e22283b99ee94c2dbdc16de/index.js#L3

Your code strips special characters, but that doesn't help anything, because you also need to control how headings get their ids. The goal of markdown-it-table-of-contents is compatibility with markdown-it-anchors. Markdown-it-anchors generates ids that include special characters.

Maybe you're using a different plugin for generating heading ids?

Two solutions:

  • Provide a custom slugify function, if you heading ids differ from markdown-it-anchor.
  • Use markdown-it-attrs and provide each heading with your own custom id.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants