Self-Upgrading In Document Table of Contents Plugin

It would be nice to have inbuilt, within Obsidian, a Table of Contents (TOC) tool that self-upgrades / automatically upgrades, such that changes in headings are captured and rendered.

It’s disappointing that Obsidian lacks such a basic tool.

Existing plugins for this aren’t getting the job done.

The Dynamic Table of Contents (DToC) plugin is no longer giving usable results (GitHub - Aidurber/obsidian-plugin-dynamic-toc: An Obsidian plugin for creating Tables of Contents that stay updated with your document).

I posted about this recently, to ask for a temporary workaround (here: Dynamic Table of Contents Buggy and No Longer Working Well).

I’m aware that DToC is now archived. And in its archived form, it’s not useful.

For instance, headings’ indentation and varied levels aren’t being reflected in the table of contents that it creates. As a result, when applicable, one can’t see which headings are others’ subheadings. DToC now shows all headings as being on the same level, when that’s not the case (I have Obsidian version 0.16.5, and it created this problem well before this version).

The older plugin that came before DToC isn’t dynamic and therefore cannot automatically upgrade its contents when changes are made --forcing users to waste time constantly recreating TOCs.

A plugin that (1) preserves and shows the different levels of headings within a document and (2) automatically upgrades any created TOC’s content when heading changes are made would not only be nice but crucial. MS Word even does this well.

8 Likes

Not the solution to your FR but might be interesting: There is a ‘Floating TOC’ plugin currently in code review. I installed it already using BRAT.

2 Likes

Thank you for mentioning it.

It still doesn’t address the fact that creating TOC’s isn’t an inbuilt feature.

And why is that a big problem?

Because (1) the ability to create TOC’s is a crucial and fundamental capacity (that’s available in MS Word and many classic word processors) and (2) many Obsidian plugins undergo neglect.

For instance, this plugin “had not been updated for a long time” and is available only through BRAT.

1 Like

This might be an obvious question, but have you turned on ‘Outline’ in core plugins?

Sidenote: Floating TOC is not out yet, but probably will be soon.

1 Like

Why not just use Dataview? It’s simple, functional, and automatically updates.

Create a toc.js file in the root of your Obsidian vault, with this contents:

// Set this to 1 if you want to include level 1 headers,
// or set it to 2 if you want to ignore level 1 headers
const startAtLevel = 2
const content = await dv.io.load(dv.current().file.path)
const toc = content.match(new RegExp(`^#{${startAtLevel},} \\S.*`, 'mg'))
  .map(heading => {
    const [_, level, text] = heading.match(/^(#+) (.+)$/)
    const link = dv.current().file.path + '#' + text
    return '\t'.repeat(level.length - startAtLevel) + `1. [[${link}|${text}]]`
  })
dv.header(2, 'Table of contents')
dv.paragraph(toc.join('\n'))

And then in any note which you wish to add a dynamic TOC, you just add this code:

```dataviewjs
dv.view('toc')
```

10 Likes

I understand what the OP is asking but I don’t see this as a core feature.
Renamed and Moved to plugin ideas.

2 Likes

I, too, would like to make use of a [[TOC]] block that automatically updates based on my document’s contents. I’ve grown accustomed to it in things like Azure DevOps Wikis. Also, unfortunately, GitHub - Aidurber/obsidian-plugin-dynamic-toc: An Obsidian plugin for creating Tables of Contents that stay updated with your document stopped working for me, as of 1.0.0. :sob:

Definitely going to give the Floating TOC a try, as well as Dataview.

Just wanted to add my comment here so that OP knows they are not alone…and I joined these forums to post that.

Also, Obsidian is changing my life. Thank you!

1 Like

I also agree that [TOC] rendering is a useful feature

3 Likes

i forgot to mention, there is also the “Quiet outline” plugin which renders a prettier TOC than the built in outline.

2 Likes

I’m +1 on this plugin Idea. I’d love to have a Table of Contents outline that is automatically updated in my document. It’s a useful feature in other note apps and I’m glad to see it being addressed here.

And, I agree this feature is perfect for a community or core plugin.

Update
I just found the core plugin Outline and it does what I was looking for. When turned on there is a document outline / Table of Contents based on headers in the left panel. If I click on the outline heading in the panel, the cursor jumps to that position in the main window document. Perfect.

I think the updating automatically is the issue, but I just close and reopen the doc to update the outline. Thanks

The core outline plugin should update in real time I thought.

If you want a TOC in the note itself, I just stumbled over this awesome Dataview solution:

2 Likes

Thanks for that , works nicely but i see some issues.

It only sees Header one and two. Header three is not showing.
Underlying header does not print previous header. So for example when i start a header 2 under header 1 i would expect something like 1.1 but it only prints 1

Can this be tweaked in your sample script?

Thanks!

1 Like

That’s strange since I don’t experience that in the Sandbox vault - so potentially there is an issue with some plugin you have installed?

This is just CSS related. You can add a snippet like this to change your numbering format:

ol {
  list-style-type: none;
  counter-reset: item;
  margin: 0;
  padding: 0;
  padding-inline-start: 10px !important;
}
li {
  display: table;
  counter-increment: item;
  padding-bottom:20px;
}
li:before {
  content: counters(item, ".") ". ";
  display: table-cell;
  padding-right: 0.6em;
  padding-top: 0.2em;
}
li li {
  margin: 0;
}
li li:before {
  content: counters(item, ".") " ";
}
2 Likes

The floating Toc plugin is currently working flawlessly for me here- and it even implement that in the way i like most wich is besides the main document (much like the outline panel), so one can quickly jump to other headers no matter where they are on the page.

1 Like

Thank for the css it now has the output as i like it. :grinning:

And the third header is also solved. Apparently when you have one space too many the header is not seen as a header :slight_smile: So i was have 2 Spaces in between the # and the header.

Looks great now. Thanks a lot!

Hello,

I notice the references in the toc refer to obsidian locations. Can i also refer just to the headers inside the document without refering to obsidian. I wan to be able to export the note to PDF and send this to someone that does not have my obsidian :slight_smile:

Thanks!

Johan

thank u :slight_smile:

Why doesn’t it show all headers addresses?

Thanks! This works very well, except the numbering formatting a bit weird.
I don’t know is this because theme style setting or what