yzhang-gh / vscode-markdown

Markdown All in One
https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one
MIT License
2.89k stars 323 forks source link

Prefix to section headers #1432

Open Yakyyaky opened 3 months ago

Yakyyaky commented 3 months ago

Proposal

I've been going through some of the issues that plague Azure Devops's wiki and the TOC. As an alternate solution to the issue with Azure devops treating # as task item. I was hoping that we can have prefixes to Add/Update section header function.

If there's already a solution to this, I'm happy to learn.

Example

Example doc with sectioning:

# 1. Introduction
# 2. Details
## 2.1. Details SubText1
## 2.2. Details SubText2

Current output as Github (default) slugs:

- [1. Introduction](#1-introduction)
- [2. Details](#2-details)
  - [2.1. Details SubText1](#21-details-subtext1)
  - [2.2. Details SubText2](#22-details-subtext2)

This is greats, unfortunately just doesn't render the links correct in azure devops due to the aforementioned # issue.

Current output AzureDevops slugs

- [1. Introduction](#%31%2E%2D%69%6E%74%72%6F%64%75%63%74%69%6F%6E)
- [2. Details](#%32%2E%2D%64%65%74%61%69%6C%73)
  - [2.1. Details SubText1](#%32%2E%31%2E%2D%64%65%74%61%69%6C%73%2D%73%75%62%74%65%78%74%31)
  - [2.2. Details SubText2](#%32%2E%32%2E%2D%64%65%74%61%69%6C%73%2D%73%75%62%74%65%78%74%32)

While this works, it is practically unreadable, especially when you have to reference the headings again or when you have to fix heading reference due to heading name changes.

Personally, I definitely prefer the github style in favour of maintaining readability. As a workaround/alternative solution. I would like to have section prefix. So when we generate the ADD/Update section headers. It will generate the following instead.

# Section 1. Introduction
# Section 2. Details
## Section 2.1. Details SubText1
## Section 2.2. Details SubText2

Which then generates the TOC like so

- [Section 1. Introduction](#section-1-introduction)
- [Section 2. Details](#section-2-details)
  - [Section 2.1. Details SubText1](#section-21-details-subtext1)
  - [Section 2.2. Details SubText2](#section-22-details-subtext2)

Outcome Benefits

For my specific use case, it provides the following advantage

Others may have usages for section header prefixes being auto generated. For example they may be writing requirements doc

# Requirement 1
## Requirement 1.1 fine note 1
## Requirement 1.2 fine note 2
### Requirement 1.2.1 exclusion

When cross referencing documents, it's nice to see these headings presented as such.

Background details

802

1383

Thank you

Thanks for reading my request and more importantly the very useful extension to help with markdown writing.

yzhang-gh commented 3 months ago

Thanks for the super detailed description. It can be solved by "custom section number format" (#1380) although I guess it won't get implemented in the near future 😂.

The code is simple

https://github.com/yzhang-gh/vscode-markdown/blob/895dfe3a8a78f41aba59063cecec82e8c29196ac/src/toc.ts#L151

so a workaround is to modify the $HOME/.vscode/extensions/yzhang.markdown-all-in-one-3.6.2/dist\node/main.js (searching e=>`${a[e+r-1]}.`)

- e=>`${a[e+r-1]}.`
+ e=>`Section ${a[e+r-1]}.`

Then VS Code may warn you that the extension file is corrupted but it should be okay. This change will persist until the next update of this extension (at least a couple of months later 😂).

Yakyyaky commented 3 months ago

Thanks for the fast response.

I've tried your suggest and got this.

# ABC 1. Introduction
# ABC 2. Help
## ABC 2.ABC 1. Sub Help

Thankfully, you pointed to the source code. So I was able to work out a solution that worked for me. The final replacement is I had was:

- const s=[...Array(n-r+1).keys()].map((e=>`${a[e+r-1]}.`)).join("")
+ const s='Section ' + [...Array(n-r+1).keys()].map((e=>`${a[e+r-1]}.`)).join("")

I've also made updates to remove the "Section " so that remove removeSectionNumbers from your tos.ts will work correctly as well. This was done by replacing the min js for removeSectionNumbers, that references https://github.com/yzhang-gh/vscode-markdown/blob/895dfe3a8a78f41aba59063cecec82e8c29196ac/src/toc.ts#L174-L176

In the min file it was something along the following.

- a.replace(/^(\s{0,3}#+ +)((?:\d{1,9}\.)* )?(.*)/,((e,t,n,r)=>`${t}${r}`)):a.replace(/^(\s{0,3})((?:\d{1,9}\.)* )?(.*)/,((e,t,n,r)=>`${t}${r}`));
+ a.replace(/^(\s{0,3}#+ +)Section ((?:\d{1,9}\.)* )?(.*)/,((e,t,n,r)=>`${t}${r}`)):a.replace(/^(\s{0,3})Section ((?:\d{1,9}\.)* )?(.*)/,((e,t,n,r)=>`${t}${r}`))

And this allowed me to add and remove headers with the "Section" prefix.

However, The Add/Update section header was cummulatively adding Section prefix, which meant I couldn't call it recursively. Since I was able to update removeSectionNumbers, it was adequate, as I just have to remove and add manually instead of relying on Add/Update section header.

I'm happy with waiting for the next release, and this workaround bridge what I need for the moment.

Please close the issue if you see fit. Thanks for all your help.

yzhang-gh commented 3 months ago

Thanks for the update.