Obsidian quote database

After searching and not finding a solution that worked for me, I’ve hacked one together and hope that posting it here will save folks some time in the future.

Here’s the result I was after…

Achieved by installing Dataview (did I have to enable Javascript queries? I think so?)

Then capturing quotes like so:

---
tags:
  - quote
  - quote_v01
  - tao
author: Laozi
source: Tao Te Ching
aliases:
  - The Tao that can be trodden
  - The tao that can be told
---
> [!quote] The Tao that can be trodden  
> The Tao that can be trodden is not the enduring and unchanging Tao.  
> The name that can be named is not the enduring and unchanging name.  
>   
> — *[[Laozi]], [[Tao Te Ching]]*
^laoziquote

Which I can now reference in other notes with:

![[laozi - trodden tao#^laoziquote]]

And query with something like the following:

```dataviewjs
// Use backticks for the outer string to include double quotes
const pages = dv.pages(`"3 ENRICH/2.2 KNOWLEDGE/QUOTES"`);

// Initialize an array to hold the extracted quotes
let quotes = [];

// Regular expression to match the quote callout with optional source after [!quote]
const quoteRegex = /^>\s*\[!quote\]\s*(.*)\n((?:>.*\n?)*)/gm;

for (let page of pages) {
    // Load the content of the page
    const content = await dv.io.load(page.file.path);

    let match;
    // Use the regex to find all quote callouts in the content
    while ((match = quoteRegex.exec(content)) !== null) {
        // Extract the text after [!quote], could be 'Source' or empty
        const headerText = match[1].trim();

        // Extract the quote text from the content
        const quoteText = match[2]
            .split('\n')
            .map(line => line.replace(/^>\s*/, '').trim())
            .join(' ');

        // Get the 'Author' from the front matter
        const author = page.author ? page.author : "";

        // Get the 'Source' from front matter or use headerText
        const source = page.source ? page.source : headerText;

        // Create a link to the file
        const fileLink = page.file.link;

        // Add the extracted data to the quotes array
        quotes.push([quoteText, author, source, fileLink]);
    }
}

if (quotes.length > 0) {
    // Display the quotes in a table with 'Source' and 'File' columns
    dv.table(["Quote", "Author", "Source", "File"], quotes);
} else {
    dv.paragraph("No quotes found.");
}

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.