New maintainer needed for "Dynamic ToC"

@mrienstra you can achieve a reasonable implementation of this using Dataview, which might help in the decision of maintaining this or not.

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

7 Likes