Self-Upgrading In Document Table of Contents Plugin

That’s theme related. Normally doing the entire list with 1. works fine in Markdown, but you can provide the actual numbers like this:

const startAtLevel = input?.level || 2
const content = await dv.io.load(dv.current().file.path)
const counter = [0, startAtLevel]
let numbers = [1]
const toc = content.match(new RegExp(`^#{${startAtLevel},} \\S.*`, 'mg'))
    .map(heading => {
        const [_, level, text] = heading.match(/^(#+) (.+)$/)
        const link = dv.current().file.path + '#' + text
        if (level.length > counter[1]) {
            counter[0]++
            numbers[counter[0]] = 1
        } else if (level.length < counter[1]) {
            counter[0] = Math.max(0, counter[0] - 1)
            numbers[counter[0]]++
            numbers = numbers.slice(0, counter[0] + 1)
        }
        counter[1] = level.length
        return '\t'.repeat(counter[0]) + `${input?.style || numbers[numbers.length - 1] + '.'} [[${link}|${text}]]`
    })
if (input?.heading !== false) {
    dv.header(2, 'Table of contents')
}
dv.paragraph(toc.join('\n'))
1 Like

Well, we could add a copy button to the outline core plugin

Why reinventing wheel ??

This is a one-liner that displays an automatically-updating table of contents inside the note, rather than in the sidebar and/or manually copy/pasting.

It’s a different wheel.

Maybe i didn’t explain very well.
My suggestion is very simple and doable, we only need a copy button in the core Outline plugin tab

This way we could paste a nicely formatted toc into our notes. I myself wrote a script copying a table of content to the clipboard. Then i copy this wiki-links into my note. It’s a bit manually but hey, i think adding a toc is something you do at the end of the day, basically for your readers and less, for yourself
I’d like to have this one integrated in Obsidian :wink:

To add to this: a TOC feature like Markdown All in One in vscode would be perfect.

The problem with one of the previous suggestions mentioning DataView or this obsidian-automatic-table-of-contents plugin is they’re only viewable in Obsidian. If we were to commit this to our team wiki, then readers would see this:

```table-of-contents
```

instead of a table of contents.

Hi Alan,

I love your solution and am using it widely.
I’m not very savvy when it comes to js though and I’d like to remove the numbering. Is that possible within the script, or would I have to do it with css?

There’s not a specific class to the TOC, and I don’t want to hide the numbering everywhere…

Thanks in advance!