when it comes to variables, i usually use dataviewjs because they are easier to work with (no need to learn the documentation)
as for the dataview documentation, look up dv.view
and use cases on the forum
otherwise you can simply paste a dataviewjs query with a templater script: at cursor position with tR +=
or at a particular line (say, last line), or even replace a line
e.g your script would start like this:
<%*
const dv = this.app.plugins.plugins["dataview"].api;
const currentFile = app.workspace.getActiveFile();
if (!currentFile) {
new Notice("No active file found.");
return;
}
const editor = app.workspace.activeEditor.editor;
- you ought to learn this last line, for instance: make notes in a different vault on obsidian related functions
- you ought to learn that if you want to embed dataviewjs functionality in a templater script, you need to add
const dv = this.app.plugins.plugins["dataview"].api;
- all these stuff i had to gather from the forum myself so i’m no coder, you know…
then you have your dvjs query written and you format it like this (this is my use case, yours will be different):
// Create the dataviewjs query with the extracted parts
const dataviewjsQuery = `
\`\`\`dataviewjs
const regex = ${regexPattern};
const myPage = dv.page("${fileName}");
const content = await dv.io.load(myPage.file.path);
const lines = content.split('\\n');
const matches = lines.map(line => {
const match = line.match(regex);
if (match) {
let part1 = match[1].trim();
// Remove trailing ")" if present
if (part1.endsWith('")')) {
part1 = part1.slice(0, -2).trim();
}
return { part1: part1, part2: match[2].replace(/\\|/g, '').trim() };
} else {
return null;
}
}).filter(match => match);
dv.table(
[\`File\`, \`Content\`],
matches.map(match => [\`**\${match.part1}\**`, match.part2])
);
\`\`\`
`;
- you can use a
${tp.file.title}
variable what you tried or dv.current
- notice how the dataviewjsQuery variable is built and the way you needed to add
\
before backticks
in the last part you will do the insert, based on where you want it – in my case it was selection replacement:
editor.replaceSelection(dataviewjsQuery.trim());
but you can navigate the cursor to the end of the file, using:
this.app.workspace.activeLeaf.view.editor.setCursor({ line: 99999, ch: 0 });
then you can add the dataviewjsQuery with tR +=
then finally you close the script with:
%>
using the ingredients above, you can have your script written with a 2024 version gpt (gpt4o)