Templater: open file in new tab and append to end of file

Found some good stuff here, thanks to the participants.

I changed some of the script to suit my needs: I found that if these temporary queries are above the main content, they are less likely to start querying the page again and again when I move my cursor to edit the material and come back to the top to check and click on results.

<%*
const files = app.vault.getMarkdownFiles().map(file => {
  const fileCache = app.metadataCache.getFileCache(file);
  if (fileCache?.frontmatter?.aliases) {
    file.display = `${file.basename}\n${fileCache.frontmatter.aliases.join(", ")}`;
  } else {
    file.display = file.basename;
  }
  return file;
});
const selectedItem = (await tp.system.suggester(item => item.display, files)).basename;

const note = tp.file.find_tfile(`${selectedItem}.md`);

const insertedContent = `\n\n%%\n\`\`\`query\nfile: /^${selectedItem}.md/ /\\b[a-záàäæãééíóöőüű]+\\s*=\\s+[^\`]*?(?=\\s|\\z)|(?:(?:German|English|Greek|Hebrew|Arabic|Slavic|Turkic|Turkish|Ossetian|Alanian|Avestan|Sanskrit|Persian|Egyptian|Sumerian|Accadian|Akkadian|Assyrian|Assyr-Babilonian|Hittite[^\`]*?))\\s([^\`]+?)\\s/\n\`\`\`\n\`\`\`query\nfile: /^${selectedItem}.md/ /(Evernote|otherstuffiwanttocommentout)(?!.*\\%\\%)/\n\`\`\`\n%%`;

const fileContent = await app.vault.read(note);
const separator = '---';

const firstSeparatorPos = fileContent.indexOf(separator);
const secondSeparatorPos = fileContent.indexOf(separator, firstSeparatorPos + 1);

if (secondSeparatorPos !== -1) {
  const updatedContent = fileContent.slice(0, secondSeparatorPos + separator.length) +
    `${insertedContent}` +
    fileContent.slice(secondSeparatorPos + separator.length);
  
  await app.vault.modify(note, updatedContent);
}

await new Promise(resolve => setTimeout(resolve, 250));
app.workspace.getLeaf(true).openFile(note);
_%>

The tp.file.title parts had to be indeed exchanged with selectedItem variables in all queries, otherwise the title of file triggered from was used.

In the content to be added, I had to single escape backticks and add an extra slash where there was one already in the query.

As for the queries: in the second query, I check for cue words I don’t want to be in my published material (like Evernote stuff I don’t want to link to) and other things I need to comment out. I have many. In the language part, I only listed a limited number as well. No need to clutter up the code here now.

These temporary queries can help one review one’s material before publishing them.

If you use Linter, turn on Lint on File Change (generally, not for this specific purpose, don’t forget to exclude your template and other folders where your meta stuff is) and in the regex section (last but one), add:

Regex to find: \n\n^%%\n```query[\d\D]*?^%%
Regex to replace: leave this box empty.

So if leave your file now, the query will be gone next time you enter the file (the usual way).

The code expects the user to have a valid YAML with a pair of --- separators.

If you want to add to the active file, use:

<%*
const separator = '---';

const currentFile = app.workspace.getActiveFile();
if (!currentFile) return;
const editor = app.workspace.activeEditor.editor;

const fileContent = await app.vault.read(currentFile);
const firstSeparatorPos = fileContent.indexOf(separator);
const secondSeparatorPos = fileContent.indexOf(separator, firstSeparatorPos + 1);

if (secondSeparatorPos !== -1) {
  const dynamicContent = `\n\n%%\n\`\`\`query\nfile: /^${tp.file.title}.md/ /\\b[a-záàäæãééíóöőüű]+\\s*=\\s+[^\`]*?(?=\\s|\\z)|(?:(?:German|English|Greek|Hebrew|Arabic|Slavic|Turkic|Turkish|Ossetian|Alanian|Avestan|Sanskrit|Persian|Egyptian|Sumerian|Accadian|Akkadian|Assyrian|Assyr-Babilonian|Hittite[^\`]*?))\\s([^\`]+?)\\s/\n\`\`\`\n\`\`\`query\nfile: /^${tp.file.title}.md/ /(Evernote|otherstuffiwanttocommentout)(?!.*\\%\\%)/\n\`\`\`\n%%`;

  // Replace placeholders with actual values
  const contentToAdd = dynamicContent.replace('${currentFile}', currentFile);

  const updatedContent = fileContent.slice(0, secondSeparatorPos + separator.length) +
    contentToAdd +
    fileContent.slice(secondSeparatorPos + separator.length);

  await app.vault.modify(currentFile, updatedContent);
editor.setCursor({ line: 0, ch: 0 });
}
_%>
  • And of course, change your queries…
2 Likes