Accessing file headings as a variable in dataview

I’m trying to create a table in dataview which will list the content of the second header in each file. e.g. something like

FROM #my-tag 

where x retrieves the content of the text in the file’s 2nd heading. (These files from #my-tag only have one 2nd heading.) So e.g. if one of the files from #my-tag had a heading “## fish sticks” then the text string “fish sticks” would show up in the table from the dataview query.

Things I have tried

Tried for longer than I would like to admit searching the dataview documentation, googling for similar issues (these looked related but I couldn’t use them to solve my problem: 1, 2) etc. but nothing I’ve found has been quite on point.

This question has come up quite a few times on the forum in the past few days! dataview works on Field:: value pairs. Some fields like and file.tasks are “implicit”, so you do not need to write them down anywhere in your file to use them, but for anything that is not on the “implicit field” list in the documentation, you cannot access it without a Field_name:: Field_value annotation. Headings and other text content of your file are not on the implicit list.

Possible alternatives:

  • if your files have only one level 2 heading, you could use Obsidian’s built-in search via an embedded query block. There is a query operator for tags that can filter for my-tag and I think you can use a regular expression (regex) for “line that starts with exactly ## and then a space” to get the level 2 heading. Pinging @I-d-as or some other regex expert on the forum for how to translate that last bit from English into a regex. Hopefully one of them can share how to display the text under that heading in the query results.
  • Call your level 2 heading the same thing every time and use ![[otherNote#lvl2HeadingName]] embedding to display the section.
  • Save the name of your level2Heading in some dataview variable (for example myHeading:: and then use a LIST or TABLE dataview to embed(link("pathToOtherFile#" + myHeading)) embed the section as in the option above. There is a note on the dataview page that embed may not display consistently, so if you have issues you will have to try some other tactic.
  • Maybe there is a different community plugin that can do this?

Good luck!

1 Like

Thanks for the input!

To clarify, I am not trying to reproduce the content of the section underneath the heading, rather I am only trying to extract the text of the heading itself for display in a dataview table. I use the contents of this heading as a kind of title for the file (which varies between files) so the idea is that I would like the dataview table to list the “title” of the file in the dataview table, as contained in the text for that file’s second heading.

I know that technically I could achieve this by defining a dataview field called something like “title” in each file that would reproduce the title text, but that feels like an inelegant solution that would require a fair bit of work for me to go back and format a bunch of files that already have the title info in them.

If it really is not possible to extract the heading contents as an implicit field then the solution to fetch the title using a regex solution that returns text following the characters '## ’ seems like it would be a workable patch, if someone could share how to do that.

1 Like

For what it’s worth the regex to match the rest of the line that occurs after ## at the beginning of a line is the following (for embedded queries):

/(?<=\n## ).*/

By the way, this regex only works on Obsidian desktop, I think.

However, in an embedded query this will simply highlight the text as it appears in files throughout the vault, not actually make the string accessible as far as I know. On a side note, this reminds me of another issue dealing with searching just this note that you have been trying to figure out. Anyways, I experimented with using contains in conjunction with regexmatch to build a query to return the string in a table, but I had no luck.

Maybe someone can fill in the gaps or see something we are missing. I’d be very interested to see a solution. Thanks.

Good luck!


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.