fix: better handling of hyperlinks with parentheses

This commit is contained in:
Bowei Zhang authored and Wez Furlong committed 2025-02-08 14:14:28 -07:00
1 parent c3cfaa8458
commit dd7ae90f1f
2 files changed
+55 -3

No files matched your search

+5 -3
View File
@@ -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(),
]
+50
View File
@@ -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.
@@ -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",
);
}
}