johansatge / obsidian-automatic-table-of-contents

💠 An Obsidian plugin to create a table of contents in a note, that updates itself when the note changes
MIT License
128 stars 12 forks source link

TOC renders as code block #6

Closed antistereov closed 10 months ago

antistereov commented 11 months ago

The headings are correct and display the links perfectly. I can't click on it though since it's embedded in a code block.

grafik

I've been waiting for a plugin like this for so long! Thank you so much for putting in the effort and time!

diamond-one commented 11 months ago

The same thing is happening to me. It was working this afternoon, after first install, but I updated, and now the contents are showing as a code block.

johansatge commented 11 months ago

Hey! Can you past here the markdown that is generating the output from your screenshot? (This part 👇)

CleanShot 2023-10-01 at 18 07 42

diamond-one commented 11 months ago

Hi Johan, I'm not the original poster, but I can provide ref of what's happening above.

By testing things out I think i have found the problem :

If heading 1 is not used, the contents page does not work and results in a code block:

image

Markdown:

Heading 2

heading 2

Heading 3


Below is a test showing on my system the contents pages working when including heading 1

image

Markdown:

Heading 1

heading 2

Heading 3

This solves my issue, but an unexpected quirk for users

Thanks (love the contents tree!)

johansatge commented 11 months ago

Thanks! Indeed it looks like the generated markdown is not valid if the document doesn't contain a first-level heading (or if the hierarchy is not "respect", in a more general way)

I'll need to come up with a strategy to handle this case gracefully

soweli-Luna commented 11 months ago

it seems to me like instead of using the header level to absolutely define the TOC level, it should detect the delta of the header level to inform if the TOC level should be incremented or decremented.. this way a level 1 header followed by a level 4 header will just be detected as a delta and inform you to step down one TOC level.. i think that should handle all cases gracefully, so long as you clamp the TOC level to never increment or decrement above 1 or below some minLevel

sergeevabc commented 11 months ago

Codeblock instead of usable TOC here too. Agggrrrhhh!

diamond-one commented 11 months ago

Codeblock instead of usable TOC here too. Agggrrrhhh!

Have you made sure you are using heading 1?

Heading one

sergeevabc commented 10 months ago

Have you made sure you are using heading 1?

I use H1 quite rarely, so no. For example, there is a page where I save various apps settings: H2 is used for each app name, H3 is used if there are too many settings (Firefox addons, Firefox themes). So far non-automatic TOC generator for Obsidian comes in handy for such a scenario.

jayv commented 10 months ago

I'll need to come up with a strategy to handle this case gracefully

  • Start from second-level heading if no first-level is found in the page
  • Introduce some minLevel option as suggested in minLevel #11
  • Other ideas are welcome!

@johansatge please don't hardcode or make it config, it will always be the wrong answer for someone or some use case. The easiest strategy is to scan the doc, find the min heading level and use that as the baseline for your indent... Confluence's TOC works the same way and just works.

Oh and forgot to say thank you for spending the time working on this!

northben commented 10 months ago

I'm in exactly the same situation. H1 fixes the problem but I almost never use H1. The plugin is really great and I'm looking forward to using it! Thank you so much.

jayv commented 10 months ago

I'm in exactly the same situation. H1 fixes the problem but I almost never use H1.

My current workaround is to put an empty # near the top of my page which renders the TOC with a double indent, but it's not the end of the world:

image

johansatge commented 10 months ago

Hey! Thx for the feedbacks everyone 🙏

The easiest strategy is to scan the doc, find the min heading level and use that as the baseline for your indent...

Sounds like the most solid approach indeed - implemented in 1.0.4. Please let me know if this version solved the issue.

jayv commented 10 months ago

Sounds like the most solid approach indeed - implemented in 1.0.4. Please let me know if this version solved the issue.

Thx, works great!