From a90ea64ad0bd164efa94172e90219e4edc328c4c Mon Sep 17 00:00:00 2001 From: Karl Horky Date: Mon, 11 Nov 2024 16:14:46 +0100 Subject: [PATCH] Avoid linkifying trailing question mark (#50) Co-authored-by: fregante --- index.js | 9 ++++----- test.js.md | 4 ++-- test.js.snap | Bin 1546 -> 1553 bytes 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 05e0a00..3a2d396 100644 --- a/index.js +++ b/index.js @@ -21,11 +21,10 @@ const parseValue = (value, href) => { // Get `` element as string function linkify(href, options = {}) { - // The regex URL mistakenly includes a dot at the end of the URL - let trailingDot = ''; - if (href.endsWith('.')) { + // The URL regex mistakenly includes punctuation (a period or question mark) at the end of the URL + const punctuation = /[.?]$/.exec(href)?.[0] ?? ''; + if (punctuation) { href = href.slice(0, -1); - trailingDot = '.'; } return createHtmlElement({ @@ -37,7 +36,7 @@ function linkify(href, options = {}) { href, // eslint-disable-line no-dupe-keys -- Ensures it's not overwritten }, ...parseValue(options.value, href), - }) + trailingDot; + }) + punctuation; } // Get DOM node from HTML diff --git a/test.js.md b/test.js.md index f8883ec..9a7d397 100644 --- a/test.js.md +++ b/test.js.md @@ -154,7 +154,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 3 - 'https://www.例.jp?' + 'https://www.例.jp?' ## linkifyUrlsToHtml: supports trailing period @@ -312,7 +312,7 @@ Generated by [AVA](https://avajs.dev). > Snapshot 3 - 'DocumentFragment: https://www.例.jp?' + 'DocumentFragment: https://www.例.jp?' ## linkifyUrlsToDom: supports trailing period diff --git a/test.js.snap b/test.js.snap index c78d873ed69d1d64f6341a3a60aa9ee9793a8276..2e97f63046cb282286a2043822aa90280baf020f 100644 GIT binary patch literal 1553 zcmV+s2JZPmRzV4d<4}A+1s-ut1v& zyS8Oi;~$F%00000000B+nN4pSMHt5&5JE!Y00&MSCQj6(No*&jsnk#>X;PJ!RB9m! zr3$4U?~d2gtap~3**Fm#(w3J45+^`Cw1-v*I8gP1M8%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