Dataview / Templater issue with JS code

Hi guys,

I know that dynamic content generated by Dataview is an issue for a lot of us here. Especially with links.

Here the script I use with DataView :


So I try to copy past all the link above and I have a list of link like that :

le stress impact notre mémorisation%20Disque%20Dur/Archive/

I would like to erase everything after [le stress impact notre mémorisation] and add [] around to get this :

[[le stress impact notre mémorisation]]

For that, I would like to run a script with Templater.

Here’s my code :


function convertLinesToList(lines) {
  let links = [];
  for (let line of lines) {
    if (/\[.*?\]\((app:\/\/obsidian\.md\/.*?)\)/.test(line)) {
      let newLine = line.replace(/\[(.*?)\].*/, '[[$1]]');
  return links.join('\n')

BUT here’s the problem : When I run this script, It always “paste” the script and erase my content.

I know this script works out of Obsidian but not inside.

Someone can help me about that ?

Just one thing : I don’t know so much about JS, a friend of mine write the script for me.

Thanks a lot,

1 Like

You don’t need templater. Just try this dataview code instead:

List without id
"\[\[" + + "\]\]" 
from [[MEMOIRE]]


Then you can copy and paste the result.

Hello reaty,

Second time you participate to save me haha !

But as always, I have a little issue.

When I use that code, it doesn’t recognize all the links. Do you know if there’s a reason for that ?

But anyway, it realy solve my issue. Thanks a lot !

I found out why.

Thanks again !

I reckon you’re still wanting to convert your dataview query for a list of links into a note with both the links and contents, so here are two templates which goes toward that goal.

Template to get access to the list of links


const dv = app.plugins.plugins.dataview.api
const result =  await dv.queryMarkdown(`

if (result.successful) {
  tR += result.value.split("\n").map(l => l.substring(2)).join("\n") 
} else 
  tR += `~~~\n${ result.error }\n~~~\n`

Instead of going some convoluted way, and copy-pasting stuff, and re-running some template. Let Templater do the whole stuff. This template produces the list of links directly, after querying from. Replace/modify the query to your liking.

Including the content of the link, as well

Taking it one step further, and include the file content (if any) for this list of links:

const NAME_RE = /\[\[([^|]+)\|.*/
const dv = app.plugins.plugins.dataview.api
const result =  await dv.queryMarkdown(`

if (result.successful) {
  for (let line of result.value.split("\n")) {
	const link = line.substring(2)
    tR += link + "\n"
    const filematch = link.match(NAME_RE)
    if ( filematch )  
	  tR += await tp.file.include( app.vault.getAbstractFileByPath(filematch[1]) )
      tR += "==No file found=="
    tR += "\n"
} else 
  tR += `~~~\n${ result.error }\n~~~\n`

In addition to building the links, we know extract the filename out of the link, and use app.vault.getAbstractFileByPath() to get a tFile object, which pass on to tp.file.include() to get the full content of (including any template expansion).

In case of some simple errors, it’ll produce an error message related to the query going wrong, or if the file is missing. Could possibly also benefit from some try-catch around the actual file read.

So there you have an option to produce a full page of links and content based off a dataview query. Insert it into a template, and either associate a hotkey to it, or use the templater insert modal to trigger it.

You could potentially replace the query part with something like const result = await dv.query( tp.file.selection() ), where you then could open a file, insert your query (without the backticks around it, just the LIST ... part), select it and run the template and get the query replaced with the link and content stuff. This would allow for other queries, and not just this fixed query.

With a little more modification, you could even make it recognise that your selected text is an actual query, trim off the first and last line, and then run it. This would allow for building the query in a new note until you’re satisfied with the query result, and then select the entire query, and run the template to get it replaced by static content of the link and file contents.


Hello Holroy,

Waw ! That’s just awesome ! I didn’t expected something complete like that.

It works like wonder. Thanks a lot for that.

I hope it could help some others.

1 Like

Very nice! This approach could be used to provide a lightweight but very flexible “document transclusion compiler” – Sort of like Longform, but using dataview to select notes or parts of notes to include.

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