[Obsidian] Debug Dataviewjs script
Obsidian is a writing app that allows us to write notes and visualize on linking map. I use this app to build my sencond brain.
Dataview is an Obsidian’s plugin - A high-performance data index and query language over Markdown files.
Today I leanred that we can debug dataviewjs script by using Developer Tool Console. (Open by Ctrl + Shift + I
or ⌥ + ⌘ + I
), with simple declaration: dv = DatviewApi
This is sample dataviewjs code (source) to filter and combine all my finance (thinking) notes to 1 file:
// Headings you would like to summarise the text for
const headings = ["Finance Notes"]
// You can update this to filter as you like.
// Here it is only returning results inside the "Daily notes" folder
const pages = dv.pages('"10 Journals"')
const output = {}
headings.forEach(x => output[x] = [])
for (const page of pages) {
const file = app.vault.getAbstractFileByPath(page.file.path)
// Read the file contents
const contents = await app.vault.read(file)
for (let heading of headings) {
// Sanitise the provided heading to use in a regex
heading = heading.replace(/[/\-\\^$*+?.()|[\]{}]/g, '\\$&')
const regex = `(^|\n)#+ ${heading}\r?\n(.*?)(\n#+ |\n---|$)`
// Extract the summary
for (const block of contents.match(new RegExp(regex, 'isg')) || []) {
const match = block.match(new RegExp(regex, 'is'))
output[heading].push({
title: file.basename,
text: match[2].trim()
})
}
}
}
Object.keys(output).forEach(heading => {
dv.header(1, heading)
output[heading].sort((a, b) => {
if (a.title < b.title) {
return 1
}
if (a.title > b.title) {
return -1
}
return 0
})
output[heading].forEach(entry => {
dv.header(4, entry.title)
dv.paragraph(entry.text)
})
})