I was feeling a little strange when posting the previous post, as I kind of felt like I had seen the headings listed in the metadata somewhere. And surely enough when I was reviewing another question I came across this section of code:
```dataviewjs
const tp = app.plugins.plugins['templater-obsidian'].templater.current_functions_object
const candidate = dv.current().myHeading
const linkedPages = await Promise.all(await dv.pages('"ForumStuff/f73/f73565"')
.where( p => p.file.outlinks?.length )
.filter( p => dv.func.contains(p.file.outlinks, candidate) )
.forEach(async p => {
const theFile = await tp.file.find_tfile(p.file.name)
const theMetadata = await app.metadataCache.getFileCache(theFile)
console.log("theMeta: ", theMetadata.headings)
// Check if the headings has a link to our candidate
// ...
// If so, produce an embed of that and all other sub-headings
// console.log(dv.func.filter(dv.array(theMetadata.headings), p=> p.heading.includes(candidate.file.name)))
})
)
```
This query looks for a linked heading, used in Collecting all text under linked headings and allowing it be rendered as Markdown, but what’s interesting in the context of this thread, is that doing these lines:
const theMetadata = await app.metadataCache.getFileCache(theFile)
console.log("theMeta: ", theMetadata.headings)
Shows that theMetadata.headings
holds the headings with their position for that file, so the first entry for a random file in my test set shows:
So using similar code to this, combined with stuff from my previous post, you should rather easily be able to pick out both the “Notes section” heading, and the next one, and then find the line before that as your starting point for inserting new stuff.