Aidurber / obsidian-plugin-dynamic-toc

An Obsidian plugin for creating Tables of Contents that stay updated with your document
MIT License
269 stars 41 forks source link

Having a jumps in heading levels ruins the indentation #26

Closed Archie-2021 closed 2 years ago

Archie-2021 commented 2 years ago

For example. I like to embed the TOC and some dataview metadata stuff at the top of my notes in a lvl4 heading so that I can fold it out of the way. But if I do that all the indentation above the lvl 4 get removed.

Aidurber commented 2 years ago

Can you share with me an example + a screenshot if at all possible.

Archie-2021 commented 2 years ago

Can you share with me an example + a screenshot if at all possible.

Look how lvl1 heading shown at lvl2 becauae Toc on top of it starts at lvl2

Screenshot 2021-12-20 092311.png

Screenshot 2021-12-20 092246.png

Aidurber commented 2 years ago

Short Answer

You cannot have a level 2 heading first in the document and then have a level 1 heading.

Detailed Answer

I don't think this can be fixed. The first heading in a document should be the lowest (level 1). It's quite difficult to explain so hopefully images will help.

In this example, I moved toc to level 1, this fixed the indentation. CleanShot 2021-12-21 at 11 04 44@2x

The logic that is in play is:

  1. Find all headings in a document
  2. Get take the first heading
  3. Get the level of that heading, this sets the basis of the indentation (we need indentation to render markdown lists correctly)
  4. For each heading we subtract the first headings level found in step 3 this is because we cannot have a list that has a first item that is indented more than the subsequent items, it produces invalid markdown.

So in your example:

Removing the subtract logic produces invalid markdown which won't render

CleanShot 2021-12-21 at 11 06 59@2x

Recommendation

You are using headings a little unconventionally. Headings aren't for style, they provide lexical meaning to a document.

If your documents were structured like this, by having a top level 1 heading, that will fix your issue. With the logic that we're forced to apply by subtracting the first headings level in a document by a headings level:

# Page title
## toc
## lvl1
### lvl2

That will fix your issue. Alternatively, make toc a level 1 header

Archie-2021 commented 2 years ago

Short Answer

You cannot have a level 2 heading first in the document and then have a level 1 heading.

Detailed Answer

I don't think this can be fixed. The first heading in a document should be the lowest (level 1). It's quite difficult to explain so hopefully images will help.

In this example, I moved toc to level 1, this fixed the indentation. CleanShot 2021-12-21 at 11 04 44@2x

The logic that is in play is:

  1. Find all headings in a document
  2. Get take the first heading
  3. Get the level of that heading, this sets the basis of the indentation (we need indentation to render markdown lists correctly)
  4. For each heading we subtract the first headings level found in step 3 this is because we cannot have a list that has a first item that is indented more than the subsequent items, it produces invalid markdown.

So in your example:

  • toc = level 2

    • 2 - 2 = 0 (no indentation)
  • lvl1 = level 1

    • 1 - 2 = -1 (negative indentation isn't a think in markdown so it's 0)

Removing the subtract logic produces invalid markdown which won't render

CleanShot 2021-12-21 at 11 06 59@2x

Recommendation

You are using headings a little unconventionally. Headings aren't for style, they provide lexical meaning to a document.

If your documents were structured like this, by having a top level 1 heading, that will fix your issue. With the logic that we're forced to apply by subtracting the first headings level in a document by a headings level:

  • page title - level 1

    • 1 - 1 = 0 (no indentation)
  • toc - level 2

    • 2 - 1 = 1 (one level if indentation)
  • lvl1 - level 2

    • 2 - 1 = 1
# Page title
## toc
## lvl1
### lvl2

That will fix your issue. Alternatively, make toc a level 1 header

Thanks for your detailed explanation, I agree that my use of heading here is a bit unconventional but the thing is that I like folding and unfolding and separating document in manageable and referenceable chunks a lot and it is the only heading that I can do it in markdown. I will use a first level heading at the top for putting front matter out of the way after this. it is going to be a bit distracting and ugly but would do the job.