Automatically link back to note from where it was created

What I’m trying to do

  1. I have “Note 1” of type A in which I have a button to create a new note, “Note 2” of type B. I use the Meta Bind and templater plug in for this.
  2. The meta data of “Note 2” holds references to notes of type A.
  3. When clicking on the button in “Note 1” I would like the new “Note 2” to automatically get a reference to Note 1.

Things I have tried

I tried adding the following to the template for note B

A-notes:
  - "[[{{FromTitle}}]]"

Maybe it does not work with the button. It just become “{{FromTitle}}” as a link. I have also cosidered if I could pass the name when using the templaterCreateNote action of meta-bind plug in but could not find anything.

This is how I finally made it.

Required plugins:

  • Templater
  • Meta Bind
  • JS Engine

When pressing the button I call a js script that will store current file name to “@Extras/Global-var/linkToOpenFileName”.

The script I stored in “@Extras/Scripts/saveOpenFileName.js”

This is the script:

(async () => {
    try {
        // Get new content for global var openFileName.md
        const currentFile = app.workspace.getActiveFile().basename;
        const content = `[[${currentFile}]]`;

        const openFileNamePath = "@Extras/Global-var/linkToOpenFileName.md";

        // Get file
        let openFileContent = await app.vault.adapter.read(openFileNamePath);

        // Replace content
        openFileContent = content;

        // Save the file
        await app.vault.adapter.write(openFileNamePath, openFileContent);

    } catch (err) {
        console.error(`Error: ${err}`);
    }
})();

And here are the two templates (stored at “@Extras/Templates”)
Template, A-file:


meta-bind-button
label: Create New Note
icon: ""
hidden: false
class: ""
tooltip: ""
id: ""
style: default
actions:
  - type: js
    file: "@Extras/Scripts/saveOpenFileName.js"
    args: {}
  - type: templaterCreateNote
    templateFile: "@Extras/Templates/Template, B-file.md"
    folderPath: /
    fileName: newFileName
    openNote: true


Template, B-file:

---
A-files:
 - '<% tp.file.include("[[../Global-var/linkToOpenFileName]]") %>'
---


Other solutions

The solution described here does not currently work due to this bug.

I did this a couple of days ago, using Templater and QuickAdd.
The Templater script here allows me to get the name of the “parent” file so I can rename the child note using the parent note’s name (with a prefix added).
The {{Linkcurrent}} is from QuickAdd, to create a link back to the parent.
image

1 Like

Thank you for your input groberts56. Vey much appreciated! Based on your suggestion using QuickAdd I implemented solution below. It is less complicated and more secure.

Required plugins:

  • Templater
  • QuickAdd
  • Meta Bind

Steps:

  1. Create the Template, B-file:
---
A-files:
  - "{{LINKCURRENT}}"
---

  1. In QuickAdd settings, create a new quickadd of type template. In my case I called it “createNote”.
  2. In setting for “createNote” specify the template created in step 1.
  3. Check the lightning bolt for “createNote” to make it available in the command pallet.
  4. Create the Template, A-file shown below. The button is best done using the button builder in order to get the correct commandID.

```meta-bind-button
label: Create New Note
icon: ""
hidden: false
class: ""
tooltip: ""
id: ""
style: default
actions:
  - type: command
    command: quickadd:choice:2282c473-072b-443c-ad99-3d40309247a8

Thanks for the feedback Simon4! I need to check out that meta-bind-button.

For my use case I simply created a hotkey after doing the command palette add, so I can create the child note and insert the link to it at the cursor location.

1 Like

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