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

Fix linkify to only match valid URI characters #75

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const ANSI_COLORS = [
, { color: "255, 255, 255", "class": "ansi-bright-white" }
]
];
// https://datatracker.ietf.org/doc/html/rfc3986#appendix-A
const linkRegex = /(https?:\/\/(?:[A-Za-z0-9#;/?:@=+$',_.!~*()[\]-]|&|%[A-Fa-f0-9]{2})+)/gm;

class Anser {

Expand All @@ -49,9 +51,7 @@ class Anser {
* Adds the links in the HTML.
*
* This replaces any links in the text with anchor tags that display the
* link. The links should have at least one whitespace character
* surrounding it. Also, you should apply this after you have run
* `ansiToHtml` on the text.
* link. You should apply this after you have run `ansiToHtml` on the text.
*
* @name Anser.linkify
* @function
Expand Down Expand Up @@ -196,7 +196,7 @@ class Anser {
* @returns {String} The HTML output containing link elements.
*/
linkify (txt) {
return txt.replace(/(https?:\/\/[^\s]+)/gm, str => `<a href="${str}">${str}</a>`);
return txt.replace(linkRegex, str => `<a href="${str}">${str}</a>`);
}

/**
Expand Down