Need help converting a block dataview query to inline

Hi, I have a fairly simple dataview block that I would like to convert to an inline query.

The query looks like this:

list where type = "monthly notes" sort file.ctime desc limit 1

Visually it looks like this:
Screenshot 2024-07-29 at 11.06.40 PM

The reason why is that I would like to have an inline link so that I could put that inline link into a header or paragraph instead of having it into a list on its own.

So I could have a link saying: “Current monthly notes” linking to that file. Instead of having a header and a single list item like on the screenshot.

So far it seems that inline DQL is mostly to display meta properties but not to do actual queries. I thought I might be able to do it with inline dataviewjs but I can’t seem to figure it out so I thought I’d ask here.

Anyone has an idea wether or not it’s possible?

Thanks!

There are two different approaches to this, both using dataviewjs to some extent. That is either to use it with a normal DQL query and extracting the result from there, or rebuilding the entire query into a pure dataviewjs query. In both cases, you’d most likely want to build the entire paragraph from within the query.

Or you also use an inline dataviewjs query, which is a little more icky with regards to getting the correct amount of semicolons and spaces, and what not.

Here are an example of each of these variants:


## DQL query from dvjs

```dataviewjs

const result = await dv.query(`
  LIST 
  WHERE type = "monthly notes"
  SORT file.ctime desc
  LIMIT 1
`)

if (result.successful) {
console.log(result)
 const firstResult = result.value.values[0]
 dv.paragraph("Current monthly note: " + firstResult)
} else
  dv.paragraph("~~~~\n" + result.error + "\n~~~~")
```


## dvjs variant

```dataviewjs

const result = dv.pages()
 .where(p => p.type && p.type == "monthly notes")
 .sort(p => p.file.ctime)
 .limit(1)

dv.paragraph("Current monthly note: " + result[0].file.link)
```

## Inline variant

Current monthly note: `$= dv.span(dv.pages().where(p => p.type && p.type == "monthly notes").sort(p => p.file.ctime).limit(1)[0].file.link) `

All produces the same output through different means. Only the first does some error handling.

1 Like

Wow! I can’t believe how extensive dataviewjs is. This opens so many possibilities. I wasn’t aware of those different syntaxes to handle queries. Obsidian is a developer’s dream come true.

It works perfectly, thanks for the examples!

1 Like

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