diff --git a/index.js b/index.js index 3ff5b83..05e0a00 100644 --- a/index.js +++ b/index.js @@ -20,16 +20,25 @@ const parseValue = (value, href) => { }; // Get `` element as string -const linkify = (href, options = {}) => createHtmlElement({ - name: 'a', - // First `href` is needed for the `href` attribute to be the first attribute on the `a` tag - attributes: { - href, - ...options.attributes, - href, // eslint-disable-line no-dupe-keys -- Ensures it's not overwritten - }, - ...parseValue(options.value, href), -}); +function linkify(href, options = {}) { + // The regex URL mistakenly includes a dot at the end of the URL + let trailingDot = ''; + if (href.endsWith('.')) { + href = href.slice(0, -1); + trailingDot = '.'; + } + + return createHtmlElement({ + name: 'a', + // First `href` is needed for the `href` attribute to be the first attribute on the `a` tag + attributes: { + href, + ...options.attributes, + href, // eslint-disable-line no-dupe-keys -- Ensures it's not overwritten + }, + ...parseValue(options.value, href), + }) + trailingDot; +} // Get DOM node from HTML const domify = html => document.createRange().createContextualFragment(html); diff --git a/test.js b/test.js index f83ae74..9d67328 100644 --- a/test.js +++ b/test.js @@ -147,8 +147,8 @@ for (const [name, linkify] of Object.entries({ t.snapshot(linkify('https://github.com/scarf005/hangul-test/wiki/한글-위키-페이지 and other hangul')); t.snapshot(linkify('https://www.例.jp?')); }); -} -test.failing('skips the trailing period', t => { - t.is(linkifyUrlsToHtml('Visit https://fregante.com.'), 'Visit https://fregante.com.'); -}); + test(name + ': supports trailing period', t => { + t.snapshot(linkify('Visit https://fregante.com.')); + }); +} diff --git a/test.js.md b/test.js.md index f2b5ebc..f8883ec 100644 --- a/test.js.md +++ b/test.js.md @@ -156,6 +156,12 @@ Generated by [AVA](https://avajs.dev). 'https://www.例.jp?' +## linkifyUrlsToHtml: supports trailing period + +> Snapshot 1 + + 'Visit https://fregante.com.' + ## linkifyUrlsToDom: main > Snapshot 1 @@ -307,3 +313,9 @@ Generated by [AVA](https://avajs.dev). > Snapshot 3 'DocumentFragment: https://www.例.jp?' + +## linkifyUrlsToDom: supports trailing period + +> Snapshot 1 + + 'DocumentFragment: Visit https://fregante.com.' diff --git a/test.js.snap b/test.js.snap index 427ae74..c78d873 100644 Binary files a/test.js.snap and b/test.js.snap differ