obsidian-plugin-dynamic-toc icon indicating copy to clipboard operation
obsidian-plugin-dynamic-toc copied to clipboard

incorrect linking to headings with same name

Open slsdunn opened this issue 3 years ago • 2 comments

first off, love the plugin

If I have (sub)headings with the same name eg

Heading A

Subheading a

Heading B

Subheading a

clicking on the TOC link for #B##a takes me to #A##a. This is an obsidian default linking thing where it just goes by heading name and has been brought up in the forum before (although I couldn't find it just now). The response was that best practice is to have unique header names and if you really need to you can link specifically to #B##a (their reasoning being that how far should you go up the path and links could get really long). But I was wondering if it could be an option for TOC links to contain the full heading info (ie #A##a and #B##a instead of just ##a) and thus would take you to the correct section?

slsdunn avatar Dec 16 '21 20:12 slsdunn

So the problem is how Obsidian renders heading links. In HTML usually every heading should have its own unique ID

<h2 id="something-unique">Heading</h2>

I did try to do this in the past to fix #18 and ran into some issues.

I think the solution is to generate a unique hash based on the header content + it's position in the tree. I've taken a step back from software development for a little while, I'll add this to my todo list. To dive a little deeper to see if it's possible.

Aidurber avatar Dec 17 '21 16:12 Aidurber

I've taken another look at this and I'm not sure if it can be done with the current Obsidian API, at least with the solution I have in mind:

  1. In the post processor assign a unique and deterministic hash to each heading based on level and text content. Add this as an ID to the heading element.
  2. When we generate the markdown ToC, deterministically generate the hash for a heading
  3. In the produced markdown, make the link point to the unique hash

The problem with this is that it would require an entire rewrite of this plugin. The Obsidian API does not provide the appropriate metadata required to get the information in the post-processor. I would need to build a custom parser.

What's frustrating is that the built in Obsidian Outline plugin links to the correct heading, but nothing is exposed in the API to support that. I wonder if I would need to rebuild this plugin by using the CodeMirror API instead.

Aidurber avatar Feb 01 '22 09:02 Aidurber