Obsidian插件:DataView

Structure of a Query

<QUERY-TYPE> <fields> 
FROM <source>
<DATA-COMMAND> <expression>
<DATA-COMMAND> <expression> ...

Query-Type fields

Query-Type 包括:

  • LIST 单个列(列即field)
  • TABLE 多个列
  • TASK 类似list单列查询
  • CALENDAR A calendar view displaying each hit

fields 类似 SQL的查询列(Column),常用的fields:

  • file.xx

file支持的属性: https://blacksmithgu.github.io/obsidian-dataview/annotation/metadata-pages/

  • file.name
  • file.mtime
  • file.tasks
  • file.lists

file还包括两个特殊的属性, list 和 task,这里指的是文件内容里,使用 gfm 的 list & todo语法格式的行,而不是“QUERY-TYPE”的 LIST 和 TASK

所以查询的 fields也支持 :

  • file.lists.text
  • file.tasks.text
  • 但是似乎没有 file.line 这种…

举例:

// 原文 * this's list1 [status:: todo]
// 查询全部的list中包含'status:: todo'的行

TABLE L.text AS "My lists"
FLATTEN file.lists AS L
WHERE contains(L.status, "todo")

file.list 和 task 还支持更多的查询属性 : 如 status、checked、text.. Metadata on Tasks and Lists - Dataview

FROM source

FROM后可以是 文件夹 or Tag,
例子-Combining sources:

LIST
FROM "Projects" AND (#project/active OR #project/soon)

DATA-COMMAND

Data Commands - Dataview
包括 FROM、WHERE、SORT、GROUP BY、FLATTEN

FROM

  • Tags: To select from a tag (and all its subtags), use FROM #tag.
  • Folders: To select from a folder (and all its subfolders), use FROM "folder".
  • Single Files: To select from a single file, use FROM "path/to/file".
  • Also “negate” sources: FROME -#tag AND -"folder"

WHERE

LIST WHERE file.mtime >= date(today) - dur(1 day)
LIST FROM #projects
WHERE !completed AND file.ctime <= date(today) - dur(1 month)

FLATTEN

Flatten an array in every row, yielding one result row per entry in the array.

自定义metadata

https://blacksmithgu.github.io/obsidian-dataview/annotation/add-metadata/

自定义mate支持两种方式:

  • Frontmatter 里定义kv对
  • Inline 的方式定义kv对

举例1:原文 TODO:: 买机票 定义了kv对,key即‘TODO’,val即‘买机票’,如果要查询出全部的TODO,语句如下:

TABLE TODO
where TODO
SORT file.mtime desc

举例2:原文如下

I would rate this a [rating:: 9]! It was [mood:: acceptable].

- [ ] Send an mail to David about the deadline [due:: 2022-04-05].

查询:

task from #projects/active
sort rating desc
where mood = "acceptable"

对task的支持

参考: Metadata on Tasks and Lists - Dataview

dataview 支持一些预定的 metadata,例如 start、due、completion

例子1,@ref:: https://forum.obsidian.md/t/create-dataview-table-of-multiple-tasks-within-notes/31054

TABLE WITHOUT ID 
regexreplace(Tasks.text, "\[.*$", "") AS Task,
choice(Tasks.completed, "✅completed", "➡️incompleted") AS Status,
Tasks.due AS "Due Date",
regexreplace(Tasks.subtasks.text, "\[.*$", "") AS Subtasks,
file.link AS "File"
FROM "@Project/active" FLATTEN file.tasks AS Tasks
WHERE file.tasks

例子2

TABLE WITHOUT ID 
regexreplace(Tasks.text, "\[.*$", "") AS Task,
Tasks.due AS "Due Date",
Tasks.link AS "File"
FROM "@Project/active" FLATTEN file.tasks AS Tasks
WHERE !Tasks.completed

API & 高级用法(dataviewjs)

例1:

dv.taskList(dv.pages("#idea").file.tasks
.where( t=> t.text.includes("#idea"))
.where( t=> !t.completed))

例2:

let pages = dv.pages("#标签1 and -#标签2")
dv.table(
[name, genre],
pages.sort(b => file.mtime, "desc").map(b => [b.file.link, b.genre])
)

注:需要代码块标注 dataviewjs

dataviewjs 语法参考:

最佳实践:笔记中使用 Dataview Friendly 的标识

方案1,使用自定义meta kv:

@todo1:: 这是一个todo
@doubt1:: 这是一个疑问

方案2,使用2级标签:

#inbox/todo 
#inbox/done
#project/active
#project/closed