Type [toc] for a ToC

It would be useful to be able to put a table of contents with clickable links to the headers at the top of a document. This is esp. useful if the file is exported to a PDF.

The easy way to do that would be to be able to type [toc] and get a table of contents rendered in Preview.

57 Likes

I’d like to see this this as well, was chasing this ability earlier day. Found a partial solution, however, it’s still not quite perfect.

3 Likes

There is a Table of Content community plugin I just found that works well. Gives you a new shortcut to run that you can bind to a hotkey if you wish.

Only problem with it is that clicking the link opens a new pane centered on the section rather then just jumping the current pane down. I’m guessing this is a Obsidian API limitation, but not sure.

1 Like

@rob3r: thanks for pointing me to this plug-in. It works well, and the problem you mention in your 2nd paragraph does not occur for me: click on a jumps to the right header in the current pane.

The only issue I have is that the text “Table of Contents” is not inserted above the ToC. I have filed an issue report of the dev’s Github page.

2 Likes

This is quite important when exporting files, either in PDF or DOCX format. Quite valuable!

1 Like

It would be really useful functionality. Just as is the outline on the right pane to be imported into the markdown file.

@yisoh62178 : there is a Table of Contents plug-in the Community collection.

1 Like

Great, it could be added to core functionality then I assume.
Although it has a limitation: It doesn’t automatically update the generated ToC.

1 Like

@yisoh62178 : I don’t see why it should. In any case, that’s for the devs to decide.

To me the clickable ToC in PDF would be the value here, and with the plugin in does not work like that :frowning:

1 Like

@3dward that is what I stated in my OP. In fact, what would really, really useful in the PDF is a clickable ToC AND have those little return arrows to return from the header to the ToC.

1 Like

I miss this feature so much, especially to be able to copy paste some notes on platform like Github who supports ToC for quick browsing.

Hi everyone, I really miss this feature ([toc])! The mentioned plugin is not a solution as it doesn’t update automatically. Indoc Table of content is really useful. :pray:

The Dynamic Table of contents (link) does update automatically, but the plugin isnt maintained anymore.

1 Like

searching solutions around this. I was watching around query. but how to tell query search this file? I tried ., ./ and how to add a search like: /# (space) to detect headers and path: … in the same line?
the other topic around this was closed

/# /file:2022-10-07

seems to work but I have to enter the file name

I would also like this feature. It exists in Cryptpad’s Markdown editor. The Cryptpad code is open source, so perhaps it could be borrowed. Having an inline TOC is extremely useful.

1 Like

You can do this very easily with Dataview:

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')
```

4 Likes

Thanks for const content = await dv.io.load(dv.current().file.path)!

I was successfully querying the full vault but not the current file.
I use these with regex queries as reminders to do stuff on content before publishing them. Good thing it is dynamic (meaning the dragonslayer in me sees the monster raising its ugly head(s)).
Like in this use case of mine:

Many thanks, buddy!

Let me mention an alternative version of @AlanG’s script with a tiny modification.
Obsidian indexes the information about headings, so you don’t have to do regex. Obsidian does that part on your behalf, so you only have to read it.

// 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 path = dv.current().file.path;
const { headings } = app.metadataCache.getCache(path);
const toc = headings
    ?.filter(({ level }) => level >= startAtLevel)
    .map(({ heading, level }) => {
        const link = dv.sectionLink(path, heading, false, heading);
        return '\t'.repeat(level - startAtLevel) + `1. ${link}`
    }).join('\n');

dv.header(2, 'Table of contents');
dv.paragraph(toc);
4 Likes

6 posts were merged into an existing topic: Outline in Document