Dataview

up:: Obsidian Plugins

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 time
  • file.ctime: created time
  • file.tags: tags of file
  • file.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]])
  • 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

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), `![p|70](${b.urls["thumbnail-url"]})`, 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 => [`![p|70](${b.urls["thumbnail-url"]})`, 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");
}