cmaas / markdown-it-table-of-contents

A table of contents plugin for Markdown-it
MIT License
99 stars 33 forks source link

Fixed header links to increment if duplicates found #52

Closed cpadilla closed 2 years ago

cpadilla commented 3 years ago

I ran across the issue of headers generating the same links if they have the same name.

Based on the way markdown generates heard links (based on this documentation I found):

The IDs are generated from the content of the header according to the following rules:

  1. All text is converted to lowercase.
  2. All non-word text (e.g., punctuation, HTML) is removed.
  3. All spaces are converted to hyphens.
  4. Two or more hyphens in a row are converted to one.
  5. If a header with the same ID has already been generated, a unique incrementing number is appended, starting at 1.

Seems to be accurate. Judging by the default slugify, it seems like only rules 1 and 3 are being used, but I haven't tested this.

I added logic to cover rule 5, although it appears to be the case that the number appended actually starts at 2.

I might additional test cases for duplicate header names if I have time tomorrow.

cpadilla commented 3 years ago

Finished committing changes. Fixed test file names from #51. Added test files and a new test for duplicate headings.

wachunga commented 3 years ago

@cpadilla Any plans to merge this? I'm running into the same issue