Templater: navigation links between notes

I have a small script for Templater plugin that generates links between documents in the same directory, excluding the note with the same name as the containing folder, for use with the Waypoint plugin. With a keyboard shortcut, it works great, it works for Publish too. I don’t know if it’s the best solution and I’m sure there will be better ones, but it works great for me!

<%*
const currentFile = this.app.workspace.getActiveFile();
const currentFolderPath = currentFile.parent.path;
const folderName = currentFile.parent.name;
const files = this.app.vault.getMarkdownFiles().filter(file => 
    file.parent.path === currentFolderPath && 
    file.basename !== folderName); // Excluir el archivo con el mismo nombre que la carpeta

// Ordenar archivos por nombre
files.sort((a, b) => a.basename.localeCompare(b.basename));

const currentFileIndex = files.findIndex(file => file.path === currentFile.path);
const nextFile = (currentFileIndex + 1 < files.length) ? files[currentFileIndex + 1] : null;
const previousFile = (currentFileIndex - 1 >= 0) ? files[currentFileIndex - 1] : null;

if (previousFile) {
    const previousFileName = previousFile.basename;
    tR += `[[${previousFileName}]] ← Precedente\n`;
} else {
    tR += `Inicio de la serie\n`;
}

if (nextFile) {
    const nextFileName = nextFile.basename;
    tR += `Siguiente → [[${nextFileName}]]`;
} else {
    tR += `Fin de la serie`;
}
%>

In this page there is a version without Waypoint support:
Templater - Navigation links between notes - Marco Noris — lab

1 Like