Path encoding on callback URL?

I’m creating an action to send text to Obsidian via the URL scheme. I have everything working fine except the parameter in the URL scheme that takes the “path” for the file.

The proper coding looks like:

path%2Fto%2Fthe%2Ffile%2Ftitle

However, when I try to put that in the addParameter field, it doesn’t encode the slashes properly. It leaves them as slashes. For whatever reason, this causes the note to be created in Obsidian, but not in the specified folder.

I have tried many ways to send the correct information to Obsidian, but if I use the %2F encoding, Drafts re-encodes the percent sign, and if I type slashes in the path value, Drafts doesn’t encode them. Any way around this?

Can you share the details of how you are doing this as the action? We can then take a look to determine if it is a data/action onfig/device/app issue. Otherwise we’re guessing and I can think or maybe two of three ways at least that you could have something roughly set up.

let title = draft.getTemplateTag("ptitle");

let date = Date.today().toString("yyyy-MM-dd");

let header = `---
date: ${date}
---
`

let content = header + draft.content


const baseURL = "obsidian://new?"

var cb = CallbackURL.create();
cb.baseURL = baseURL;
cb.addParameter("vault","obsidian");
cb.addParameter("file","60 Practice/63 Journal/01 Journal/"+title);
cb.addParameter("content",content);
cb.waitForResponse = false;
console.log(cb.url);
cb.open();

here’s the relevant console bit:

obsidian://new?file=60%20Practice/63%20Journal/01%20Journal/&content=testing&vault=obsidian

which I now see isn’t quite right–it’s not grabbing the “title” variable in the file parameter.

ok, so the real question isn’t about the slashes at all. How do I add the title to the end of the path? THat’s where things are getting tripped up.

You can construct the parameters yourself and put them in the baseURL property if you have special requirements. Like:

const baseURL = "obsidian://new?file=" + encodeURLComponent("my/path/");

This is one of those encoding bits not all libraries agree on. CallbackURL is using Apple’s URLQueryItem to process and encode queries parameters, and it does not encode the / (which most consider should be unencoded per RFC.

JavaScript does encode it…which is likely what Obsidian is using under the hood to decode the values.

Ok thanks. I think maybe we should delete this thread because my error had nothing to do with the path. Wasn’t getting the title right from the prompt, so it’s all fixed now. The slashes are fine!

2 Likes

You could rename the thread - if you like.