Why doesn't dataview return tagged substrings?

Hello all, I hope you are having a good day and thank you for reading! Now without further adieu…

What I’m trying to do

I am trying to surface the text in a note that ends with #question using Dataview. I would like it so that every row in this Dataview table contains the full line that proceeds a tag of #question. So for a file that has this:

  • Here’s an example:
    • Blah
      • Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
    • Blahblah
      • This line should be returned as a row #question

Dataview can identify files that have a certain tag, but cannot return the strings in those files that end with the tag.

Things I have tried

So first, this query successfully returns files that contain the tag:

table file.name AS "Note"
where contains(file.tags, "#question")

The issue is returning the substring in that file which precedes the tag. I have tried this query:

table substring(file.content, 0, 500) AS "Content"
where contains(file.tags, "#question")

I have also tried more complicated queries, such as:

table file.cday as "Created", substring(file.content, 0, 500) as "Snippet"
from "Self"
where regexmatch(".*#question$", file.content)

But for some reason, none of these surface the content that ends in #question.

Why is this? What am I doing wrong?

Also, the file looks something like this attached file:
Test Doc.md (1.1 KB)

If there is some change I can make to the way I write my notes that would make them easier to query, I am more than happy to do this. Any help is appreciated and thanks for reading (・ω・)ノ

In a pure Dataview query, there is no file.content. It’s not avaiable. To get the entire content of a file, you need to switch to dataviewjs, and read the file yourself through coding.

Upon further reading of your use case, I see that you’ve encapsulated the text you want to list in a list item, that makes it a lot easier to get the content.

Try this query:

```dataview
TABLE file.cday as Created, item.text as Snippet
FROM "Self" AND #question
FLATTEN file.lists as item
WHERE contains(item.tags, "#question")
```

This splits the lists found in a file into separate row (through the FLATTEN operation), and selects the one with the tag in it, and displays only that one. This only done on files within “Self” which somewhere in the file have the tag #question.


An alternate way to achieve something very similar is to use inline fields. Then your document could look like:

- Blah
  - Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
- Blahblah [question:: This is my question]

Then you could do an even simpler query:

```dataview
TABLE file.cday as Created, Q as Question
FROM "Self"
WHERE question
FLATTEN question as Q
```

In this query we use FLATTEN to split up if there are multiple questions. If you don’t mind them being displayed in the same row in the table. You could remove the FLATTEN line, and use question as Question in the first line.

This syntax would also allow for styling the question however you want through CSS, and similar syntax could be use for highlights, extracts, and what not.

1 Like

Incredible! Thank you so much, the first query works. The second does not and returns nothing.

That said, the first query is more than sufficient, thank you!

The second one would need for your input to change accordingly to the example. It is untested, but it should work. :slight_smile:

1 Like