Please help me formulate code for the DataView plugin. I want it to select a random quote from notes in the ‘Citations’ folder when opening a daily note that contains the ‘###Citation’ heading.
May I suggest doing a search in the forum.
This post may help you:
I wrote this code in my note, and it gives me an error and I wanted to ask that this code will only show notes, but I need it to go to the note and select a random quote. This code:I wrote this code in my note, it gives me an error and I wanted to ask, this code will only show notes, but I need it to go to the note and select a random quote.
What code:
<%*
const noOfNotes = 5
const dv = this.DataviewAPI
const files = await dv.tryQuery(`
LIST
FROM -"_tools/template" AND -"Presentations/Export"
`)
let randomList = []
for (let i = 0; i < noOfNotes; i++) {
const random = Math.floor(Math.random() *
(files.values.length - 1))
randomList.push(files.values[random])
}
tR += dv.markdownList(randomList)
%>
What error:Evaluation Error: SyntaxError: Unexpected token ‘<’
at DataviewInlineApi.eval (plugin:dataview:18638:21)
at evalInContext (plugin:dataview:18639:7)
at asyncEvalInContext (plugin:dataview:18646:16)
at DataviewJSRenderer.render (plugin:dataview:18670:19)
at DataviewJSRenderer.onload (plugin:dataview:18260:14)
at e.load (app://obsidian.md/app.js:1:1158302)
at DataviewApi.executeJs (plugin:dataview:19198:18)
at DataviewPlugin.dataviewjs (plugin:dataview:20068:18)
at eval (plugin:dataview:19967:124)
at t.initDOM (app://obsidian.md/app.js:1:1559390)
at t.toDOM (app://obsidian.md/app.js:1:1152358)
at t.sync (app://obsidian.md/app.js:1:356467)
at e.sync (app://obsidian.md/app.js:1:338200)
at app://obsidian.md/app.js:1:378989
at e.ignore (app://obsidian.md/app.js:1:458200)
at t.updateInner (app://obsidian.md/app.js:1:378771)
at t.update (app://obsidian.md/app.js:1:378526)
at e.update (app://obsidian.md/app.js:1:466848)
at e.dispatchTransactions (app://obsidian.md/app.js:1:463531)
at e.dispatch (app://obsidian.md/app.js:1:465414)
at app://obsidian.md/app.js:1:1576669
It runs fine in my Obsidian.
Are you sure you placed this code in the templates folder?
This is JavaScript code that should be part of a template.
That code is used to pull a random number of notes from a file list. It’s not written to extract text out of a file.
To get what you want you’ve got two options:
-
if your quotes are not in list format you’ll need to use javascript to read the content of the file, and search for the correct section and extract your quotes “manually”
-
if your quotes are in lists, you can use a dataview query on the file and do something like
FLATTEN file.lists as item
where eachitem
would be one of your quotes. Then you could select a random one if them and display it.
Example code for both of the above are available in the forum. The first related to extracting sections using javascript.