« get me outta code hell

content: transformContent: substitute - 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>2025-09-08 19:09:32 -0300
committer(quasar) nebula <qznebula@protonmail.com>2025-09-08 19:09:32 -0300
commit27b4de9be0f23e25bdb6eb81e3e48c50cb0d4163 (patch)
treebfd57a4e7264e86b5925f7a6fbddff73a1dcb14a
parent7ff786e6cc7a67434eb6a8bcaf21ad1b7f3cc239 (diff)
content: transformContent: substitute
-rw-r--r--src/content/dependencies/generateArtistCredit.js10
-rw-r--r--src/content/dependencies/transformContent.js62
2 files changed, 51 insertions, 21 deletions
diff --git a/src/content/dependencies/generateArtistCredit.js b/src/content/dependencies/generateArtistCredit.js
index 363ba768..1d6b5dd8 100644
--- a/src/content/dependencies/generateArtistCredit.js
+++ b/src/content/dependencies/generateArtistCredit.js
@@ -146,6 +146,16 @@ export default {
 
       relations.formatText.setSlots({
         mode: 'inline',
+
+        substitute: [
+          {
+            match: {
+              replacerKey: 'artist',
+              replacerValue: 'screamcatcher',
+            },
+            substitute: 'YAYAS!',
+          },
+        ],
       });
     }
 
diff --git a/src/content/dependencies/transformContent.js b/src/content/dependencies/transformContent.js
index a6639acd..83c85d05 100644
--- a/src/content/dependencies/transformContent.js
+++ b/src/content/dependencies/transformContent.js
@@ -109,7 +109,7 @@ export default {
           }
 
           if (spec.link) {
-            let data = {link: spec.link};
+            let data = {link: spec.link, replacerKey, replacerValue};
 
             determineData: {
               // No value at all: this is an index link.
@@ -188,8 +188,8 @@ export default {
             ...node,
             data: {
               ...node.data,
-              replacerKey: node.data.replacerKey.data,
-              replacerValue: node.data.replacerValue[0].data,
+              replacerKey,
+              replacerValue,
             },
           };
         }),
@@ -204,24 +204,7 @@ export default {
         sprawl.error,
 
       nodes:
-        sprawl.nodes
-          .map(node => {
-            switch (node.type) {
-              // Replace internal link nodes with a stub. It'll be replaced
-              // (by position) with an item from relations.
-              //
-              // TODO: This should be where label and hash get passed through,
-              // rather than in relations... (in which case there's no need to
-              // handle it specially here, and we can really just return
-              // data.nodes = sprawl.nodes)
-              case 'internal-link':
-                return {type: 'internal-link'};
-
-              // Other nodes will get processed in generate.
-              default:
-                return node;
-            }
-          }),
+        sprawl.nodes,
     };
   },
 
@@ -313,6 +296,19 @@ export default {
       validate: v => v.is('small', 'medium', 'large'),
       default: 'large',
     },
+
+    substitute: {
+      validate: v =>
+        v.strictArrayOf(
+          v.validateProperties({
+            match: v.validateProperties({
+              replacerKey: v.isString,
+              replacerValue: v.isString,
+            }),
+
+            substitute: v.isHTML,
+          })),
+    },
   },
 
   generate(data, relations, slots, {html, language, niceShowAggregate, to}) {
@@ -328,6 +324,24 @@ export default {
 
     let offsetTextNode = 0;
 
+    const substitutions =
+      (slots.substitute
+        ? slots.substitute.slice()
+        : []);
+
+    const pickSubstitution = node => {
+      const index =
+        substitutions.findIndex(({match}) =>
+          match.replacerKey === node.data.replacerKey &&
+          match.replacerValue === node.data.replacerValue);
+
+      if (index === -1) {
+        return null;
+      }
+
+      return substitutions.splice(index, 1).at(0);
+    };
+
     const contentFromNodes =
       data.nodes.map((node, index) => {
         const nextNode = data.nodes[index + 1];
@@ -346,6 +360,12 @@ export default {
           }
         };
 
+        const substitution = pickSubstitution(node);
+
+        if (substitution) {
+          return {type: 'substitution', data: substitution.substitute};
+        }
+
         switch (node.type) {
           case 'text': {
             const text = node.data.slice(offsetTextNode);