Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linkify breaks with URLs surrounded by color codes #63

Closed
milas opened this issue Mar 23, 2021 · 3 comments · Fixed by #75
Closed

Linkify breaks with URLs surrounded by color codes #63

milas opened this issue Mar 23, 2021 · 3 comments · Fixed by #75
Labels

Comments

@milas
Copy link

milas commented Mar 23, 2021

If you have a URL that's got color codes around it and linkify the output, you'll get a </span> in the link because the regex for URL (specifically the host/path portion) will match any non-whitespace character:

anser/lib/index.js

Lines 196 to 198 in fb58237

linkify (txt) {
return txt.replace(/(https?:\/\/[^\s]+)/gm, str => `<a href="${str}">${str}</a>`);
}

Example

(Same code, just inlined here)

var anser = require("anser")

const link = anser.linkify(anser.ansiToHtml(
    "\u001b[31mhttps://github.com/IonicaBizau/anser\u001b[0m"
))

console.log(link)

RunKit repro (same code as here): https://runkit.com/milas/605a270caf5fed001a4b6e93

Output

<span style="color:rgb(187, 0, 0)"><a href="https://github.com/IonicaBizau/anser</span>">https://github.com/IonicaBizau/anser</span></a>

Solution

If we assume the input has been escaped with escapeForHtml, perhaps linkify's regex could be made more strict to only match on URL safe characters? Just adding < to the not matching set would also probably be sufficient for this case, but there might be other edge cases re: linkifying in general (i.e. not induced by having a surrounding color code) that are still missed.

@IonicaBizau
Copy link
Owner

Hmm, good catch! I will happily merge a PR fixing this! ❇️

@aaronvg
Copy link

aaronvg commented Jan 26, 2024

any plan to fix this?

perlpunk added a commit to perlpunk/anser that referenced this issue Sep 11, 2024
Fixes IonicaBizau#63

It handles the fact that escapeForHtml has already been called and
matches `&amp;` separately.

I used the RFC (basically the last 5 rules) from:
https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
perlpunk added a commit to perlpunk/anser that referenced this issue Sep 11, 2024
Fixes IonicaBizau#63

It handles the fact that escapeForHtml has already been called and
matches `&amp;` separately.

I used the RFC (basically the last 5 rules) from:
https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
perlpunk added a commit to perlpunk/anser that referenced this issue Sep 11, 2024
Fixes IonicaBizau#63

It handles the fact that escapeForHtml has already been called and
matches `&amp;` separately.

I used the RFC (basically the last 5 rules) from:
https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
perlpunk added a commit to perlpunk/anser that referenced this issue Sep 11, 2024
Fixes IonicaBizau#63

It handles the fact that escapeForHtml has already been called and
matches `&amp;` separately.

I used the RFC (basically the last 5 rules) from:
https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
@perlpunk
Copy link
Contributor

perlpunk added a commit to perlpunk/anser that referenced this issue Sep 11, 2024
Fixes IonicaBizau#63

It handles the fact that escapeForHtml has already been called and
matches `&amp;` separately.

I used the RFC (basically the last 5 rules) from:
https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants