From 3ac0ca5a5cbd0c24e09674eef56f04638e1485aa Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 3 Sep 2024 22:38:15 +0700 Subject: [PATCH 1/5] Clarify value parsing function --- index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f218feb..52c4e3c 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,17 @@ import createHtmlElement from 'create-html-element'; // Capture the whole URL in group 1 to keep `String#split()` support const urlRegex = () => (/((? { + switch (typeof value) { + case 'function': + return {html: value(href)}; + case 'undefined': + return {text: href}; + default: + return {html: value}; + } +}; + // Get `` element as string const linkify = (href, options = {}) => createHtmlElement({ name: 'a', @@ -11,9 +22,7 @@ const linkify = (href, options = {}) => createHtmlElement({ ...options.attributes, href, // eslint-disable-line no-dupe-keys }, - 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 From 07a90e1876c74bff434f7f773616954ae777b2df Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 3 Sep 2024 22:38:44 +0700 Subject: [PATCH 2/5] Lint switch --- index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 52c4e3c..751091c 100644 --- a/index.js +++ b/index.js @@ -5,12 +5,17 @@ const urlRegex = () => (/((? { switch (typeof value) { - case 'function': + case 'function': { return {html: value(href)}; - case 'undefined': + } + + case 'undefined': { return {text: href}; - default: + } + + default: { return {html: value}; + } } }; From 6adc4c4a3c9f6d20dd7edebdcc91d02cbb8b4f7a Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 3 Sep 2024 22:38:58 +0700 Subject: [PATCH 3/5] Drop unused code --- index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/index.js b/index.js index 751091c..5745e24 100644 --- a/index.js +++ b/index.js @@ -23,9 +23,8 @@ const parseValue = (value, href) => { const linkify = (href, options = {}) => createHtmlElement({ name: 'a', attributes: { - href: '', ...options.attributes, - href, // eslint-disable-line no-dupe-keys + href, }, ...parseValue(options.value, href), }); From ebed51e0fe07b228111ce7ae42531a54379b96d4 Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 3 Sep 2024 22:42:56 +0700 Subject: [PATCH 4/5] Explain existing code --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 5745e24..db9aff4 100644 --- a/index.js +++ b/index.js @@ -23,8 +23,9 @@ const parseValue = (value, href) => { const linkify = (href, options = {}) => createHtmlElement({ name: 'a', attributes: { + href, // Set first so it appears first in the final HTML string ...options.attributes, - href, + href, // eslint-disable-line no-dupe-keys -- Ensures it's not overwritten }, ...parseValue(options.value, href), }); From 35d27d15f856ee8e2c9d290ba7b74bdd6010d17f Mon Sep 17 00:00:00 2001 From: Federico Date: Tue, 3 Sep 2024 22:49:45 +0700 Subject: [PATCH 5/5] Take comment from linkify-issues --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index db9aff4..3ff5b83 100644 --- a/index.js +++ b/index.js @@ -22,8 +22,9 @@ 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, // Set first so it appears first in the final HTML string + href, ...options.attributes, href, // eslint-disable-line no-dupe-keys -- Ensures it's not overwritten },