naokazuterada / MarkdownTOC

SublimeText3 plugin which generate a table of contents (TOC) in a markdown document.
https://packagecontrol.io/packages/MarkdownTOC
MIT License
301 stars 48 forks source link

[Feature request] Remove numbers of headers in link #112

Closed Kristinita closed 6 years ago

Kristinita commented 6 years ago

1. Request

It would be nice, if you add an option — remove numbered headers from anchor link.

It would be nice, if anchor to header not change, if different numbers (\n+\.) in begin of header.

2. Settings

For example, I have SashaExample.md file. It content:

# 1. First header

Any text

# 2. Sasha header

Any text

3. Steps to reproduce

I place my carriage in begin of the file → Ctrl+Shift+P (⌘⇧p for Mac) → MarkdownTOC: Insert TOC.

4. Actual behavior

<!-- MarkdownTOC -->

1. [1. First header](#1-first-header)
1. [2. Sasha header](#2-sasha-header)

<!-- /MarkdownTOC -->
# 1. First header
<a name="1-first-header"></a>

Any text

<a name="2-sasha-header"></a>
# 2. Sasha header

Any text

I share second header as http://SashaSite.com/SashaExample#2-sasha-header.

5. Expected behavior

<!-- MarkdownTOC -->

1. [1. First header](first-header)
1. [2. Sasha header](sasha-header)

<!-- /MarkdownTOC -->
# 1. First header
<a name="first-header"></a>

Any text

<a name="sasha-header"></a>
# 2. Sasha header

Any text

6. Justification

For example, I modify my SashaExample.md file:

# 1. First header

Any text

# 2. Second header

Any text

# 3. Sasha header

Any text

I save a file → I get result:

<!-- MarkdownTOC -->

1. [1. First header](#1-first-header)
1. [2. Second header](#2-second-header)
1. [3. Sasha header](#3-sasha-header)

<!-- /MarkdownTOC -->
# 1. First header
<a name="1-first-header"></a>

Any text

<a name="2-second-header"></a>
# 2. Second header

Any text

<a name="3-sasha-header"></a>
# 3. Sasha header

Any text

Now link http://SashaSite.com/SashaExample#2-sasha-header will be invalid, http://SashaSite.com/SashaExample#3-sasha-header will be correct. It would be nice, if link http://SashaSite.com/SashaExample#sasha-header will correct in both cases.

Thanks.

naokazuterada commented 6 years ago

Hi, @Kristinita How about to add this setting to your MarkdownTOC.sublime-settings?

{
  "id_replacements": {
    "" : ["1. ", "2. ", "3. "]
  }
}

this will provide following results

<!-- MarkdownTOC style="ordered" autoanchor="true" bracket="round" autolink="true" -->

1. [1. First header](#first-header)
1. [2. Second header](#second-header)
1. [3. Sasha header](#sasha-header)

<!-- /MarkdownTOC -->

<a name="first-header"></a>
# 1. First header

Any text

<a name="second-header"></a>
# 2. Second header

Any text

<a name="sasha-header"></a>
# 3. Sasha header

Any text

Currently id_replacements doesn't allow regex patterns, so it's hard to cover all numbers though. I'm working on it.

Would you agree with my idea?

Kristinita commented 6 years ago

@naokazuterada , thanks, nice idea!

It will works with headers, for example, # 1.4. Any text or # 4.14.7. Any text?

Thanks.

naokazuterada commented 6 years ago

@Kristinita It's possible if id_replacements supports regex. But I'm still working on it (as I mentioned). Anyway I will send message here when it has been done.

naokazuterada commented 6 years ago

Hi, @Kristinita

I finally released MarkdownTOC v3.0.0 today. (Please follow the Upgrade Guide for detail because it has Breaking changes) Now I think you can deal with your needs with [new feature] regular expressions in 'id_replacements'.

Add this settings into your User Settings(Packages/User/MarkdownTOC.sublime-settings).

{
  "id_replacements": [
    {
      "pattern": "^[0-9\\. ]+ ",
      "replacement": ""
    }
  ]
}

Then you will get following results.

<!-- MarkdownTOC autolink="true" style="ordered" autoanchor="true" -->

1. [1.4. Any text](#any-text)
1. [4.14.7. Any text](#any-text-1)

<!-- /MarkdownTOC -->

<a id="any-text"></a>
# 1.4. Any text

<a id="any-text-1"></a>
# 4.14.7. Any text