vscode-markdown icon indicating copy to clipboard operation
vscode-markdown copied to clipboard

Allow Duplicate Headings/Subheadings

Open nepgituser opened this issue 3 years ago • 6 comments

There are quite a few threads about updating links to headers, which would really be nice. This is kind of related to them and yet not.

The Markdown that I have has multiple headings on it. Each heading can have a subheading which might be a duplicate of another subheading (sample below). This causes two issues. First, when creating links, it's not easy to tell which anchor to use. Second, when creating the subheading there is no way to know that a duplicate subheading already exists.

Markdown

# Excel
## Configure
# Outlook
## Configure

TOC

- [Excel](#excel)
  - [Configure](#configure)
- [Outlook](#outlook)
  - [Configure](#configure-1)

So, quite a while back I came up with a "hack" (sample below) that allowed headings to be "duplicates". However, with the latest update it appears to no longer be working.

Markdown

# Excel
## Configure<u>Excel</u>
# Outlook
## Configure<u>Outlook</u>

CSS (at top of Markdown file)

<style type="text/css">
h1 u, h2 u, h3 u, h4 u, h5 u, h6 u { display: none; }
</style>

TOC (pre-update)

- [Excel](#excel)
  - [Configure](#configureuexcelu)
- [Outlook](#outlook)
  - [Configure](#configureuoutlooku)

TOC (post-update)

- [Excel](#excel)
  - [ConfigureExcel](#configureexcel)
- [Outlook](#outlook)
  - [ConfigureOutlook](#configureoutlook)

Is there some way to accomplish this type of behavior post-update? Or should I be laying out the Markdown some other way? Currently, I have one Markdown file, as most items are related and I didn't want to split them into separate files. That would make searching the content more challenging.

Anyway, been using MAIO for a long time now and it works really well, so thank you!

nepgituser avatar Nov 23 '22 16:11 nepgituser

Thanks for the feedback.

We cannot have duplicate headings (in fact heading ids) as otherwise all those TOC links refer to the first heading. For the issue that your workaround no longer works in the new version, I think it is a bug. (similar issues #903, #1112)

yzhang-gh avatar Nov 23 '22 17:11 yzhang-gh

Thanks for the extra info and bug notification.

nepgituser avatar Nov 23 '22 17:11 nepgituser

Is it possible to split this problem from the larger #903 code change? That change caused this issue.

At its root, this issue is fundamentally different from that one. "Unsafe" characters were used because there is no supported way to have multiple headers with the same name. Currently, what happens is that each additional heading becomes "heading-#". This works but links could easily be broken if a heading is changes. More importantly, it is hard to know which link to add because they are the same name plus a number.

As a suggested workaround, is it possible to have duplicate headings append their direct parent heading? That is what the work around was doing. Or does that simply complicate other heading generation?

nepgituser avatar Sep 18 '23 18:09 nepgituser

Thanks for bring it up again. I will try to make it work as the older version did this weekend.

is it possible to have duplicate headings append their direct parent heading?

It may cause more problems, e.g., their parent headings can still be the same (although it is rare).

yzhang-gh avatar Sep 20 '23 14:09 yzhang-gh

I rechecked the code and realized you are right about "At its root, this issue is fundamentally different from that one (#903)."

it is hard to know which link to add because they are the same name plus a number. is it possible to have duplicate headings append their direct parent heading?

It is possible. But we will need a new option for it as GitHub simply use the current "name plus number" method if I remember correctly.

yzhang-gh avatar Sep 23 '23 11:09 yzhang-gh

In case anyone is interested and willing to help, we need to collect each heading's parent here

https://github.com/yzhang-gh/vscode-markdown/blob/28981030384e7d87df66a0060f10f192cacae4aa/src/toc.ts#L681-L689

and deal with duplicated headings here

https://github.com/yzhang-gh/vscode-markdown/blob/28981030384e7d87df66a0060f10f192cacae4aa/src/toc.ts#L665-L679

My gut feeling is there will be some corner cases, e.g. a heading doesn't have a parent, the appended parent heading is still duplicated etc.

yzhang-gh avatar Sep 23 '23 11:09 yzhang-gh