« get me outta code hell

content: linkExternal: slot mode instead of taking type option - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-06-12 17:11:35 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-06-12 17:11:35 -0300
commit4e2993f3bedd8e413e56aafcb96a22b16d930680 (patch)
tree443bfe0e1d721a304be11d47d0273030215021e7
parent5952f159e4d02c8f8356ab8a9860fca6330b7757 (diff)
content: linkExternal: slot mode instead of taking type option
-rw-r--r--src/content/dependencies/generateAlbumInfoPage.js7
-rw-r--r--src/content/dependencies/linkExternal.js23
-rw-r--r--tap-snapshots/test/snapshot/linkExternal.js.test.cjs4
-rw-r--r--test/snapshot/linkExternal.js8
4 files changed, 17 insertions, 25 deletions
diff --git a/src/content/dependencies/generateAlbumInfoPage.js b/src/content/dependencies/generateAlbumInfoPage.js
index e317adb1..09de21bf 100644
--- a/src/content/dependencies/generateAlbumInfoPage.js
+++ b/src/content/dependencies/generateAlbumInfoPage.js
@@ -99,7 +99,7 @@ export default {
 
       listen.externalLinks =
         album.urls.map(url =>
-          relation('linkExternal', url, {type: 'album'}));
+          relation('linkExternal', url));
     }
 
     // Section: Extra links
@@ -234,7 +234,10 @@ export default {
           sec.listen &&
             html.tag('p',
               language.$('releaseInfo.listenOn', {
-                links: language.formatDisjunctionList(sec.listen.externalLinks),
+                links:
+                  language.formatDisjunctionList(
+                    sec.listen.externalLinks
+                      .map(link => link.slot('mode', 'album'))),
               })),
 
           html.tag('p',
diff --git a/src/content/dependencies/linkExternal.js b/src/content/dependencies/linkExternal.js
index 08191a21..7c3d86a8 100644
--- a/src/content/dependencies/linkExternal.js
+++ b/src/content/dependencies/linkExternal.js
@@ -5,21 +5,18 @@ const MASTODON_DOMAINS = ['types.pl'];
 export default {
   extraDependencies: ['html', 'language'],
 
-  data(url, {
-    type = 'generic',
-  } = {}) {
-    const types = ['generic', 'album'];
-    if (!types.includes(type)) {
-      throw new TypeError(`Expected type to be one of ${types}`);
-    }
+  data(url) {
+    return {url};
+  },
 
-    return {
-      url,
-      type,
-    };
+  slots: {
+    mode: {
+      validate: v => v.is('generic', 'album'),
+      default: 'generic',
+    },
   },
 
-  generate(data, {html, language}) {
+  generate(data, slots, {html, language}) {
     let isLocal;
     let domain;
     try {
@@ -50,7 +47,7 @@ export default {
         ? language.$('misc.external.mastodon.domain', {domain})
 
     : domain.includes('youtu')
-        ? data.type === 'album'
+        ? slots.mode === 'album'
           ? data.url.includes('list=')
             ? language.$('misc.external.youtube.playlist')
             : language.$('misc.external.youtube.fullAlbum')
diff --git a/tap-snapshots/test/snapshot/linkExternal.js.test.cjs b/tap-snapshots/test/snapshot/linkExternal.js.test.cjs
index e4376810..5a3ef738 100644
--- a/tap-snapshots/test/snapshot/linkExternal.js.test.cjs
+++ b/tap-snapshots/test/snapshot/linkExternal.js.test.cjs
@@ -28,10 +28,6 @@ exports[`test/snapshot/linkExternal.js TAP linkExternal (snapshot) > custom matc
 <a href="https://youtu.be/abc" class="nowrap">YouTube (full album)</a>
 <a href="https://youtube.com/watch?v=abc" class="nowrap">YouTube (full album)</a>
 <a href="https://youtube.com/Playlist?list=kweh" class="nowrap">YouTube (playlist)</a>
-<a href="https://youtu.be/abc" class="nowrap">YouTube</a>
-<a href="https://youtu.be/abc?list=kweh" class="nowrap">YouTube</a>
-<a href="https://youtube.com/watch?v=abc" class="nowrap">YouTube</a>
-<a href="https://youtube.com/watch?v=abc&list=kweh" class="nowrap">YouTube</a>
 `
 
 exports[`test/snapshot/linkExternal.js TAP linkExternal (snapshot) > missing domain (arbitrary local path) 1`] = `
diff --git a/test/snapshot/linkExternal.js b/test/snapshot/linkExternal.js
index 9661aead..a65044df 100644
--- a/test/snapshot/linkExternal.js
+++ b/test/snapshot/linkExternal.js
@@ -37,13 +37,9 @@ testContentFunctions(t, 'linkExternal (snapshot)', async (t, evaluate) => {
       {args: ['https://youtu.be/abc', {type: 'album'}]},
       {args: ['https://youtube.com/watch?v=abc', {type: 'album'}]},
       {args: ['https://youtube.com/Playlist?list=kweh', {type: 'album'}]},
-
-      // Reuse default when no type specified
-      {args: ['https://youtu.be/abc']},
-      {args: ['https://youtu.be/abc?list=kweh']},
-      {args: ['https://youtube.com/watch?v=abc']},
-      {args: ['https://youtube.com/watch?v=abc&list=kweh']},
     ],
+    postprocess:
+      link => link.slot('mode', 'album'),
   });
 
   evaluate.snapshot('custom domains for common platforms', {