gregjacobs / Autolinker.js

Utility to Automatically Link URLs, Email Addresses, Phone Numbers, Twitter handles, and Hashtags in a given block of text/HTML
MIT License
1.48k stars 238 forks source link

Bug: getUrl method not working correctly for links in markdown formats #383

Closed saurabhLearns closed 1 year ago

saurabhLearns commented 1 year ago

Hey,

I've been messing with autolinker and found this strange behavior (not sure if this is expected or not, but looks like a bug in first place)

Adding a small snippet which is already tried and tested on runkit as well.

let autolinker = require("autolinker");

let inputText = 'If I add this sample link which is in the markdown format [https://example.com/productdetails.aspx?productid=9999](https://example.com/productdetails.aspx?productid=9999), getUrl method does not correctly works. but if I do this same with this [https://linkedin.com](https://linkedin.com) it works perfectly)'

autolinker.link(inputText, {
      replaceFn: (match) => {
        if (match.getType() == 'url') {
          const matchedUrl = match.getUrl();
          console.log(matchedUrl)
        }
      }
    });

Expected Output

https://example.com/productdetails.aspx?productid=9999
https://linkedin.com

Actual Output

https://example.com/productdetails.aspx?productid=9999](https://example.com/productdetails.aspx?productid=9999)
https://linkedin.com

Snippet from runkit:

Screenshot 2022-08-08 at 3 10 37 PM
gregjacobs commented 1 year ago

Hey @saurabhLearns, so Autolinker doesn't support markdown format. You should compile your markdown into HTML first before sending it to Autolinker (in which Autolinker does support HTML).

That being said, the behavior is expected. ] and ( characters are acceptable in the "path" section of a URL (the section after a / char), whereas they are not acceptable in the "domain" (host) section. This is why when you have a path (the example.com/... url), the ]( chars are included. But when you don't have a path (the linkedin URL), the ]( characters are excluded.

Bottom line: compile markdown -> HTML first :)