« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/content/dependencies/linkPathFromMedia.js13
-rw-r--r--src/content/dependencies/linkPathFromRoot.js13
-rw-r--r--src/content/dependencies/linkPathFromSite.js13
-rw-r--r--src/content/dependencies/linkTemplate.js6
-rw-r--r--src/content/dependencies/linkThing.js13
-rw-r--r--src/content/dependencies/transformContent.js42
-rw-r--r--src/strings-default.json1
-rw-r--r--tap-snapshots/test/snapshot/linkTemplate.js.test.cjs8
-rw-r--r--tap-snapshots/test/snapshot/transformContent.js.test.cjs11
-rw-r--r--test/snapshot/linkTemplate.js7
-rw-r--r--test/snapshot/transformContent.js8
11 files changed, 109 insertions, 26 deletions
diff --git a/src/content/dependencies/linkPathFromMedia.js b/src/content/dependencies/linkPathFromMedia.js
new file mode 100644
index 00000000..34a2b857
--- /dev/null
+++ b/src/content/dependencies/linkPathFromMedia.js
@@ -0,0 +1,13 @@
+export default {
+  contentDependencies: ['linkTemplate'],
+
+  relations: (relation) =>
+    ({link: relation('linkTemplate')}),
+
+  data: (path) =>
+    ({path}),
+
+  generate: (data, relations) =>
+    relations.link
+      .slot('path', ['media.path', data.path]),
+};
diff --git a/src/content/dependencies/linkPathFromRoot.js b/src/content/dependencies/linkPathFromRoot.js
new file mode 100644
index 00000000..dab3ac1f
--- /dev/null
+++ b/src/content/dependencies/linkPathFromRoot.js
@@ -0,0 +1,13 @@
+export default {
+  contentDependencies: ['linkTemplate'],
+
+  relations: (relation) =>
+    ({link: relation('linkTemplate')}),
+
+  data: (path) =>
+    ({path}),
+
+  generate: (data, relations) =>
+    relations.link
+      .slot('path', ['shared.path', data.path]),
+};
diff --git a/src/content/dependencies/linkPathFromSite.js b/src/content/dependencies/linkPathFromSite.js
new file mode 100644
index 00000000..64676465
--- /dev/null
+++ b/src/content/dependencies/linkPathFromSite.js
@@ -0,0 +1,13 @@
+export default {
+  contentDependencies: ['linkTemplate'],
+
+  relations: (relation) =>
+    ({link: relation('linkTemplate')}),
+
+  data: (path) =>
+    ({path}),
+
+  generate: (data, relations) =>
+    relations.link
+      .slot('path', ['localized.path', data.path]),
+};
diff --git a/src/content/dependencies/linkTemplate.js b/src/content/dependencies/linkTemplate.js
index dab81ad3..2a9261e2 100644
--- a/src/content/dependencies/linkTemplate.js
+++ b/src/content/dependencies/linkTemplate.js
@@ -5,6 +5,7 @@ export default {
     'appendIndexHTML',
     'getColors',
     'html',
+    'language',
     'to',
   ],
 
@@ -23,6 +24,7 @@ export default {
     appendIndexHTML,
     getColors,
     html,
+    language,
     to,
   }) {
     let href = slots.href;
@@ -64,6 +66,8 @@ export default {
         style,
         title,
       },
-      slots.content);
+      (html.isBlank(slots.content)
+        ? language.$('misc.missingLinkContent')
+        : slots.content));
   },
 }
diff --git a/src/content/dependencies/linkThing.js b/src/content/dependencies/linkThing.js
index 4ebf4d76..e3e2608f 100644
--- a/src/content/dependencies/linkThing.js
+++ b/src/content/dependencies/linkThing.js
@@ -44,16 +44,15 @@ export default {
   generate(data, relations, slots, {html}) {
     const path = [data.pathKey, data.directory];
 
-    let content = slots.content;
-
     const name =
       (slots.preferShortName
-        ? data.nameShort ?? data.name
-        : data.name);
+        ? data.nameShort ?? data.name ?? null
+        : data.name ?? null);
 
-    if (html.isBlank(content)) {
-      content = name;
-    }
+    const content =
+      (html.isBlank(slots.content)
+        ? name
+        : slots.content);
 
     let color = null;
     if (slots.color === true) {
diff --git a/src/content/dependencies/transformContent.js b/src/content/dependencies/transformContent.js
index fef74e8c..a194e268 100644
--- a/src/content/dependencies/transformContent.js
+++ b/src/content/dependencies/transformContent.js
@@ -33,7 +33,7 @@ export const replacerSpec = {
     value: (ref) => new Date(ref),
     html: (date, {html, language}) =>
       html.tag('time',
-        {datetime: date.toString()},
+        {datetime: date.toUTCString()},
         language.formatDate(date)),
   },
   'flash-index': {
@@ -129,9 +129,9 @@ const linkThingRelationMap = {
 };
 
 const linkValueRelationMap = {
-  // media: 'linkPathFromMedia',
-  // root: 'linkPathFromRoot',
-  // site: 'linkPathFromSite',
+  media: 'linkPathFromMedia',
+  root: 'linkPathFromRoot',
+  site: 'linkPathFromSite',
 };
 
 const linkIndexRelationMap = {
@@ -236,8 +236,16 @@ export default {
           }
 
           // This will be another {type: 'tag'} node which gets processed in
-          // generate.
-          return node;
+          // generate. Extract replacerKey and replacerValue now, since it'd
+          // be a pain to deal with later.
+          return {
+            ...node,
+            data: {
+              ...node.data,
+              replacerKey: node.data.replacerKey.data,
+              replacerValue: node.data.replacerValue[0].data,
+            },
+          };
         }),
     };
   },
@@ -370,16 +378,18 @@ export default {
 
             const {link, label, hash, toIndex} = linkNode;
 
-            return {
-              type: 'link',
-              data:
-                (toIndex
-                  ? link.slots({content: label, hash})
-                  : link.slots({
-                      content: label, hash,
-                      preferShortName: slots.preferShortLinkNames,
-                    })),
-            };
+            // These are removed from the typical combined slots({})-style
+            // because we don't want to override slots that were already set
+            // by something that's wrapping the linkTemplate or linkThing
+            // template.
+            if (label) link.setSlot('content', label);
+            if (hash) link.setSlot('hash', hash);
+
+            if (toIndex) {
+              link.setSlot('preferShortName', slots.preferShortLinkNames);
+            }
+
+            return {type: 'link', data: link};
           }
 
           case 'tag': {
diff --git a/src/strings-default.json b/src/strings-default.json
index ec174a27..7987a9f6 100644
--- a/src/strings-default.json
+++ b/src/strings-default.json
@@ -197,6 +197,7 @@
   "misc.external.flash.homestuck.page": "{LINK} (page {PAGE})",
   "misc.external.flash.homestuck.secret": "{LINK} (secret page)",
   "misc.external.flash.youtube": "{LINK} (on any device)",
+  "misc.missingLinkContent": "(Missing link content)",
   "misc.nav.previous": "Previous",
   "misc.nav.next": "Next",
   "misc.nav.info": "Info",
diff --git a/tap-snapshots/test/snapshot/linkTemplate.js.test.cjs b/tap-snapshots/test/snapshot/linkTemplate.js.test.cjs
index a9c1b678..e9d22ba5 100644
--- a/tap-snapshots/test/snapshot/linkTemplate.js.test.cjs
+++ b/tap-snapshots/test/snapshot/linkTemplate.js.test.cjs
@@ -10,9 +10,13 @@ exports[`test/snapshot/linkTemplate.js TAP linkTemplate (snapshot) > fill many s
 `
 
 exports[`test/snapshot/linkTemplate.js TAP linkTemplate (snapshot) > fill path slot & provide appendIndexHTML 1`] = `
-<a href="/c*lzone/myCoolPath/ham/pineapple/tomato/index.html"></a>
+<a href="/c*lzone/myCoolPath/ham/pineapple/tomato/index.html">delish</a>
+`
+
+exports[`test/snapshot/linkTemplate.js TAP linkTemplate (snapshot) > missing content 1`] = `
+<a href="banana">(Missing link content)</a>
 `
 
 exports[`test/snapshot/linkTemplate.js TAP linkTemplate (snapshot) > special characters in path argument 1`] = `
-<a href="media/album-additional/homestuck-vol-1/Showtime%20(Piano%20Refrain)%20-%20%23xXxAwesomeSheetMusick%3FrxXx%23.pdf"></a>
+<a href="media/album-additional/homestuck-vol-1/Showtime%20(Piano%20Refrain)%20-%20%23xXxAwesomeSheetMusick%3FrxXx%23.pdf">Damn, that's some good sheet music</a>
 `
diff --git a/tap-snapshots/test/snapshot/transformContent.js.test.cjs b/tap-snapshots/test/snapshot/transformContent.js.test.cjs
index 44535ec0..4b4e090f 100644
--- a/tap-snapshots/test/snapshot/transformContent.js.test.cjs
+++ b/tap-snapshots/test/snapshot/transformContent.js.test.cjs
@@ -5,6 +5,12 @@
  * Make sure to inspect the output below.  Do not ignore changes!
  */
 'use strict'
+exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > dates 1`] = `
+<p><time datetime="Thu, 13 Apr 2023 00:00:00 GMT">4/12/2023</time> Yep!</p>
+<p>Very nice: <time datetime="Fri, 25 Oct 2413 03:00:00 GMT">10/25/2413</time></p>
+
+`
+
 exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > inline images 1`] = `
 <p><img src="snooping.png"> as USUAL...</p>
 <p>What do you know? <img src="cowabunga.png" width="24" height="32"></p>
@@ -39,6 +45,11 @@ exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > non
 
 `
 
+exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > super basic string 1`] = `
+<p>Neat listing: Albums - by Date</p>
+
+`
+
 exports[`test/snapshot/transformContent.js TAP transformContent (snapshot) > two text paragraphs 1`] = `
 <p>Hello, world!</p>
 <p>Wow, this is very cool.</p>
diff --git a/test/snapshot/linkTemplate.js b/test/snapshot/linkTemplate.js
index 1db4582e..83d77108 100644
--- a/test/snapshot/linkTemplate.js
+++ b/test/snapshot/linkTemplate.js
@@ -30,6 +30,7 @@ testContentFunctions(t, 'linkTemplate (snapshot)', async (t, evaluate) => {
 
     slots: {
       path: ['myCoolPath', 'ham', 'pineapple', 'tomato'],
+      content: 'delish',
     },
   });
 
@@ -41,6 +42,12 @@ testContentFunctions(t, 'linkTemplate (snapshot)', async (t, evaluate) => {
         'homestuck-vol-1',
         'Showtime (Piano Refrain) - #xXxAwesomeSheetMusick?rxXx#.pdf',
       ],
+      content: `Damn, that's some good sheet music`,
     },
   });
+
+  evaluate.snapshot('missing content', {
+    name: 'linkTemplate',
+    slots: {href: 'banana'},
+  });
 });
diff --git a/test/snapshot/transformContent.js b/test/snapshot/transformContent.js
index f55ca4fa..bf09a751 100644
--- a/test/snapshot/transformContent.js
+++ b/test/snapshot/transformContent.js
@@ -57,4 +57,12 @@ testContentFunctions(t, 'transformContent (snapshot)', async (t, evaluate) => {
   quickSnapshot(
     'non-inline image #3',
       `<img src="spark.png">\nBaller.`);
+
+  quickSnapshot(
+    'dates',
+      `[[date:2023-04-13]] Yep!\nVery nice: [[date:25 October 2413]]`);
+
+  quickSnapshot(
+    'super basic string',
+      `Neat listing: [[string:listingPage.listAlbums.byDate.title]]`);
 });