How would you do this? Text Parsing

I create Markdown Tables in Obsidian and often have a link to an Obsidian page as the content of one of the cells. I also frequently use Aliases to make the names more readable.

For example:

Start Time | Activity | Revision
------------ | ------------ | ----------
2:00 | [[foo-bar-notes|Foo Bar]]

To make the table parse correctly I need to escape the pipe between the file name and the Alias, so it ends up looking like this:

Start Time | Activity | Revision
------------ | ------------ | ----------
2:00 | [[foo-bar-notes\|Foo Bar]]

On the mac, how would you create an automation to look back in the text and add the \ before the |?

I do this dozens of times a day and I’d love to just push a key and magically it adds the \ but I’m not even sure where to start.

Thanks in advance for any help you can give.

The ideal option would be to develop an Obsidian plugin, but if you are at the point of asking the how, this is probably a step too far. Therefor you either want to grab the content, process it and put it back (e.g. Via the clipboard), or in the Markdown file - but then you would have to determine which file in order to process it.

The algorithm would probably be something like use regular expression matching to match the double square brackets containing the pipe but not preceded by a Slash, with the double square bracketed string being on the same line as starts and ends up ith pipes (should match a table row usually). Then replace such matches to include the Slash before the pipe I the double square brackets.

Regular expressions are your friend. Your choice of language may further suggest an approach, but if you have say Keyboard Maestro, that could offer you ways to trigger and run this in-app or file interactions.

Hope that helps.

OK, that’s a great start. I was thinking about using KM. Here’s where I got stuck.

I don’t know how to get KM to look back on the existing page and grab text.

Would it make sense for me to just copy the line to the clipboard, run the regex on the text on the clipboard and then paste it back? Or is there an easier way to access the text to run the regex?

I guess the other option would be to use keystrokes in KM to select all and copy, process the whole file at once then paste it back…

I agree that the plugin for Obsidian is a step too far for me at this point!

I appreciate your help.

I think rather than the line, process the whole text, and yes, select all, copy, process (regex replace)
and paste back the result. Key press activation is probably best, or maybe menu items if they behave well.

One thing I would note is that Keyboard Maestro supports named clipboard, so you can keep your system clipboard intact if you make use of those.

https://wiki.keyboardmaestro.com/Clipboards

For trying out your regex replace/substitution, I would always recommend regex101.

Thank you Sylumer. Very helpful as always.

Just as a follow-up, I finally got a chance to dig into regex 101 and I built this automation today and it seems to be working well.

Thanks again for your help @sylumer

Glad you got it working :sunglasses:

Maybe it would be worth sharing your overall solution in case others come across this thread?

Thanks for the prompt, after my initial success I found a problem that took me a while to debug.

The RegEx search that I ended up with is:

[{2}(.?)|(?<!\|)(.?)]{2}

Which looks for a | that’s contained within [[ ]] then checks to make sure that it’s not already escaped (that’s the ?<!\|).

Then replaces it with [[$1|$2]]

The whole KM script is:

Hope that helps someone else!

1 Like