jon-heard / obsidian-inline-scripts

Obsidian plugin: Type text shortcuts that expand into javascript generated text.
MIT License
134 stars 4 forks source link

Recursive expansion of shortcuts #12

Closed MohrJonas closed 2 years ago

MohrJonas commented 2 years ago

Let's say I have two shortcuts, with the prefix ;; and the suffix **. If I now do something like this: ;;some text ;;some more text****, the internal shortcut is expanded: ;;some text expanded**, but the second one isn't. Having this as a feature would be immensely useful. Cheers. (BTW: Sorry for all the issues, I'm currently kinda “stress-testing” the plugin 😁)

jon-heard commented 2 years ago

It's fine that your bringing up many issues. It's nice to know that the plugin is being used.

I'm somewhat confused about the circumstance you are describing. Is "some text" just any old shortcut in this example? Is it tied to "some more text" in some way?

Are you suggesting, for example, ;;set firstname Jon ;;set lastname Smith** as syntactic sugar for ;;set firstname Jon ;;set lastname Smith**?

It might help me to see an example showing how this could be useful.

In case it's relevant to you: you can call shortcuts from within shortcuts. For example:

~~ ^name$ ~~ return expand("firstname") + " " + expand("lastname"); `~

MohrJonas commented 2 years ago

Alright, let me try to explain it a bit better: Assume I have a shortcut a that converts Hello World to Bye World and another shortcut b that converts dlroW to World. If I now enter ;;Hello ;;dlroW****, I would assume the dlroW to be first converted to World (using shortcut b), converting the text to Hello World and then, using shortcut a convert it to Bye World. Hope this explains it a bit better. Cheers

jon-heard commented 2 years ago

Ah. That makes sense now. I am glad to see that you are using the plugin in ways I hadn't anticipated. In this case, I can see the drawback to the prefix and postfix I chose (i.e. that they use the same character). I'd still prefer the second character to be as easy to type as possible, like apostrophe, or maybe period. I'm going to think on this.

Anyway, I've tried to reproduce the scenario you've described and didn't encounter an obvious issue. Here's a rundown of what I did. Let me know if and where your having problems with this.

First, the shortcut-file. I changed the "Hello world" shortcut to make the expansion a bit more obvious:

~~ ^dlrow$ ~~ return "world"; ~~ ~~ ^hello world$ ~~ return "goodbye sun"; ~~

Then I changed the prefix & postfix to ;; and **, respectively.

Then I typed ;;hello ;;dlrow, followed by four asterisks.

The resulting expansion was:

goodbye sun

TEJS_nestedShortcuts

MohrJonas commented 2 years ago

That's interesting. With the shortcuts you provided, it does work. But when I try it with more complex a shortcuts it breaks: Helper script:

function ensureIsMath() {
    if ( ( (expansionInfo.line.substring(0, expansionInfo.inputStart).match(/\$/g)?.length||0) + 1 ) % 2 ) {
        expansionInfo.cancel = true;
    }
}

Shortcut a: ^set ((?:\S+\ ?)+)$

ensureIsMath()
const setElements = $1.split(" ")
return `${setElements.shift()}=\\{${setElements.join(", ")}\\}`

Shortcut b: ^(.+) over (.+)$

ensureIsMath()
return `\\frac{${$1}}{${$2}}`

rec

MohrJonas commented 2 years ago

Okay, I actually found out what the problem was. As you can see in the video above, the second pair of asterisks at the end was autocompleted by obsidian, causing the expansion to not be triggered. Using symbols that are autocompleted by Obsidian because they have a meaning is markdown (** is bold) is probably not the best idea. Maybe adding some sort of warning on the settings page might be a good idea 🤔.

jon-heard commented 2 years ago

Good catch. It turns out that autocomplete is a problem for key entry recognition in Obsidian's new editor, but not its old one. I'll add a warning. Better yet, I'll prevent the user from selecting a prefix/suffix pair that will trigger this issue.

Sorry for this inconvenience.

By the way, I don't know if you saw, but I set up a discussion for a new default prefix/suffix here. Feel free to chime in if you wish.