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: better handling of hyperlinks with parentheses #6391

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 5 additions & 3 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1666,9 +1666,11 @@ pub fn default_hyperlink_rules() -> Vec<hyperlink::Rule> {
hyperlink::Rule::with_highlight(r"\((\w+://\S+)\)", "$1", 1).unwrap(),
hyperlink::Rule::with_highlight(r"\[(\w+://\S+)\]", "$1", 1).unwrap(),
hyperlink::Rule::with_highlight(r"<(\w+://\S+)>", "$1", 1).unwrap(),
// Then handle URLs not wrapped in brackets
// and include terminating ), / or - characters, if any
hyperlink::Rule::new(r"\b\w+://\S+[)/a-zA-Z0-9-]+", "$0").unwrap(),
// Then handle URLs not wrapped in brackets that
// 1) have a balanced ending parenthesis or
hyperlink::Rule::new(hyperlink::CLOSING_PARENTHESIS_HYPERLINK_PATTERN, "$0").unwrap(),
// 2) include terminating _, / or - characters, if any
hyperlink::Rule::new(hyperlink::GENERIC_HYPERLINK_PATTERN, "$0").unwrap(),
// implicit mailto link
hyperlink::Rule::new(r"\b\w+@[\w-]+(\.[\w-]+)+\b", "mailto:$0").unwrap(),
]
Expand Down
50 changes: 50 additions & 0 deletions termwiz/src/hyperlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ impl<'t> Match<'t> {
result
}
}
pub const CLOSING_PARENTHESIS_HYPERLINK_PATTERN: &str =
r"\b\w+://[^\s()]*\(\S*\)(?=\s|$|[^_-/a-zA-Z0-9])";
pub const GENERIC_HYPERLINK_PATTERN: &str = r"\b\w+://\S+[_-/a-zA-Z0-9]";

impl Rule {
/// Construct a new rule. It may fail if the regex is invalid.
Expand Down Expand Up @@ -347,4 +350,51 @@ mod test {
]
);
}

#[test]
fn parse_with_parentheses() {
fn assert_helper(test_uri: &str, expected_uri: &str, msg: &str) {
let rules = vec![
Rule::new(CLOSING_PARENTHESIS_HYPERLINK_PATTERN, "$0").unwrap(),
Rule::new(GENERIC_HYPERLINK_PATTERN, "$0").unwrap(),
];

assert_eq!(
Rule::match_hyperlinks(test_uri, &rules)[0].link.uri,
expected_uri,
"{}",
msg,
);
}

assert_helper(
" http://example.com)",
"http://example.com",
"Unblanced terminating parenthesis should not be captured.",
);

assert_helper(
"http://example.com/(complete_parentheses)",
"http://example.com/(complete_parentheses)",
"Balanced terminating parenthesis should be captureed.",
);

assert_helper(
"http://example.com/(complete_parentheses)>",
"http://example.com/(complete_parentheses)",
"Non-URL characters after a balanced terminating parenthesis should be dropped.",
);

assert_helper(
"http://example.com/(complete_parentheses))",
"http://example.com/(complete_parentheses))",
"Non-terminating parentheses should not impact matching the entire URL - Terminated with )",
);

assert_helper(
"http://example.com/(complete_parentheses)-((-)-()-_-",
"http://example.com/(complete_parentheses)-((-)-()-_-",
"Non-terminating parentheses should not impact matching the entire URL - Terminated with a valid character",
);
}
}