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] + "]] ");
i++
}
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?