obsidian-dataview icon indicating copy to clipboard operation
obsidian-dataview copied to clipboard

Link to header created by dataviewjs

Open alexey-zaharchenko opened this issue 3 years ago • 6 comments
trafficstars

Where is any way to create link to header that was created in dataviewjs? Something like this:

file1.md:

[[file1.md#header]] <- Unable to find section #header in file1.md

 ``` dataviewjs
dv.header(1,'header')
```

alexey-zaharchenko avatar Jul 23 '22 18:07 alexey-zaharchenko

Can you describe the use case with an example?

AB1908 avatar Jul 23 '22 20:07 AB1908

I have table of all frontmatter keys with number of notes that have it and some additional columns, its something like custom tailored TOC. After that table there is section for every key with additional info like all notes, most used values, etc. I wanna be able to have a link from "TOC" to every section because page becoming too long.

alexey-zaharchenko avatar Jul 23 '22 21:07 alexey-zaharchenko

Oh now that I'm reading this again, there isn't really a way to link to headers rendered in DVJS. The reason is that this is a "view" and these don't exist in the file and so can't be linked to. However, blacksmithgu might know of a way to do this since I'm not very familiar with the internals. I imagine a workaround does exist, just that it might not be straightforward.

Meanwhile, I think I don't fully understand the use case. Do you mean to say that you have a TOC defined in the frontmatter and want to use that to generate dynamic links?

AB1908 avatar Jul 30 '22 08:07 AB1908

Yes and no :-) I create page with info of every frontmatter key of every page in vault and that page have TOC created via dataviewjs.

Maybe code will be more clear:

const fmts = [];
const rows = dv.pages().forEach(note => {
                  for (const k in note.file.frontmatter) {
                      if (!fmts[k]) {fmts[k] = []}
                      fmts[k].push(note.file.link)
                  }
              })

dv.table(['key name', 'count', 'examples'], Object.keys(fmts).map(k => [
    k // section link doesn't work here yet dv.sectionLink(dv.current().file.path,k,false,k)
    , fmts[k].length
    , fmts[k].slice(0, 3)
]));

dv.el('hr', '')

Object.keys(fmts).forEach(k => {
    dv.header(2, k);
    dv.list(fmts[k]);
})

alexey-zaharchenko avatar Jul 30 '22 09:07 alexey-zaharchenko

I see. Fairly certain that this is not doable but I'll let blacksmithgu add color to this.

AB1908 avatar Aug 26 '22 21:08 AB1908

Dataview headers are not "real" headers and can't be linked to (since they directly render to your view, and are not Markdown).

blacksmithgu avatar Aug 28 '22 00:08 blacksmithgu