« get me outta code hell

external-links: always require handle/details extractions to match - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2024-03-29 19:31:06 -0300
committer(quasar) nebula <qznebula@protonmail.com>2024-03-29 20:41:54 -0300
commit7afce11de0f3ba5432372a773cfb3e8d135b76fe (patch)
treefed194f26704cf13ad7c9f9b14677fb3394c6cd3 /src
parenteac58883fd56d2e2d3dd2cbb5b8b01259b1896df (diff)
external-links: always require handle/details extractions to match
Diffstat (limited to 'src')
-rw-r--r--src/util/external-links.js55
1 files changed, 35 insertions, 20 deletions
diff --git a/src/util/external-links.js b/src/util/external-links.js
index 6d993ee0..46d6e0e9 100644
--- a/src/util/external-links.js
+++ b/src/util/external-links.js
@@ -197,14 +197,13 @@ export const externalLinkSpec = [
     match: {
       context: 'flash',
       domain: 'homestuck.com',
-      pathname: /^story\/[0-9]+\/?$/,
     },
 
     platform: 'homestuck',
 
     detail: {
       substring: 'page',
-      page: {pathname: /[0-9]+/},
+      page: {pathname: /^story\/([0-9]+)\/?$/,},
     },
 
     icon: 'globe',
@@ -281,10 +280,7 @@ export const externalLinkSpec = [
   },
 
   {
-    match: {
-      domain: 'bsky.app',
-      pathname: /^profile\/[^/]+\/?/,
-    },
+    match: {domain: 'bsky.app'},
 
     platform: 'bluesky',
     handle: {pathname: /^profile\/([^/]+?)(?:\.bsky\.social)?\/?$/},
@@ -329,10 +325,7 @@ export const externalLinkSpec = [
   },
 
   {
-    match: {
-      domain: 'itch.io',
-      pathname: /^profile\/.+\/?$/,
-    },
+    match: {domain: 'itch.io'},
 
     platform: 'itch',
     handle: {pathname: /^profile\/(.+)\/?$/},
@@ -341,10 +334,7 @@ export const externalLinkSpec = [
   },
 
   {
-    match: {
-      domain: 'ko-fi.com',
-      pathname: /^.+\/?/,
-    },
+    match: {domain: 'ko-fi.com'},
 
     platform: 'kofi',
     handle: {pathname: /^(.+)\/?/},
@@ -353,10 +343,7 @@ export const externalLinkSpec = [
   },
 
   {
-    match: {
-      domain: 'mspaintadventures.fandom.com',
-      pathname: /^wiki\/.+\/?$/,
-    },
+    match: {domain: 'mspaintadventures.fandom.com'},
 
     platform: 'fandom.mspaintadventures',
 
@@ -556,6 +543,9 @@ export function getMatchingDescriptorsForExternalLink(url, descriptors, {
   const comparePathname = regex => regex.test(pathname.slice(1));
   const compareQuery = regex => regex.test(query.slice(1));
 
+  const compareExtractSpec = extract =>
+    extractPartFromExternalLink(url, extract, {mode: 'test'});
+
   const contextArray =
     (Array.isArray(context)
       ? context
@@ -589,12 +579,29 @@ export function getMatchingDescriptorsForExternalLink(url, descriptors, {
           ? compareQuery(match.query)
        : match.queries
           ? match.quieries.some(compareQuery)
+          : true))
+
+      .filter(({handle}) =>
+        (handle
+          ? compareExtractSpec(handle)
+          : true))
+
+      .filter(({detail}) =>
+        (typeof detail === 'object'
+          ? Object.entries(detail)
+              .filter(([key]) => key !== 'substring')
+              .map(([_key, value]) => value)
+              .every(compareExtractSpec)
           : true));
 
   return [...matchingDescriptors, fallbackDescriptor];
 }
 
-export function extractPartFromExternalLink(url, extract) {
+export function extractPartFromExternalLink(url, extract, {
+  // Set to 'test' to just see if this would extract anything.
+  // This disables running custom transformations.
+  mode = 'extract',
+} = {}) {
   const {domain, pathname, query} = urlParts(url);
 
   let regexen = [];
@@ -670,15 +677,23 @@ export function extractPartFromExternalLink(url, extract) {
   })) {
     const match = test.match(regex);
     if (match) {
-      value = prefix + (match[1] ?? match[0]);
+      value = match[1] ?? match[0];
       break;
     }
   }
 
+  if (mode === 'test') {
+    return !!value;
+  }
+
   if (!value) {
     return null;
   }
 
+  if (prefix) {
+    value = prefix + value;
+  }
+
   for (const fn of transform) {
     value = fn(value);
   }