Going bananas: trying to do a search and replace in Markdown

Hello, there is a bug printing highlighted text in Bear Notes (Multimarkdown) via Marked 2.

What I need to do is very simple :

search for all == in the Bear Markdown note and replace with =<mark>= 
For example, ==this is a test== becomes  =<mark>=this is a test=<mark>=

None of the search and replace work, including the KM search and replace string and regex and neither does BBEdit Text Factory.

I suspect that the problem is due to the fact that all these actions work on the clipboard and convert to simple text instead of Markdown in the process.

Note that in Bear, I am able to copy as markdown as depicted in the macro

thank you very much

Conversion Bear to Marked.kmmacros (24.8 KB)
.

I don’t have Bear, but here are a couple of observations.

  1. First and foremost, you’re double-pasting things.
    • You have an insert text by pasting action immediately followed by a paste action... so you’re pasting twice... and the contents are different because the first one is pasting from a variable that has been modified, and the second is pasting the system clipboard which is likely the original content you copied from Bear.
  2. At the beginning of the macro, do you need to copy as markdown?
    • Can you just copy it normally and go from there?

Here’s what I would try ↓

Download Macro(s): Conversion Bear to Marked.kmmacros (2.5 KB)

Macro Image (Click to expand/collapse)

1 Like

thank you for your post Chris @cdthomer

I made 2 mistakes

  • extra paste action which I removed, but did not change the outcome
  • I did not express myself clearly.

I spent the day yesterday trying to resolve this issue. My first attempt, in the first minute, was to write a simple search and replace macro identical identical to yours.
You can see below the outcome of your macro (1) and my macro above from my initial post (2) (after removing the extra paste at the bottom). Using search replace with regex instead of string gives the same result.

After a few hours, I came to the conclusion that the problem lies in the fact that because we are dealing with a note in multimarkdown, the whole process (macro from start to end) must be in markdown. Neither your nor my macro work because somewhere along the line, there is a conversion from markdown to text (copy paste in your macro, and probably the KM action in my macro either the variable creation or the paste variable command).

The reason I say that is because if I carry out the search and replace manually using Bear's native search and replace function (as illustrated in (3)), it works fine, but it's tedious and not a solution for the long run.

At least this insight allowed me to preserve the markdown format by using the Bear native copy as markdown menu item.

The challenge is to remain in Markdown format after that.

What's confusing about this issue is that markdown is text, so formats shouldn't have any impact. Markdown is simply text markers around sections of text that you want interpreted differently by a Markdown interpreter. But the source itself is 100% text, and just text.

I installed the free version of Bear, and this much-simplified macro works:

Conversion Bear to Marked - v2.kmmacros (26 KB)

I didn't bother copying as Markdown, as I have no idea what that does to formatting. But copying just the text captures the Markdown markers, because as noted above, they're just text.

I then do the search and replace of the clipboard, save it to a variable, then paste that variable back, replacing the selected text in Bear.

-rob.

1 Like

incredible !! it works perfectly, and everything you say about text being text works perfectly. It's extremely kind of you to have gone to the trouble of downloading Bear.

Thanks very much !

Out of intellectual curiosity, to better understand KM, would you have the patience to explain why this simple variant on your macro does not work ?

Why does the solution require us to take the variable route. ?

I'm not sure, but I suspect there's something about replacing text on the clipboard: I know the clipboard supports many forms of text at once, so maybe it's trying to search one of the non-text forms? It probably depends on what Bear provides when you copy.

But by putting it in a variable, which only supports pure text, you get just the text version.

This is all just a semi-educated guess, though; I have no idea if it's really correct.

-rob.

1 Like

OK. thanks VERY much.