valeriangalliat / markdown-it-anchor

A markdown-it plugin that adds an `id` attribute to headings and optionally permalinks.
The Unlicense
291 stars 72 forks source link

Invalid header IDs #84

Closed bkrnetic closed 2 years ago

bkrnetic commented 3 years ago

Hello,

coming from Joplin as I was told it uses this library for anchor generation.

I am gonna copy the issue I've written there.

Invalid header ID according to usual markdown rules.

Environment

Joplin version: Joplin 1.7.10 (prod, darwin) Platform: macOS OS specifics: Big Sur

Steps to reproduce

  1. Copy this to your Joplin note (working version): Eg.
    
    # Table of contents

Beautiful header

First header.

Beautiful header

Second header.



## Describe what you expected to happen

When you click on _**First header**_ it should take you to the first header and when you click on the second, it should take you to the second header.

I figured out by myself that the second header ID is `#beautiful-header-2` and the first one's ID is `#beautiful-header` while `#beautiful-header-1` ID is omitted.

I use Joplin for writing development documentation and this inconsistency with mark down rules causes issues when copying the content to my documentation MD file in (GitLab) repository.

Is there a unified standard for this since using anchors in md is really often the case?

Let me know if I can help with anything else.

Best,
Bruno
valeriangalliat commented 3 years ago

Hey!

Sadly I'm not aware of a unified standard about anchors in markdown, it seems that every website/tool have somewhat different rules for "slugification" (e.g. converting "Beautiful header" into beautiful-header) and deduplication, for example GitHub would do beautiful-header then beautiful-header-1 but BitBucket would do markdown-header-beautiful-header then markdown-header-beautiful-header_1, and I'm not sure about GitLab.

That being said I made an update with version 6.0.0 that defaults the unique slug start index to 1 instead of 2, and I also added an option to configure it (uniqueSlugStartIndex), so you should definitely upgrade to version 6 or later to get the behavior that you want.

Let me know if that helps. Cheers!

bkrnetic commented 3 years ago

Thanks a lot for your response and provided solution!