Templater script: traverse linked files

Hello,
I’m quite new to obsidian, but I’m really loving it. I’m using it to organize my stand-up comedy material, linking my jokes to one another.
In my repository I’ve saved all my jokes as individual files, and each file (i.e., a joke) has a bullet list of links called “Follow-ups” that links that joke to the one that should come afterward.

I’ve built a script with templater, to help me create set lists (an organized list of the jokes I’m planing to use during a show) that uses a suggester to show me all my jokes and allow me to pick them and add them as links to my set list file. I would like to improve it, limiting the options this suggester proposes me every time, to only files linked as “Follow-ups” (so basically any outgoing link in the file) in the last picked file.
Currently, the script just shows me the full list of files, removing the selected ones each time.

Can anyone help?

Thanks

<%*
function getTags(file) { const metadata = app.metadataCache.getFileCache(file); return metadata.tags ? metadata.tags.map(tag => tag.tag) : []; }

let allFiles =app.vault.getMarkdownFiles().filter(file => !file.path.match("/")); //Files in the root folder will not

let selectMore = true
let selectedFiles = []

while (selectMore) {
let choice = await tp.system.suggester((item) => `${item.basename} (Tags: ${getTags(item).join(' ')})`, allFiles, false, "[Select files (ESC when finished)] - " + selectedFiles.join(", "))
if (!choice) 
	{ 
	selectMore = false 
	} 
else 
	{ 
	selectedFiles.push(choice.basename)
	allFiles = allFiles.filter(file => !file.path.match(choice.basename))
	} 
}
selectedFiles.forEach((selectedFile) => {
	tR += '\n - [[' + selectedFile + ']]';
})
tR += "\n ----- \n"

_%>

I’ve partnered up with my friend ChatGPT and we came up with this code:

<%*
function getTags(file) { const metadata = app.metadataCache.getFileCache(file); return metadata.tags ? metadata.tags.map(tag => tag.tag) : []; }

function getOutgoingLinks(file) {
    const cache = app.metadataCache.getFileCache(file);
    console.log("Cache for file:", file.basename, cache);
    if (cache && cache.links) {
        return cache.links.map(link => link.link);
    } else {
        return [];
    }
}

let allFiles =app.vault.getMarkdownFiles().filter(file => !file.path.match("/")); //Files in the root folder will not
let allFilesLoop = allFiles;

let selectMore = true
let selectedFiles = []

while (selectMore) {
let choice = await tp.system.suggester((item) => `${item.basename} (Tags: ${getTags(item).join(' ')})`, allFilesLoop, false, "[Select files (ESC when finished)] - " + selectedFiles.join(", "))
if (!choice) 
	{ 
	selectMore = false 
	} 
else 
	{ 
	selectedFiles.push(choice.basename)
	let linkedFiles = getOutgoingLinks(choice);
	console.log("Outgoing links:", linkedFiles); 
	allFilesLoop = allFiles.filter(file => linkedFiles.includes(file.basename));
	} 
}
selectedFiles.forEach((selectedFile) => {
	tR += '\n - [[' + selectedFile + ']]';
})
tR += "\n ----- \n"

_%>

It’s working for my needs, and I hope it will be useful for someone else.
If you can spot any improvement, I’d be more than happy to hear about them.

Peace.

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