« get me outta code hell

data, content: extract external link parsing into nicer interface - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/things
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-11-23 11:16:48 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-11-24 13:45:11 -0400
commit8f17782a5f2adbafd031b269195879eb7f79e05f (patch)
treeb15d7930f8de0c4e98e14999a49bff4e1919f4a9 /src/data/things
parent921f2d421d6ffb87fab1a2059a6c313b9c81f4f8 (diff)
data, content: extract external link parsing into nicer interface
Diffstat (limited to 'src/data/things')
-rw-r--r--src/data/things/language.js34
1 files changed, 33 insertions, 1 deletions
diff --git a/src/data/things/language.js b/src/data/things/language.js
index 646eb6d1..185488e2 100644
--- a/src/data/things/language.js
+++ b/src/data/things/language.js
@@ -1,5 +1,11 @@
-import {Tag} from '#html';
 import {isLanguageCode} from '#validators';
+import {Tag} from '#html';
+
+import {
+  getExternalLinkStringsFromDescriptors,
+  isExternalLinkSpec,
+  isExternalLinkStyle,
+} from '#external-links';
 
 import {
   externalFunction,
@@ -72,6 +78,13 @@ export class Language extends Thing {
       update: {validate: (t) => typeof t === 'object'},
     },
 
+    // List of descriptors for providing to external link utilities when using
+    // language.formatExternalLink - refer to util/external-links.js for info.
+    externalLinkSpec: {
+      flags: {update: true, expose: true},
+      update: {validate: isExternalLinkSpec},
+    },
+
     // Update only
 
     escapeHTML: externalFunction(),
@@ -299,6 +312,25 @@ export class Language extends Thing {
       : duration;
   }
 
+  formatExternalLink(url, {style = 'normal'} = {}) {
+    if (!this.externalLinkSpec) {
+      throw new TypeError(`externalLinkSpec unavailable`);
+    }
+
+    if (style !== 'all') {
+      isExternalLinkStyle(style);
+    }
+
+    const results =
+      getExternalLinkStringsFromDescriptors(url, this.externalLinkSpec, this);
+
+    if (style === 'all') {
+      return results;
+    } else {
+      return results[style];
+    }
+  }
+
   formatIndex(value) {
     this.assertIntlAvailable('intl_pluralOrdinal');
     return this.formatString('count.index.' + this.intl_pluralOrdinal.select(value), {index: value});