Skip to content

Commit

Permalink
Fix trailing periods for naked URLs (#1175)
Browse files Browse the repository at this point in the history
Fixes #1160
  • Loading branch information
lutzky authored Dec 14, 2024
1 parent a400f0f commit 3e153a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions common/markdown_parser/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,26 @@ Deno.test("Test hashtag helper functions", () => {
// should behave like this for all characters in tagRegex
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",
]);
});
2 changes: 1 addition & 1 deletion common/markdown_parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ const NakedURL = regexParser(
{
firstCharCode: 104, // h
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",
className: "sb-naked-url",
tag: NakedURLTag,
Expand Down

0 comments on commit 3e153a5

Please sign in to comment.