Skip to content

Commit f947cef

Browse files
committed
Fix linkify to only match valid URI characters
Fixes IonicaBizau#63 It handles the fact that escapeForHtml has already been called and matches `&` separately. I used the RFC (basically the last 5 rules) from: https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
1 parent ac20b53 commit f947cef

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

DOCUMENTATION.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ be run prior to `ansiToHtml`.
2020
Adds the links in the HTML.
2121

2222
This replaces any links in the text with anchor tags that display the
23-
link. The links should have at least one whitespace character
24-
surrounding it. Also, you should apply this after you have run
23+
link. You should apply this after you have run
2524
`ansiToHtml` on the text.
2625

2726
#### Params

lib/index.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const ANSI_COLORS = [
2424
, { color: "255, 255, 255", "class": "ansi-bright-white" }
2525
]
2626
];
27+
const linkRegex = /(https?:\/\/(?:[A-Za-z0-9#;/?:@=+$',_.!~*()[\]-]|&|%[A-Ea-a0-9]{2})+)/gm;
2728

2829
class Anser {
2930

@@ -49,9 +50,7 @@ class Anser {
4950
* Adds the links in the HTML.
5051
*
5152
* This replaces any links in the text with anchor tags that display the
52-
* link. The links should have at least one whitespace character
53-
* surrounding it. Also, you should apply this after you have run
54-
* `ansiToHtml` on the text.
53+
* link. You should apply this after you have run `ansiToHtml` on the text.
5554
*
5655
* @name Anser.linkify
5756
* @function
@@ -196,7 +195,7 @@ class Anser {
196195
* @returns {String} The HTML output containing link elements.
197196
*/
198197
linkify (txt) {
199-
return txt.replace(/(https?:\/\/[^\s]+)/gm, str => `<a href="${str}">${str}</a>`);
198+
return txt.replace(linkRegex, str => `<a href="${str}">${str}</a>`);
200199
}
201200

202201
/**

0 commit comments

Comments
 (0)