HermanMartinus / bearblog

Free, no-nonsense, super fast blogging.
MIT License
2.82k stars 84 forks source link

Markdown converter missformats parenthesis in hyperlinks #288

Closed andrei-akopian closed 5 months ago

andrei-akopian commented 5 months ago

If a link contains parenthesis, like many wikipedia articles do such as https://en.wikipedia.org/wiki/Python_(programming_language). If I try to write a hyperlink in markdown using the [ text ] ( link ) notation. Most markdown editors (including Github Issues) will create: Python

However bearblog disregards parenthesis that belong to the link itself:

Notice the extra parenthesis) and the embedded link is missing a parenthesis and will thus redirect to a "Page Not Found."

I assume the underlying code of the bearblog converter does the following:

  1. Find the opening parenthesis of a hyperlink
  2. Find the closest closing parenthesis

But there should be a step in between there: account for any opening parenthesis in the link.

Now you could create a debate about whether to change the hyperlink converter:

Arguments for Leaving as Is Arguments for Changing
It is simple. KISS It is the standard everywhere else.
Few links have only one parenthesis
Many links have both parenthesis
It is a simple fix with a \) It is a simple fix with a \(
It is already there and will break old blog posts Compatible with other markdown editors (which users might use for drafts)
HermanMartinus commented 5 months ago

Oof, yeah. I see the issue you're describing.

Bear uses Mistune under the hood for markdown rendering, and this seems to be a limitation with it. I'll see if I can do something about it on the renderer wrapper, otherwise I may have to dig into Mistune itself.

andrei-akopian commented 5 months ago

I found the bug report on Mistune

https://github.com/lepture/mistune/issues/355

Their entire bug tracker is discussing odd formatting behavior and what to do with it. If you add a workaround right now, you will have to remove it one Mistuned fixes stuff on their end. Up to you.

andrei-akopian commented 5 months ago

Another "solution" I found is to replace parentheses in URLs with their encodings ( = %28 and ) = %29. The URLs still work at least for Wikipedia. Maybe it could save Markdown from standardization issues...

HermanMartinus commented 5 months ago

I've pushed a fix. It pre-processes markdown links with parentheses and escapes them before going through the renderer. It's not idea, but it work. Let me know if you run into any issues with it, otherwise you can close the issue if you're happy.

HermanMartinus commented 5 months ago

Ah, just seeing your comment now. You can I came to the same solution :)

HermanMartinus commented 5 months ago

Also, this will still work even if mistune fixes the issue since it's a pre-processing step. Once they fix the issue I can go remove it, but it won't break anything.

andrei-akopian commented 5 months ago

Thanks, are you going to close the issue now?

Also, I think you left a print statement in your commit.

andrei-akopian commented 4 months ago

Update: Mistune decided not to fix it in favor of supporting "traditional markdown". The parentheses support was supposedly normalized by some other library.

https://github.com/lepture/mistune/issues/355#issuecomment-2230920270

Their "solution" is have the user wrap the link in angle brackets. ([Example](<https://example.com/(Example)>))

Idk, odd decision that leaves more trouble than it would fix, but whatever.