I don’t see in your console output what I was looking for. There should be messages like: “Invalid link: ‘[[Link]]’” in the very end. Don’t you get anything like this, after you trying to use the template?
Are you using dataview or dataviewjs to produce the links in the first place? If the latter, then why don’t use that to also include the content of the files using something like await dv.io.load() directly instead of doing this as a secondary step?
Here is another version attempting to pull the content from the various links. This is slightly safer than previous versions since it checks for the full link, and allows for some text in front and after.
So save the following into a template file:
<%*
const content = tp.file.selection()
const lines = content.split("\n")
const LINK_RE = /\[\[.*\]\]/
for (const line of lines) {
tR += line + "\n"
const link = line.match(LINK_RE)
if ( link ) {
try {
const linkContent = await tp.file.include( link[0] )
tR += linkContent + "\n"
} catch (error) {
console.log(`Templater error for link: ${ link }:\n`, error)
tR += "==No content found==\n"
}
}
}
%>
And to run you either choose a hotkey for the template, or use the insert templater modal command, but before you insert the template be sure to select all the links you want to replace with a link and the content.
The previous versions, which doesn’t have this requirement will insert all the links from the current document at the point of the cursor when inserting the template, and leave other parts (aka all the links) untouched. This would appear to be some faulty links, but in fact it’s a faulty design choice since inserting a template by default will either be inserted at the cursor location, or replace the selected text with the output of the template.
To reiterate, after the inital setup of the template code into a template file of your choice, and potentially adding a hotkey of your choice, do the following:
Select all the lines containing links. This could very well be the entire file selected by doing Cmd A (or Ctrl A on windows)
Insert/run the template on the selected text, and it’ll get replaced by links following by the content of that link, if it has any
In addition, this version will produce an error message on the console if something fails, and add ==No content found== after the link in question
It looks to work perfectly well ! And it doesn’t forget any links, that’s even better. (@reaty : I really valuate your work as well !)
But it works only if I don’t generate links thanks to dataview. And if I want to copy/past links generated by Dataview, links are like that [link] and not like that [[link]]. So it’s a mess to deal with it …
The thing is that at the beginning of this topic, I didn’t use Dataview.
Now, It helps me organized my note automatically. On notes, I have on top, something like :
MoC : [[PSYCHOLOGY]]. So, when there’s a new note, there’s an automatic link on the page PSYCHOLOGY that link to my note. That’s an awesome add-on.
So now, Where would I put await dv.io.load() on your code to make it works with links generated by Dataview ?
Humm I am not sure if it would be possible to use your code on links generated by dataview.
When I select the content, click on the templater button, content is not selected anymore.