noties / Markwon

Android markdown library (no WebView)
https://noties.io/Markwon/
Apache License 2.0
2.76k stars 313 forks source link

[Question] short URLs #383

Closed qwerty287 closed 2 years ago

qwerty287 commented 2 years ago

Thanks for the nice library! I habe a question, not necessarily related to only Markwon, but how can I short links/URLs? For example, GitHub is doing this with issue/PR links: https://github.com/user/repo/issue/123 becomes #123. Is there any way to do this? You could use an inline parser, but this requires a special char and giving a h (from https) or using regex to replace the links before parsing, doesn't seem to be a good idea. Is there any other way?

noties commented 2 years ago

Hello @qwerty287 ,

if you want to substitute some links with shorthand variants (like #123 on Github) pre-processing markdown input is a good idea. Creating an inline parser with h as a special character is not though 😄 . You can run your code (with regex or anything else) before sending to Markwon. Thus you can use markdown syntax and just create representation as you like.

For example, https://example.com/some_dude can be replaced in your raw markdown with [@some_dude](https://example.com/some_dude). And Markwon will take care of the rest (to display and handle clicks)

qwerty287 commented 2 years ago

Thanks! Yes, that's my way currently too. My only concern with it is that this will also replace links in code blocks, which I would like to prevent. Is this possible, with regex or another solution? Unfortunately, regex does not allow signs like * and + in lookbehinds, so I would have to use some regex like (\S*)https://example.com/(\S+)(\S*) with ``` lookbehinds/aheads) and substitute it by $1@$2$3?

noties commented 2 years ago

Yeah, exclude URLs from code blocks with regex might be almost impossible. Maybe you could use a similar to the autolink approach? It post-processes parsed markdown by inspecting Text nodes and replaces found links with proper Links

qwerty287 commented 2 years ago

This is working, thanks!