84 lines
3.8 KiB
Markdown
84 lines
3.8 KiB
Markdown
---
|
||
title: dataview examples for Obsidian
|
||
source: https://willcodefor.beer/posts/dataview
|
||
---
|
||
|
||
[Dataview](https://blacksmithgu.github.io/obsidian-dataview/) is a query engine for [Obsidian](https://obsidian.md/). 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 [#](#hub-pages)
|
||
|
||
As I’ve [previously mentioned](https://willcodefor.beer/notes), 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 [#](#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 [#](#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](https://github.com/SilentVoid13/Templater) plugin to insert today’s date.
|
||
|
||
**Update:** I’ve improved this query using `regexmatch` in [a separate post](https://willcodefor.beer/dataviewio).
|
||
|
||
## Fleeting notes [#](#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")
|
||
dv.table(
|
||
["File"],
|
||
dv.pages("#fleeting")
|
||
.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")
|
||
dv.table(
|
||
["Note", "Link"],
|
||
dv.pages("#fleeting").file.lists
|
||
.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. |