« get me outta code hell

html: linkExternal: use full url, not href, in formatExternalLink - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/content/dependencies
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2026-05-05 12:15:34 -0300
committer(quasar) nebula <qznebula@protonmail.com>2026-05-05 12:15:34 -0300
commit16c1ddd17d444a2a9841a102b191f2c4eee09d79 (patch)
tree6f748bf00bb0efb995a807b10d423985ae064520 /src/content/dependencies
parent919e4ae20974fb61742d100b9a7f1b951f8f9bc8 (diff)
html: linkExternal: use full url, not href, in formatExternalLink
Diffstat (limited to 'src/content/dependencies')
-rw-r--r--src/content/dependencies/linkExternal.js46
1 files changed, 21 insertions, 25 deletions
diff --git a/src/content/dependencies/linkExternal.js b/src/content/dependencies/linkExternal.js
index b47b1a90..52e43250 100644
--- a/src/content/dependencies/linkExternal.js
+++ b/src/content/dependencies/linkExternal.js
@@ -1,3 +1,4 @@
+import {errors} from '#sugar';
 import {isExternalLinkContext, isExternalLinkStyle} from '#external-links';
 
 export default {
@@ -74,35 +75,17 @@ export default {
   },
 
   generate(data, slots, {html, language, to}) {
-    let urlIsValid;
-    try {
-      new URL(data.url);
-      urlIsValid = true;
-    } catch {
-      urlIsValid = false;
-    }
-
-    let href;
-    if (urlIsValid) {
-      const {canonicalBase, canonicalMediaBase} = data;
-      const past = front => decodeURIComponent(data.url.slice(front.length));
-      if (canonicalMediaBase && data.url.startsWith(canonicalMediaBase)) {
-        href = to('media.path', past(canonicalMediaBase));
-      } else if (canonicalBase && data.url.startsWith(canonicalBase)) {
-        href = to('shared.path', past(canonicalBase));
-      } else {
-        href = data.url;
-      }
-    }
-
-    const urlEntry = {
-      url: href,
-      annotation: data.annotation,
-    };
+    const urlIsValid =
+      !errors(() => new URL(data.url));
 
     let formattedLink;
     let formattedPlatform;
     if (urlIsValid) {
+      const urlEntry = {
+        url: data.url,
+        annotation: data.annotation,
+      };
+
       formattedLink =
         language.formatExternalLink(urlEntry, {
           style: slots.style,
@@ -123,6 +106,19 @@ export default {
       formattedLink = null;
     }
 
+    let href;
+    if (urlIsValid) {
+      const {canonicalBase, canonicalMediaBase} = data;
+      const past = front => decodeURIComponent(data.url.slice(front.length));
+      if (canonicalMediaBase && data.url.startsWith(canonicalMediaBase)) {
+        href = to('media.path', past(canonicalMediaBase));
+      } else if (canonicalBase && data.url.startsWith(canonicalBase)) {
+        href = to('shared.path', past(canonicalBase));
+      } else {
+        href = data.url;
+      }
+    }
+
     const linkAttributes = html.attributes({
       class: 'external-link',
     });