Could you assist me in writing code for handling Citations? (help)

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(`
  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))

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://
at DataviewApi.executeJs (plugin:dataview:19198:18)
at DataviewPlugin.dataviewjs (plugin:dataview:20068:18)
at eval (plugin:dataview:19967:124)
at t.initDOM (app://
at t.toDOM (app://
at t.sync (app://
at e.sync (app://
at app://
at e.ignore (app://
at t.updateInner (app://
at t.update (app://
at e.update (app://
at e.dispatchTransactions (app://
at e.dispatch (app://
at app://

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:

  1. 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”

  2. 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 each item 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.

1 Like

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