« get me outta code hell

new html.noEdgeWhitespace tag option - 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>2022-09-15 23:28:55 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-09-15 23:28:55 -0300
commite9cb50f4c92e88739fb3af99ea6abc1f06826e3c (patch)
tree43b2d959af1e4823414bf996a4b80f743fb607ea /src/util
parent28b1a728f429c4c04ee3a16584f8433a1d312c7c (diff)
new html.noEdgeWhitespace tag option
Diffstat (limited to 'src/util')
-rw-r--r--src/util/html.js26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/util/html.js b/src/util/html.js
index bdb385b5..752291e9 100644
--- a/src/util/html.js
+++ b/src/util/html.js
@@ -32,6 +32,14 @@ export const onlyIfContent = Symbol();
 // string.
 export const joinChildren = Symbol();
 
+// Pass to tag() as an attributes key to prevent additional whitespace from
+// being added to the inner start and end of the tag's content - basically,
+// ensuring that the start of the content begins immediately after the ">"
+// ending the opening tag, and ends immediately before the "<" at the start of
+// the closing tag. This has effect when a single child spans multiple lines,
+// or when there are multiple children.
+export const noEdgeWhitespace = Symbol();
+
 export function tag(tagName, ...args) {
   const selfClosing = selfClosingTags.includes(tagName);
 
@@ -75,14 +83,20 @@ export function tag(tagName, ...args) {
 
   if (content) {
     if (content.includes('\n')) {
-      return (
-        `<${openTag}>\n` +
+      return [
+        `<${openTag}>`,
         content
           .split('\n')
-          .map((line) => '    ' + line + '\n')
-          .join('') +
-        `</${tagName}>`
-      );
+          .map((line, i) =>
+            (i === 0 && attrs?.[noEdgeWhitespace]
+              ? line
+              : '    ' + line))
+          .join('\n'),
+        `</${tagName}>`,
+      ].join(
+        (attrs?.[noEdgeWhitespace]
+          ? ''
+          : '\n'));
     } else {
       return `<${openTag}>${content}</${tagName}>`;
     }