Template that titles new note using previous note

What I’m trying to do

I’m trying to setup an (Antinet) Zettelkasten workflow in my Obsidian (again). To create a flow of children/sibling note creation I want to create a template which uses the title of the current note e.g. “123” and adds a suffix to in creating the new note.

I’ll say it another way because that sounds like a lot of jargon. Let’s say I have a note open with the title “123” (or “456” or “7890”). I want to create a new note which will be titled “123a” (or “456a” or “7890a”. So it’s “previous note title (123) + suffix (a)”.

Things I have tried

Initially I tried using the “Luhmann” plugin but the numbering system in that is too rigid for my needs (I had set up an analogue system and want to move that into Obsidian. I’m not using Niklas Luhmann’s exact numbering but an Antinet numbering). So I’m trying to create a base template that I can tinker with to meet my needs.

After failing with the plugin, I thought I could do this with templater. I searched the forums but came up empty. I turned to ChatGPT and Claude and they came up with these javascript blocks for templater which didn’t work. I’ll give an example they came up with below followed by the error code I got in the console.

I’m a java-illiterate troglodyte so I’m not sure where things went wrong. I thought maybe the problem was that the title contained bad characters (like {title} or something (again I’m a troglodyte I know I know I’m a troglodyte). In which case maybe I could populate the title of the previous note (from which the template was invoked) into the body of the new note and then use a tp.file.rename templater function to break this into a two-step process but the AI approach was getting me nowhere so I said I would try throwing myself at the mercy of the community and hope someone here might be able to help.

Thanks in advance for any tips or help

Code Block from Chat GPT

<%*
// Get the current note's title
const currentTitle = tp.file.title;

// Define the suffix to add
const suffix = 'a';

// Create the new title
const newTitle = currentTitle + suffix;

// Specify the content for the new file
const content = `---
title: ${newTitle}
date: ${tp.date.now("YYYY-MM-DD")}
---

# ${newTitle}

Created on ${tp.date.now("dddd, MMMM Do YYYY")}
`;

// Create the new file with the new title and content
await tp.file.create(newTitle, content, true);
%>

Error Code from Console:

"plugin:templater-obsidian:61 Templater Error: Template parsing error, aborting.
tp.file.create is not a function"

You could try using tp.file.create_new() :blush: .

AFAIK, tp.file.create() isn’t a “Templater thing”.

1 Like

Thanks for that one @Pch. I tried it out but no go. But a clearer error message perhaps:

Templater Error: Template parsing error, aborting. 
 File name cannot contain any of the following characters: * " \ / < > : | ?
For context here's the updated code block that I tried:
// Get the current note's title
const currentTitle = tp.file.title;

// Define the suffix to add
const suffix = 'a';

// Create the new title
const newTitle = currentTitle + suffix;

// Specify the content for the new file
const content = `---
title: ${newTitle}
date: ${tp.date.now("YYYY-MM-DD")}
---

# ${newTitle}

Created on ${tp.date.now("dddd, MMMM Do YYYY")}
`;

// Create the new file with the new title and content
await tp.file.create_new(newTitle, content, true);
%>

Okay after some thinking around the problem and more noodling with chatbots I found the solution for future reference. The code block below in a template will create a new note with the title of the most recently edited note and in the same folder as that note. It will add an “a” to the end of that note (you can change this by changing the a at the end of the line

const newFileName = `${secondMostRecentFile.basename}a`; 

This is useful for anyone doing Zettelkasten because it means you can create the next note in the sequence without having to retype the (sometimes very long) numbers (assuming you’re using a Luhmann-esque zettelkasten numbering system or an Antinet numbering system). It makes digital zettelkasten that bit easier.

The template code block working for me now:

<%*
const { app } = this;
const files = app.vault.getMarkdownFiles();

let mostRecentFile = null;
let secondMostRecentFile = null;
let mostRecentTime = 0;
let secondMostRecentTime = 0;

files.forEach((file) => {
    const fileStat = app.vault.getAbstractFileByPath(file.path).stat;
    if (fileStat.mtime > mostRecentTime) {
        secondMostRecentTime = mostRecentTime;
        secondMostRecentFile = mostRecentFile;
        mostRecentTime = fileStat.mtime;
        mostRecentFile = file;
    } else if (fileStat.mtime > secondMostRecentTime) {
        secondMostRecentTime = fileStat.mtime;
        secondMostRecentFile = file;
    }
});

if (secondMostRecentFile) {
    const newFileName = `${secondMostRecentFile.basename}a`;
    await tp.file.rename(newFileName);

    const folderPath = secondMostRecentFile.path.split('/').slice(0, -1).join('/');
    const newFilePath = `${folderPath}/${newFileName}`;

    await tp.file.move(newFilePath);
    
} else {
    tR += `No suitable file found.`;
}
%>

<%*
const { app } = this;
const files = app.vault.getMarkdownFiles();

let mostRecentFile = null;
let secondMostRecentFile = null;
let mostRecentTime = 0;
let secondMostRecentTime = 0;

files.forEach((file) => {
    const fileStat = app.vault.getAbstractFileByPath(file.path).stat;
    if (fileStat.mtime > mostRecentTime) {
        secondMostRecentTime = mostRecentTime;
        secondMostRecentFile = mostRecentFile;
        mostRecentTime = fileStat.mtime;
        mostRecentFile = file;
    } else if (fileStat.mtime > secondMostRecentTime) {
        secondMostRecentTime = fileStat.mtime;
        secondMostRecentFile = file;
    }
});

if (secondMostRecentFile) {
    const newFileName = `${secondMostRecentFile.basename}a`;
    await tp.file.rename(newFileName);

    const folderPath = secondMostRecentFile.path.split('/').slice(0, -1).join('/');
    const newFilePath = `${folderPath}/${newFileName}`;

    await tp.file.move(newFilePath);
    
} else {
    tR += `No suitable file found.`;
}
%>

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