diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-03-01 09:05:07 -0400 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2024-03-31 19:19:45 -0300 |
commit | 0c607b69c344e6cd71c2b9a63f3cd0feddca1063 (patch) | |
tree | 94d749637b8a445e57474f6213854f3490f81f32 /src/content | |
parent | 49240a57e063844387831336908111c0229f5ec0 (diff) |
content: linkExternal: return placeholder on invalid URL
Diffstat (limited to 'src/content')
-rw-r--r-- | src/content/dependencies/linkExternal.js | 69 |
1 files changed, 51 insertions, 18 deletions
diff --git a/src/content/dependencies/linkExternal.js b/src/content/dependencies/linkExternal.js index 188c1f0d..39a593b2 100644 --- a/src/content/dependencies/linkExternal.js +++ b/src/content/dependencies/linkExternal.js @@ -36,31 +36,64 @@ export default { }, generate(data, slots, {html, language}) { - let formattedLink = - language.formatExternalLink(data.url, { - style: slots.style, - context: slots.context, - }); - - // Fall back to platform if nothing matched the desired style. - if (html.isBlank(formattedLink) && slots.style !== 'platform') { + let urlIsValid; + try { + new URL(data.url); + urlIsValid = true; + } catch (error) { + urlIsValid = false; + } + + let formattedLink; + if (urlIsValid) { formattedLink = language.formatExternalLink(data.url, { - style: 'platform', + style: slots.style, context: slots.context, }); + + // Fall back to platform if nothing matched the desired style. + if (html.isBlank(formattedLink) && slots.style !== 'platform') { + formattedLink = + language.formatExternalLink(data.url, { + style: 'platform', + context: slots.context, + }); + } + } else { + formattedLink = null; } - const linkAttributes = html.attributes(); - const linkContent = - (html.isBlank(slots.content) - ? formattedLink - : slots.content); + const linkAttributes = html.attributes({ + class: 'external-link', + }); - linkAttributes.set('class', 'external-link'); - linkAttributes.set('href', data.url); + let linkContent; + if (urlIsValid) { + linkAttributes.set('href', data.url); + + if (html.isBlank(slots.content)) { + linkContent = formattedLink; + } else { + linkContent = slots.content; + } + } else { + if (html.isBlank(slots.content)) { + linkContent = + html.tag('i', + language.$('misc.external.invalidURL.annotation')); + } else { + linkContent = + language.$('misc.external.invalidURL', { + link: slots.content, + annotation: + html.tag('i', + language.$('misc.external.invalidURL.annotation')), + }); + } + } - if (slots.indicateExternal) { + if (urlIsValid && slots.indicateExternal) { linkAttributes.add('class', 'indicate-external'); let titleText; @@ -85,7 +118,7 @@ export default { } } - if (slots.tab === 'separate') { + if (urlIsValid && slots.tab === 'separate') { linkAttributes.set('target', '_blank'); } |