Skip to content
This repository has been archived by the owner on Nov 13, 2022. It is now read-only.

Render bare URLs as hyperlinks #326

Closed
drscotthawley opened this issue Jun 15, 2020 · 5 comments
Closed

Render bare URLs as hyperlinks #326

drscotthawley opened this issue Jun 15, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@drscotthawley
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I'm always frustrated when I add bare URLs in my Markdown editor and see them rendered as hyperlinks, but then when fastpages compiles them they are rendered on the blog as merely plain text with no opportunity to click on them. For example typing "https://www.google.com" appears as a clickable link as I'm editing the blog...

(^^ btw see how GitHub automatically rendered that bare URL as a clickable link? That's what I'm used to and that's what I'd like.)

...then I have to go back and manually either add angle-brackets around all the URLs, or add some kind of square-brackets-parentheses construction like [https://www.google.com](https://www.google.com)

Describe the solution you'd like
That any properly-formatted bare URL gets rendered as a clickable link, i.e. "blue with an underline".

Describe alternatives you've considered
As I mentioned, manually going back and reformatting the URLs.

Additional context
As mentioned, even GitHub Issues formats bare URLs as hyperlinks by default. As do many Markdown viewers.

@drscotthawley drscotthawley added the enhancement New feature or request label Jun 15, 2020
@github-actions
Copy link

Thank you for opening an issue. If this issue is related to a bug, please follow the steps and provide the information outlined in the Troubleshooting Guide. Failure to follow these instructions may result in automatic closing of this issue.

@drscotthawley
Copy link
Contributor Author

drscotthawley commented Jun 15, 2020

Seems this is a "known issue" with Jekyll. I found multiple feature requests of this sort around the internet for various markdown-based apps. Kramdown in particular does not support this.
I'm looking into either writing a little script to auto-add angle brackets around bare URLs, or a Jekyll plugin of some sort.

Some implementations support GitHub Flavored Markdown via markdown: GFM instead of markdown: kramdown. But that is not supported in fastpages; only kramdown is allowed. And yet it seems that by default we have in _config.yml:

highlighter: rouge
markdown: kramdown
kramdown:
  math_engine: katex
  input: GFM

So "GFM" is specified, but Kramdown's GFM mode is not actually GFM-compliant.

The alternative of specifying

markdown: CommonMarkGhPages
commonmark:
   extensions:
   - autolink

is not supported in FastPages.

Posted a question on this on StackOverflow.

@drscotthawley
Copy link
Contributor Author

drscotthawley commented Jun 21, 2020

I have a solution, thanks to "@mandy8055" on SO. This will add angle brackets around bare URLs and leave other links (HTML or regular Markdown) alone. I have tested it with some rather complicated URLs and found it to be compliant.

Currently this is set up to modify the file in place:

perl -i.orig  -pe 's/(?!>)^(https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*))(?!>)$/<$1>/g' markdown_file.md

where markdown_file.md is the post. Or you could run it with "*.md" on the end instead and do all the files in a directory one at a time.

If you want to see what the output will be first without overwriting the file, you can remove the "-i.orig" in the perl command, and it will just print to stdout.

Whether this gets integrated in everyone's fastpages is, totally up to you. The perl dependence may not be desirable. I tried to use sed instead of perl, but I couldn't get the former to accept the regex even after trying various command-line flags. Whereas with perl it just-worked.

There's a chance that adding this to the fastpages build would break something else somewhere, somehow, whereas users could easily just run this command manually if they want. So, might just leave this here as "handy tip".

(I have yet to test this in Jupyter notebooks. )

@drscotthawley
Copy link
Contributor Author

perl is arguably more powerful, but in case that's regarded as overkill, @mandy8055 also came up with a sed version that works for all the cases I tried:

sed -i -E 's@^[^<]?(https?://(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&/=]*))[^>]?$@<\1>@gm' markdown_post.md

@hamelsmu
Copy link
Member

I'll go ahead and close this as you have found a workaround. Thanks for documenting your approach so that other people can find this

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants