Skip to content

Commit

Permalink
Avoid linkifying trailing question mark (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: fregante <me@fregante.com>
  • Loading branch information
karlhorky and fregante authored Nov 11, 2024
1 parent b6aec52 commit a90ea64
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ const parseValue = (value, href) => {

// Get `<a>` 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({
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Generated by [AVA](https://avajs.dev).

> Snapshot 3
'<a href="https://www.例.jp?">https://www.例.jp?</a>'
'<a href="https://www.例.jp">https://www.例.jp</a>?'

## linkifyUrlsToHtml: supports trailing period

Expand Down Expand Up @@ -312,7 +312,7 @@ Generated by [AVA](https://avajs.dev).

> Snapshot 3
'DocumentFragment: <a href="https://www.例.jp?">https://www.例.jp?</a>'
'DocumentFragment: <a href="https://www.例.jp">https://www.例.jp</a>?'

## linkifyUrlsToDom: supports trailing period

Expand Down
Binary file modified test.js.snap
Binary file not shown.

0 comments on commit a90ea64

Please sign in to comment.