« get me outta code hell

infra: html: make deep Template resolving opt-in via utility - 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>2023-08-02 21:54:46 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-08-02 21:55:15 -0300
commitb149e73fcd30602fd2312e26aca86c2b769bbc01 (patch)
tree49f75db1ae730cc7b0c00160120afb1205338f7b
parent473740f69dcbad408b0d84567c2bb2de97e8a4aa (diff)
infra: html: make deep Template resolving opt-in via utility
Just always resolving was causing some bugs.
-rw-r--r--src/util/html.js30
-rw-r--r--src/write/build-modes/live-dev-server.js3
-rw-r--r--src/write/build-modes/static-build.js2
3 files changed, 26 insertions, 9 deletions
diff --git a/src/util/html.js b/src/util/html.js
index 26dd6495..d7d03732 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -494,6 +494,10 @@ export class Attributes {
   }
 }
 
+export function resolve(tagOrTemplate) {
+  return Template.resolve(tagOrTemplate);
+}
+
 export function template(description) {
   return new Template(description);
 }
@@ -785,13 +789,7 @@ export class Template {
       slots[slotName] = this.getSlotValue(slotName);
     }
 
-    // Get outta here with that recursive Template bollocks!
-    const content = this.description.content(slots);
-    if (content instanceof Template) {
-      return content.content;
-    } else {
-      return content;
-    }
+    return this.description.content(slots);
   }
 
   set description(_value) {
@@ -806,6 +804,24 @@ export class Template {
     return this.content.toString();
   }
 
+  static resolve(tagOrTemplate) {
+    // Flattens contents of a template, recursively "resolving" until a
+    // non-template is ready (or just returns a provided non-template
+    // argument as-is).
+
+    if (!(tagOrTemplate instanceof Template)) {
+      return tagOrTemplate;
+    }
+
+    let {content} = tagOrTemplate;
+
+    while (content instanceof Template) {
+      content = content.content;
+    }
+
+    return content;
+  }
+
   [inspect.custom]() {
     const {annotation} = this.description;
     if (annotation) {
diff --git a/src/write/build-modes/live-dev-server.js b/src/write/build-modes/live-dev-server.js
index 6734c065..a87da27b 100644
--- a/src/write/build-modes/live-dev-server.js
+++ b/src/write/build-modes/live-dev-server.js
@@ -11,6 +11,7 @@ import {serializeThings} from '../../data/serialize.js';
 import * as pageSpecs from '../../page/index.js';
 
 import {logInfo, logWarn, progressCallAll} from '../../util/cli.js';
+import * as html from '../../util/html.js';
 import {empty} from '../../util/sugar.js';
 import {
   getPagePathname,
@@ -365,7 +366,7 @@ export async function go({
           args: page.contentFunction.args ?? [],
         });
 
-      const {pageHTML} = topLevelResult.content;
+      const {pageHTML} = html.resolve(topLevelResult);
 
       if (!quietResponses) console.log(`${requestHead} [200] ${pathname}`);
       response.writeHead(200, contentTypeHTML);
diff --git a/src/write/build-modes/static-build.js b/src/write/build-modes/static-build.js
index 8bc1b242..fb9661ba 100644
--- a/src/write/build-modes/static-build.js
+++ b/src/write/build-modes/static-build.js
@@ -309,7 +309,7 @@ export async function go({
             args: page.contentFunction.args ?? [],
           });
 
-        const {pageHTML, oEmbedJSON} = topLevelResult.content;
+        const {pageHTML, oEmbedJSON} = html.resolve(topLevelResult);
 
         return writePage({
           pageHTML,