Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid linkifying trailing period #49

Merged
merged 1 commit into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = '.';
Copy link
Collaborator Author

@fregante fregante Nov 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trailingDot = url.slice(-1);

Plus the condition update

}

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.