I am pretty pleased that I finally found a way to collect quotes from all the notes in my Obsidian Vault. I am using Dataview to query it out.
All thanks to GPT4. We went back and forth with the code, the errors, the gaps in the logic and finally after a day I am pleased with what I have now!
Hope it is useful to those looking for something similar. One thing to note before you proceed, my quotes all use Markdown Admonitions syntax of >[!quote]
and has the tag #Quote
async function loadQuotes() {
let quotes = [];
const pages = await dv.pages("#Quote");
for (const page of pages) {
const content = await dv.io.load(page.file.path);
if (typeof content === "string") {
let inQuoteBlock = false;
let quoteBuffer = [];
content.split('\n').forEach((line) => {
if (line.trim().match(/^>\s*\[!quote\]/) && line.includes("#Quote")) {
if (inQuoteBlock) {
addQuote(quotes, quoteBuffer, page);
}
inQuoteBlock = true;
let titlePart = line.split("#Quote")[1] || "";
quoteBuffer = [`<b>${titlePart.trim()}</b>`];
} else if (inQuoteBlock) {
if (line.trim() === "") {
addQuote(quotes, quoteBuffer, page);
inQuoteBlock = false;
} else {
let formattedLine = line.trim().replace(/^>{1,2}/, '').trim().replace("[!quote]", "").replace("#Quote", "");
quoteBuffer.push(formattedLine);
}
}
});
if (inQuoteBlock && quoteBuffer.length > 0) {
addQuote(quotes, quoteBuffer, page);
}
}
}
quotes.sort((a, b) => b.rawModifiedTime - a.rawModifiedTime);
let numberedQuotes = quotes.map((q, index) => [index + 1, q.quote, q.source]);
dv.table(["#", "Quote", "Source"], numberedQuotes);
function addQuote(quotes, quoteBuffer, page) {
let modifiedTime = page.file.mtime ? dv.date(page.file.mtime) : 'Unknown';
quotes.push({
quote: quoteBuffer.join("<br>"),
source: `[[${page.file.name}]]`,
rawModifiedTime: page.file.mtime || 0
});
}
}
loadQuotes();