russross / blackfriday

Blackfriday: a markdown processor for Go
Other
5.44k stars 600 forks source link

Links with parenthesis (like wikipedia links) are not correct when using autolinks #291

Open ccampbell opened 8 years ago

ccampbell commented 8 years ago

For example if you have some markdown text with:

Check out this link:

https://en.wikipedia.org/wiki/St._Lucia_(musician)

The final markup ends up being

<p>Check out this link:</p>

<p><a href="https://en.wikipedia.org/wiki/St._Lucia_(musician" rel="nofollow">https://en.wikipedia.org/wiki/St._Lucia_(musician</a>)</p>

Notice the final closing parenthesis is not matched

rtfb commented 8 years ago

Upon reading this I was certain this is a duplicate, but after carefully reviewing the list of issues twice, I can't find the other report, weird.... Wherever the original is hiding, thanks for reporting!

ccampbell commented 8 years ago

I tried to find another ticket first, but I couldn't find one. I did find this in the tests:

https://github.com/russross/blackfriday/blob/93622da34e54fb6529bfb7c57e710f37a8d9cbd8/inline_test.go#L571

Looks like there are tests for when you format the link yourself, but not the autolink part

dmitshur commented 8 years ago

@rtfb, are these not it?

There's also this one that's vaguely related:

rtfb commented 8 years ago

Yup, they are. Didn't expect them to be fixed, so only looked among the open ones.

ccampbell commented 8 years ago

@rtfb those tickets are for when you manually put in the markdown for the URL. That is fixed and working correctly.

This ticket is for if you put the URL in your markdown without wrapping it in []()

guregu commented 1 year ago

For anyone reading this who is desperate for a workaround, you can use goquery to "fix" the links.

doc.Find("a[href*='(']:not([href$=')'])").Each(func(_ int, sel *goquery.Selection) {
    if len(sel.Nodes) == 0 {
        return
    }
    sibling := sel.Nodes[0].NextSibling
    if sibling == nil || sibling.Type != html.TextNode || !strings.HasPrefix(sibling.Data, ")") {
        return
    }
    href, _ := sel.Attr("href")
    fixed := href + ")"
    sel.SetAttr("href", fixed)
    if sel.Text() == href {
        sel.SetText(fixed)
        sibling.Data = strings.TrimPrefix(sibling.Data, ")")
    }
})