« 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
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
parent28b1a728f429c4c04ee3a16584f8433a1d312c7c (diff)
new html.noEdgeWhitespace tag option
Diffstat (limited to 'src')
-rw-r--r--src/misc-templates.js5
-rw-r--r--src/util/html.js26
2 files changed, 24 insertions, 7 deletions
diff --git a/src/misc-templates.js b/src/misc-templates.js
index b5a635d1..09c8f436 100644
--- a/src/misc-templates.js
+++ b/src/misc-templates.js
@@ -101,7 +101,10 @@ export function getArtistString(
 
       const externalLinks = hasExternalPart &&
         html.tag('span',
-          {class: 'icons'},
+          {
+            [html.noEdgeWhitespace]: true,
+            class: 'icons'
+          },
           language.formatUnitList(
             urls.map(url => iconifyURL(url, {language}))));
 
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}>`;
     }