« get me outta code hell

html: show tag child tree in util.inspect - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2023-12-29 18:17:00 -0400
committer(quasar) nebula <qznebula@protonmail.com>2023-12-30 12:29:52 -0400
commitc8aa73d6ac9401a9ffe918347202139cf63c41ab (patch)
tree933ee3f6087aefd77381547987c89eb3b48f48a9 /src/util
parentf0013d8fd937f2f1d14608edb7bb36d4c7f7e85c (diff)
html: show tag child tree in util.inspect
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js54
1 files changed, 42 insertions, 12 deletions
diff --git a/src/util/html.js b/src/util/html.js
index 4be008e..16da6d7 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -435,20 +435,50 @@ export class Tag {
     return new Tag(null, null, content);
   }
 
-  [inspect.custom]() {
-    if (this.tagName) {
-      if (empty(this.content)) {
-        return `Tag <${this.tagName} />`;
-      } else {
-        return `Tag <${this.tagName}> (${this.content.length} items)`;
-      }
-    } else {
-      if (empty(this.content)) {
-        return `Tag (no name)`;
-      } else {
-        return `Tag (no name, ${this.content.length} items)`;
+  [inspect.custom](depth, opts) {
+    const lines = [];
+
+    const heading =
+      (this.tagName
+        ? (empty(this.content)
+            ? `Tag <${this.tagName} />`
+            : `Tag <${this.tagName}> (${this.content.length} items)`)
+        : (empty(this.content)
+            ? `Tag (no name)`
+            : `Tag (no name, ${this.content.length} items)`));
+
+    lines.push(heading);
+
+    if (!opts.compact && (depth === null || depth >= 0)) {
+      const nextDepth =
+        (depth === null
+          ? null
+          : depth - 1);
+
+      for (const child of this.content) {
+        const childLines = [];
+
+        if (typeof child === 'string') {
+          const childFlat = child.replace(/\n/g, String.raw`\n`);
+          const childTrim =
+            (childFlat.length >= 40
+              ? childFlat.slice(0, 37) + '...'
+              : childFlat);
+
+          childLines.push(
+            `  Text: ${opts.stylize(`"${childTrim}"`, 'string')}`);
+        } else {
+          childLines.push(...
+            inspect(child, {depth: nextDepth})
+              .split('\n')
+              .map(line => `  ${line}`));
+        }
+
+        lines.push(...childLines);
       }
     }
+
+    return lines.join('\n');
   }
 }