Templater script having issues after making changes from JS

I wrote a script today to finish up my chess workflow in obsidian in JS today, but after looking up how to convert that into something that templater can use (how-to-use-templater-js-scripts - shabeblog) and applying what I learned there, I’m running into issues. Here is the code I have so far

async function chessgametomd(tp){

    const pgn = await tp.system.prompt("PGN");
    const fen = await tp.system.prompt("FEN Strings", "", true, true);

    //Turn input into arrays
    var fenmod = fen.split(/\r?\n/);
    var pgnmod = pgn.split(" ");

    //Get rid of move # labels
    var pgnmoves = pgnmod.filter(checkMove)

    function checkMove(str) {
        return str.includes(".") === false

    //Combining the arrays into a single array of key value pairs
    var i;
    var currentKey;
    var currentVal;

    var result = {};

    for (i = 0; i < pgnmoves.length; i++) {
        currentKey = fenmod[i];
        currentVal = pgnmoves[i];
        result[currentKey] = currentVal;    

    //Turning it all back into a string in the correct order with correct markdown syntax
    var md = ""
    var i = 2

    for (var key of Object.keys(result)) {
        if(i % 2 === 0){
          md += (i/2 + ". ")
        md += ("[[" + key + "|" + result[key] + "]] ");

module.exports = chessgametomd

the only things that got changed are the first two dummy strings got switched to a tp.system.prompt(), the whole script got put inside of a function, and I added that function as an export.

The thing I read said to call it via this:

<%* tp.user.chessgametomarkdown(tp) %>

Which seems to work, but now I am getting an error in the obsidian console:

plugin:templater-obsidian:61 Templater Error: Template parsing error, aborting.
Unexpected token ‘)’
log_error @ plugin:templater-obsidian:61
errorWrapper @ plugin:templater-obsidian:81
await in errorWrapper (async)
append_template_to_active_file @ plugin:templater-obsidian:3700
onChooseItem @ plugin:templater-obsidian:2391
t.onChooseSuggestion @ app.js:1
t.selectSuggestion @ app.js:1
e.useSelectedItem @ app.js:1
(anonymous) @ app.js:1
e.handleKey @ app.js:1
e.onKeyEvent @ app.js:1

I only just started learning javascript so I feel a little lost at the moment.

Thanks in advance to this great community for the help!

Edit: Also, upon reading this over I’m realizing that this function might not even have an output? I just want the string that the variable md holds. How would I even tell templater that that should be the output?

You’re having a mismatch on the braces. There is a least one } missing near the end, after the for-loop.

You might have other issues, but that’s for sure causing some of your grief.


1 Like

Lmao I kinda figured it’d be something dumb like that. I’ll give that a shot and see if it works

I also misnamed the calling of the function, but after that fix it works!!! Thank you!

I spoke too soon though haha. I was correct that I does not have an output. I’m not entirely sure how to fix that. I tried adding in return md but that didn’t help

I’m not sure if this forum is the best for javascript issues, but we don’t know the current state of your code, so it’s hard to guess what you should change.

In addition I, for one, don’t know what kind of input you provide when running the script, and what output you expect from that input, which also makes it a lot harder to provide valuable input to your issue(s).


I was going to agree with you that this is more of a JS issue, but when I use console.log(md), the string shows up in the console exactly as expected. When I use return however, nothing shows up. All the examples I can find show that as the way to output from a teplater’s script function. I’m not sure whats going on when templater calls the function for it to not work.

For a Templater JS script launched with <%*, you need to append your output to the tR variable.

tR += 'bob' will output ‘bob’ in your note.

The docs are here - Templater Execution Command.

Yeah I’ve been going back to that documentation a lot but honestly I find it kind of confusing with the way it shows examples. So if I just want tR to be the string that the function returns, what do I do? I tried

tR += tp.user.chessgametomd(tp)

which resulted in

[object Promise]

as opposed to the long string that I was expecting. I’m thinking maybe it has something to do with me needing to add await somewhere.

Again, appologies for my ignorance. I have very little experience with this. Thanks for being patient with me.

I figured it out!! Thanks for the help!! This is what worked for me

<%* const exe_val = await tp.user.chessgametomd(tp) %>
<%* tR += exe_val %>

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