Random [[Dataview]] snippets for my own (and anyone else's) convenience. There are excessive line breaks and comments to help with documentation; remove when using to help readability.
#### Checking existence of a link in a list-type property in dataviewjs (dvjs)
Where a page may look like this:
```yaml
file.name = "some page"
---
classes:
- "Test"
- "[[My Class]]"
---
```
Explainer of contains bit:
```js
dv.func.contains( // must use dv's special contains() function that resolves links
dv.page("some page").classes // return list property
, dv.fileLink("My Class")) // convert your check page into a comparable link
)
```
As a full statement to get pages:
```js
dv.pages().where(p => dv.func.contains(p.classes, dv.fileLink("My Class")))
```
#### Page link displayed using first alias, if one exists, else normal (dv)
```js
default(link(file.link, aliases[0]), file.link)
```
#### Aggregating (e.g. summing) a property across pages (dvjs)
```js
dv.pages()
.where(p => p.val_to_sum) // where property exists
.val_to_sum // get property; returns object (that has other non-value elements)
.values // get just the 'value' values
.reduce((total, next) => total + next, 0) // replace with requisite function
.toFixed(2) // optional number format parsing, e.g. this one to two decimals
```
```js
dv.pages()
.where(p => p.val_to_sum)
.val_to_sum
.values
.reduce((total, next) => total + next, 0)
.toFixed(2)
```
#### Dynamically load the full content of a page (dvjs)
Hard-coded page:
```js
dv.span(await dv.io.load(dv.page("some page").file.path)); // or use dv.el()
```
In a for loop (you cannot use `forEach()` because of the `await`):
```js
let pages = dv.pages('"some folder"'); // or whatever critera
for await (const page of pages) {
dv.span("# " + page.file.name); // break with h1's using file name
dv.span(await dv.io.load(page.file.path));
}
```
#### Dynamically invoke a query defined within a property (dvjs)
For any page `"my script"` and any property `property_with_script`
```js
dv.execute(await dv.page("my script").property_with_script)
```
For the current page's `self_query` property
```js
dv.execute(await dv.current().self_query)
```
#### List all dot points (list items) mentioning a keyword
```js
TABLE list_item
FLATTEN file.lists.text AS "list_item"
WHERE contains(list_item, "keyword") -- or "[[keyword]]" for the so-named page
SORT file.cday DESC LIMIT 20 -- optional
```
#### List all dot points (list items) linking to a page
```js
TABLE WITHOUT ID file.link AS "Page", lists.text AS "Item"
FLATTEN file.lists AS "lists"
WHERE econtains(lists.outlinks, [[My Page]])
SORT default(date, file.cday) DESC LIMIT 20 -- optional
```
#### Parameterise a dataview query into a 'reporting services' page
````yaml
---
parameter: "keyword"
limit: 10
---
```dataview
TABLE list_item
FLATTEN file.lists.text AS "list_item"
WHERE contains(list_item, this.parameter)
SORT file.cday DESC
LIMIT this.limit
```
````
#### Return L1 folder of the file
```js
split(file.folder, "/")[0]
```
#### List all children and grandchildren
Where pages wikilink to their parent pages in a `parents` property:
```js
TABLE WHERE
econtains(flat(parents.parents), this.file.link) OR
econtains(parents, this.file.link)
```
#### Display a flattened list property excluding current file or other file
```js
join(sort(filter(classes, (x) => x != [[Not Me]] AND x != this.file.link)), ", ") AS "Subclass"
```
#### Forcing image size in result set
Leverages the Obsidian inbuilt sizing based on alt text (where the 200 below is actually for the alt text of the link)
```js
link(image_field, "200")
```
#### Listing your most-linked pages that do not yet exist
- Non-existent pages do not have file meta (`!outlink.file`).
- `length(rows) >= 5` is the threshold number of mentions, adjust as required.
- Image and file embeds count as outlinks, excluded here via specific extensions, but will need to be adjusted based on what you are embedding.
```js
TABLE length(rows) AS "mentions"
FLATTEN file.outlinks AS "outlink"
WHERE
!outlink.file
AND !endswith(meta(outlink).path, ".svg")
AND !endswith(meta(outlink).path, ".png")
AND !endswith(meta(outlink).path, ".jpg")
GROUP BY outlink
WHERE length(rows) >= 5
SORT length(rows) DESC
LIMIT 50
```
#### Using dataview to list the keys of all current Obsidian commands
```js
`$=dv.list(Object.keys(app.commands.commands))`
```