Home Writing Reading

til / dataview examples for Obsidian

Dataview is a query engine for Obsidian. With it, you can, for instance, collect links to all pages tagged with #book and sort them by rating (if you’ve added it to the page’s metadata). It has two languages for creating queries, Dataview Query Language (DQL), which looks similar to SQL, and a JavaScript API.

  • DQL queries use a code block tagged as dataview
  • JavaScript queries use a code block tagged as dataviewjs

Hub pages #

As I’ve previously mentioned, I use the Zettelkasten method to create notes. When I create a development note, I tag it with #development and #permanent.

TABLE file.cday as "Created"
FROM #development AND #permanent 
SORT file.name ASC

Using these tags and Dataview, I can create a hub page that lists all my development notes, sort them by name and display the date it was created. I have a similar hub page and query for notes related to my job. This query uses the DQL syntax and displays the result in a table.

House tasks #

Sometimes I add to-dos related to my house on my daily pages. I tag these with #house. I also have a note with information related to the house. Using Dataview I can gather all uncompleted house tasks in the house note.

dv.header(3, "Tasks")
dv.taskList(dv.pages("#house").file.tasks.where(t => !t.completed))

This query uses the JavaScript API. It first adds a h3 header, ### in Markdown, with the word “Tasks”. It then renders a task list, a list with the to-do’s checkbox included, for all uncompleted tasks with the tag #house.

The same query, without the header title, in DQL syntax would be:

TASK FROM #house WHERE !completed

We can also group the tasks based on where they were defined.

TASK FROM #house WHERE !completed GROUP BY file.link

Notes created today #

I have a section for “Input/Output” on my daily notes page. Here I use a query that lists all the pages that were created on that day.

LIST WHERE file.cday = date("2022-06-15") AND file.name != "2022-06-15"

This creates a bullet list with all the pages created today, but not the daily notes page itself. This is added in my daily notes template and uses the Templater plugin to insert today’s date.

Update: I’ve improved this query using regexmatch in a separate post.

Fleeting notes #

The last example is very specific for my use case, but it demonstrates the power of the plugin. I tag fleeting notes on my daily notes page with #fleeting. I also tag pages that I haven’t created permanent notes from yet as #fleeting. To collect all the fleeting notes, I’ve created a “Fleeting notes” page where I have two queries.

dv.header(2, "Page notes")
	.filter(p => !p.file.name.match(/^\d{4}-\d{2}-\d{2}$/i))
	.map(p => [p.file.link]))

I don’t want any daily notes pages to be listed here, as they will be picked up using the list format below. Therefore, I’ve added a regex that will filter out any dates (YYYY-MM-DD) in the filename. Lastly, I display a link to the page in table format.

dv.header(2, "List notes")
	["Note", "Link"],
	.filter(l => l.tags.includes("#fleeting"))
	.map(l => [l.text, l.link])

This query will find lists inside pages that contain the fleeting tag, filter out rows that don’t have the fleeting tag, and list the item’s text and link to the page in a table. Pretty powerful and lets me keep on top of my fleeting notes.

  • Loading next post...
  • Loading previous post...