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

Clarify value parsing function #47

Merged
merged 5 commits into from
Sep 3, 2024
Merged
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
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ import createHtmlElement from 'create-html-element';
// Capture the whole URL in group 1 to keep `String#split()` support
const urlRegex = () => (/((?<!\+)https?:\/\/(?:www\.)?(?:[-\p{Letter}.]+?[.@][a-zA-Z\d]{2,}|localhost)(?:[-\w\p{Letter}.:%+~#*$!?&/=@]*?(?:,(?!\s))*?)*)/gu);

const parseValue = (value, href) => {
switch (typeof value) {
case 'function': {
return {html: value(href)};
}

case 'undefined': {
return {text: href};
}

default: {
return {html: value};
}
}
};

// 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: '',
href,
...options.attributes,
href, // eslint-disable-line no-dupe-keys
href, // eslint-disable-line no-dupe-keys -- Ensures it's not overwritten
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I blamed this to 72d8831

I tried changing it and it broke a snapshot just for the order of the attributes. I'm not sure if that was your original intent but I like it.

},
text: options.value === undefined ? href : undefined,
html: options.value === undefined ? undefined
: (typeof options.value === 'function' ? options.value(href) : options.value),
...parseValue(options.value, href),
});

// Get DOM node from HTML
Expand Down