Fix trailing periods for naked URLs (#1175)

Fixes https://github.com/silverbulletmd/silverbullet/issues/1160
pull/1183/head
Ohad Lutzky 2024-12-14 08:19:25 +00:00 committed by GitHub
parent a400f0fc20
commit 3e153a525c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 1 deletions

View File

@ -216,3 +216,26 @@ Deno.test("Test hashtag helper functions", () => {
// should behave like this for all characters in tagRegex // should behave like this for all characters in tagRegex
assertEquals(renderHashtag("exclamation!"), "#<exclamation!>"); assertEquals(renderHashtag("exclamation!"), "#<exclamation!>");
}); });
const nakedURLSample = `
http://abc.com is a URL
Also http://no-trailing-period.com. That's a URL. It ends with m, not '.'.
http://no-trailing-comma.com, that same a URL, ends with m (and not ',').
http://trailing-slash.com/. That ends with '/' (still not '.').
http://abc.com?e=2.71,pi=3.14 is a URL too.
http://abc.com?e=2.71. That is a URL, which ends with 1 (and not '.').
`;
Deno.test("Test NakedURL parser", () => {
const tree = parseMarkdown(nakedURLSample);
const urls = collectNodesOfType(tree, "NakedURL");
assertEquals(urls.map((x) => x.children![0].text), [
"http://abc.com",
"http://no-trailing-period.com",
"http://no-trailing-comma.com",
"http://trailing-slash.com/",
"http://abc.com?e=2.71,pi=3.14",
"http://abc.com?e=2.71",
]);
});

View File

@ -577,7 +577,7 @@ const NakedURL = regexParser(
{ {
firstCharCode: 104, // h firstCharCode: 104, // h
regex: regex:
/^https?:\/\/[-a-zA-Z0-9@:%._\+~#=,;]{1,256}([-a-zA-Z0-9()@:%_\+.,;~#?&=\/]*)/, /(^https?:\/\/([-a-zA-Z0-9@:%_\+~#=]|(?:[.](?!(\s|$)))){1,256})(([-a-zA-Z0-9(@:%_\+~#?&=\/]|(?:[.,:;)](?!(\s|$))))*)/,
nodeType: "NakedURL", nodeType: "NakedURL",
className: "sb-naked-url", className: "sb-naked-url",
tag: NakedURLTag, tag: NakedURLTag,