Skip to content

Commit

Permalink
Support trailing dot
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Nov 7, 2024
1 parent 7181a05 commit 67fd9d7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
29 changes: 19 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@ const parseValue = (value, href) => {
};

// Get `<a>` 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);
Expand Down
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://fregante.com">https://fregante.com</a>.');
});
test(name + ': supports trailing period', t => {
t.snapshot(linkify('Visit https://fregante.com.'));
});
}
12 changes: 12 additions & 0 deletions test.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ Generated by [AVA](https://avajs.dev).
'<a href="https://www.例.jp?">https://www.例.jp?</a>'

## linkifyUrlsToHtml: supports trailing period

> Snapshot 1
'Visit <a href="https://fregante.com">https://fregante.com</a>.'

## linkifyUrlsToDom: main

> Snapshot 1
Expand Down Expand Up @@ -307,3 +313,9 @@ Generated by [AVA](https://avajs.dev).
> Snapshot 3
'DocumentFragment: <a href="https://www.例.jp?">https://www.例.jp?</a>'

## linkifyUrlsToDom: supports trailing period

> Snapshot 1
'DocumentFragment: Visit <a href="https://fregante.com">https://fregante.com</a>.'
Binary file modified test.js.snap
Binary file not shown.

0 comments on commit 67fd9d7

Please sign in to comment.