Skip to content

Commit 261a077

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 261a077

File tree

2 files changed

+5
-6
lines changed

2 files changed

+5
-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

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

2830
class Anser {
2931

@@ -49,9 +51,7 @@ class Anser {
4951
* Adds the links in the HTML.
5052
*
5153
* 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.
54+
* link. You should apply this after you have run `ansiToHtml` on the text.
5555
*
5656
* @name Anser.linkify
5757
* @function
@@ -196,7 +196,7 @@ class Anser {
196196
* @returns {String} The HTML output containing link elements.
197197
*/
198198
linkify (txt) {
199-
return txt.replace(/(https?:\/\/[^\s]+)/gm, str => `<a href="${str}">${str}</a>`);
199+
return txt.replace(linkRegex, str => `<a href="${str}">${str}</a>`);
200200
}
201201

202202
/**

0 commit comments

Comments
 (0)