vmg / redcarpet

The safe Markdown parser, reloaded.
MIT License
4.99k stars 527 forks source link

Autolink breaks on underscores #427

Closed jacobat closed 4 years ago

jacobat commented 10 years ago

When converting markdown with urls containing underscores they need to be escaped, unless using autolinking in which case they must not be escaped:

irb(main):001:0> Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: false).render("http://google.com/foo\\_bar\\_baz")
=> "<p>http://google.com/foo_bar_baz</p>\n"
irb(main):002:0> Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true).render("http://google.com/foo\\_bar\\_baz")
=> "<p><a href=\"http://google.com/foo%5C_bar%5C_baz\">http://google.com/foo\\_bar\\_baz</a></p>\n"

If one avoids escaping underscores in URL's it works with autolinking, but not without:

irb(main):003:0> Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: false).render("http://google.com/foo_bar_baz")
=> "<p>http://google.com/foo<em>bar</em>baz</p>\n"
irb(main):004:0> Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true).render("http://google.com/foo_bar_baz")
=> "<p><a href=\"http://google.com/foo_bar_baz\">http://google.com/foo_bar_baz</a></p>\n"

I believe the right solution is to escape underscores in URL's and change the autolinking to work with this.

jacobat commented 10 years ago

This is possibly related to #402