Dataview
up:: Obsidian Plugins
- ποΈ Documentation
- DataviewJS Documentation
- Dataview plugin snippet showcase - Share & showcase - Obsidian Forum
- DataviewJS Snippet Showcase - Share & showcase - Obsidian Forum
General
- The links introduced through dataview are not Obsidian notes, so donβt show up in backlinks or the graph
- Emojis are valid YAML metadata!
- See note as dataview does:
app.plugins.plugins.dataview.api.page('Note Name')
Methods:
file.size
file.name
file.mtime
: modified timefile.ctime
: created timefile.tags
: tags of filefile.path
: βFolder/Name.mdβ
Lists
- From tag:
from #moc
- From folder:
from "Attachements"
- Links
- coming into:
from [[Test MOC]]
- going out:
from outgoing([[test moc]])
- coming into:
- Logical operators:
and
or
- negate:
list from -#personal
- String Concatenation:
list "File path: " + file.path + " :)"
Tasks:
- Take all check boxes:
task from #moc
where {condition}
- comparison operators:
<
,>
,!=
- Examples:
- exclude Templates:
WHERE !contains(file.name,"emplate")
- exclude note: `where file.name != βTo excludeβ
- modified at least yesterday:
where file.mtime >= date(today) - dur(1 day)
- without a (or empty) yaml frontmatter field:
where !dueDate
- exclude Templates:
Tables
table authors from "Sources"
- Sort:
sort file.name asc
- for ties: multiple properties
flatten
: Each author gets own row (duplicate)- Grouping:
table intensity, rows.file.name
from "Assignments"
group by something
Snippets
DataviewJS
βBurn outβ to markdown
const dv = app.plugins.plugins["dataview"].api;
const te = await dv.queryMarkdown(`TABLE parent as Parent, siblings as Siblings, tags as Tags FROM "Zettelkasten ποΈ" WHERE contains(modified, "`+ tp.date.now('Do MMMM YYYY') + `") or contains(file.name, "`+ tp.date.now('YYYYMMDD') + `")` );
tR += te.value;
List
dv.list(
dv.pages("").where(p => p.file.folder == "A - Test"
).file.link
)
Table: Tiers + Owned Checkbox
dv.table(["", "Pic", "Title", "Author", "Tier"], dv.pages("#book")
.where(b => (b.status == "to-read"))
.sort(b => b.added, "asc")
.map(b => [readNext(b), ``, b.file.link, b.Author, checkTier(b)])
)
function readNext(book) {
if (book["read-next"]) {
console.log(encodeURI(book["read-next"]))
return `[β
](obsidian://advanced-uri?vault=Vault&filepath=${encodeURI(book.file.name)}.md&search=read-next%253A%2520true&replace=read-next%253A%2520false)`;
} else {
return `[βΈ](obsidian://advanced-uri?vault=Vault&filepath=${encodeURI(book.file.name)}.md&search=read-next%253A%2520false&replace=read-next%253A%2520true)`;
}
}
function checkTier(b) {
if (dv.page(b.Author).poc == "π€" && dv.page(b.Author).nationality != "πΊπΈ" && dv.page(b.Author).gender == "πΊ") {
return "β"
} else if (dv.page(b.Author).poc == "π€" && dv.page(b.Author).nationality == "πΊπΈ" && dv.page(b.Author).gender == "πΊ") {
return "πΊπ€"
} else if (dv.page(b.Author).poc == "π€" && dv.page(b.Author).nationality != "πΊπΈ" && dv.page(b.Author).gender != "πΊ") {
return "ππ€"
} else if (dv.page(b.Author).poc != "π€" && dv.page(b.Author).nationality != "πΊπΈ" && dv.page(b.Author).gender == "πΊ") {
return "ππΊ"
} else if (dv.page(b.Author).poc != "π€" && dv.page(b.Author).nationality == "πΊπΈ" && dv.page(b.Author).gender == "πΊ") {
return "πΊ"
} else if (dv.page(b.Author).poc == "π€" && dv.page(b.Author).nationality == "πΊπΈ" && dv.page(b.Author).gender != "πΊ") {
return "π€"
} else if (dv.page(b.Author).poc != "π€" && dv.page(b.Author).nationality != "πΊπΈ" && dv.page(b.Author).gender != "πΊ") {
return "π"
} else {
return "4: β"
}
}
Table: Availability
dv.table(["Pic", "Title", "Author", "Available?"], dv.pages("#book")
.where(b => (b.status == "to-read") && (dv.page(b.Author).poc != "π€" && dv.page(b.Author).nationality == "πΊπΈ" && dv.page(b.Author).gender != "πΊ"))
.sort(b => checkAvailability(b), "asc")
.map(b => [``, b.file.link, b.Author, checkAvailability(b)])
)
<!--ID: 1698382212802-->
function checkAvailability(b) {
if (b.owned) {
return " π Owned"
} else if (b.urls["library-url"]?.includes('libby')) {
return `[π² Libby](${b.urls["library-url"]})`
} else if (b.urls["library-url"]?.includes('libris')) {
return `[π Library](${b.urls["library-url"]})`
} else if (b.urls["kindle-url"]?.includes('amazon')) {
return `[π Kindle](${b.urls["kindle-url"]})`
} else {
return "*none*"
}
}
Sorting
By tzhou
on Discord:
.sort(p => p.file.name, 'asc', (a, b) => a.localeCompare(b, 'zh-CN'))
Pages read percentage
var pagesRead = dv.current().pagesRead; dv.paragraph("Read: **" + Math.floor((pagesRead / dv.current().pages * 100)) + "**% (" + (dv.current().pages - pagesRead) + "p left)")
To-do
wanted: list everything that doesnβt have the tag #publish
but links to a note that has it
Daily journaling
const dayInMonth = moment().format("D");
const currentMonth = moment().format("M");
var i = 1;
while (currentMonth > i) {
var entries = entriesSum(i);
var max = daysInMonth(i);
var perc = entries + "/" + max;
dv.span(
`### ${monthName(i)} <sup>[[${monthNote(i)}|${perc}]]</sup><br>${progressBar(entries, max)}`
);
// Current month
i++;
entries = entriesSum(i);
max = dayInMonth;
perc = entries + "/" + max;
if (currentMonth == i) {
dv.span(
`### π’ ${monthName(i)} <sup>[[${monthNote(i)}|${perc}]]</sup><br>${progressBar(entries, max)}`
);
}
}
// Math.floor(amountOfCompletedTasks / amountOfTasks * 100);
// <progress value="${amountOfCompletedTasks}" max="${amountOfTasks}">${amountOfCompletedTasks}</progress>
function monthName(month) {
return moment(`2022-${month}`).format("MMMM");
}
function entriesSum(month) {
var month = month.toString().padStart(2, '0');
const re = new RegExp(`2022-${month}-\\d{2}`);
return dv.pages('"10 Journals/2022.11 Dailies"').where(p => re.test(p.file.name)).array().length;
}
function daysInMonth(month) {
var month = month--;
return moment([2022, month]).daysInMonth();
}
function progressBar(entries, max) {
return `<progress value="${entries}" max="${max}">${entries}</progress>`;
}
function monthNote(month) {
return moment(`2022-${month}`).format("YYYY-MM-MMMM");
}