« get me outta code hell

external-links: allow providing multiple contexts - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-02-14 13:04:09 -0400
committer(quasar) nebula <qznebula@protonmail.com>2024-02-14 13:44:35 -0400
commit08114ae53a4053b45577860f0a127ee2e6b3e3a6 (patch)
treea0154729aa516a0a72127b797476f7f52eeae5b2 /src/util
parentd642e41ed5459b86a55b0db274a93adb2f7d907a (diff)
external-links: allow providing multiple contexts
Diffstat (limited to 'src/util')
-rw-r--r--src/util/external-links.js22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/util/external-links.js b/src/util/external-links.js
index 57b6c3f6..14ee10a5 100644
--- a/src/util/external-links.js
+++ b/src/util/external-links.js
@@ -5,6 +5,7 @@ import {
   is,
   isObject,
   isStringNonEmpty,
+  looseArrayOf,
   optional,
   validateAllPropertyValues,
   validateArrayItems,
@@ -30,7 +31,10 @@ export const externalLinkContexts = [
   'track',
 ];
 
-export const isExternalLinkContext = is(...externalLinkContexts);
+export const isExternalLinkContext =
+  anyOf(
+    is(...externalLinkContexts),
+    looseArrayOf(is(...externalLinkContexts)));
 
 // This might need to be adjusted for YAML importing...
 const isRegExp =
@@ -76,10 +80,7 @@ export const isExternalLinkSpec =
         query: optional(isRegExp),
         queries: optional(validateArrayItems(isRegExp)),
 
-        context:
-          optional(anyOf(
-            isExternalLinkContext,
-            validateArrayItems(isExternalLinkContext))),
+        context: optional(isExternalLinkContext),
       }),
 
       platform: isStringNonEmpty,
@@ -444,6 +445,11 @@ export function getMatchingDescriptorsForExternalLink(url, descriptors, {
   const comparePathname = regex => regex.test(pathname.slice(1));
   const compareQuery = regex => regex.test(query.slice(1));
 
+  const contextArray =
+    (Array.isArray(context)
+      ? context
+      : [context]).filter(Boolean);
+
   const matchingDescriptors =
     descriptors
       .filter(({match}) => {
@@ -452,8 +458,10 @@ export function getMatchingDescriptorsForExternalLink(url, descriptors, {
         return false;
       })
       .filter(({match}) => {
-        if (Array.isArray(match.context)) return match.context.includes(context);
-        if (match.context) return context === match.context;
+        if (Array.isArray(match.context))
+          return match.context.some(c => contextArray.includes(c));
+        if (match.context)
+          return contextArray.includes(match.context);
         return true;
       })
       .filter(({match}) => {