« get me outta code hell

content: transformContent: fix lyrics-mode line break bugs - 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-09 18:36:35 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-08-09 18:36:35 -0300
commita44ebe6c0790ed5a1dc08bc27e4e8c59155e931a (patch)
tree4c1f99d68dd00d4399b5eecf68315d7b80dc54d0
parentaa0023dadf8b40ccbf3dedfbcbafba25f23b995a (diff)
content: transformContent: fix lyrics-mode line break bugs
-rw-r--r--src/content/dependencies/transformContent.js25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/content/dependencies/transformContent.js b/src/content/dependencies/transformContent.js
index 93669cc..cc4e7a3 100644
--- a/src/content/dependencies/transformContent.js
+++ b/src/content/dependencies/transformContent.js
@@ -517,12 +517,29 @@ export default {
       // multiline.
       return marked.parse(
         contentFromNodes
-          .map(node => {
-            if (node.type === 'text') {
-              return node.data.replace(/\b\n\b/g, '<br>\n');
-            } else {
+          .map((node, index) => {
+            if (node.type !== 'text') {
               return node.data.toString();
             }
+
+            // First, replace line breaks that follow text content with
+            // <br> tags.
+            let content = node.data.replace(/(?!^)\n/gm, '<br>\n');
+
+            // Scrap line breaks that are at the end of a verse.
+            content = content.replace(/<br>$(?=\n\n)/gm, '');
+
+            // If the node started with a line break, and it's not the
+            // very first node, then whatever came before it was inline.
+            // (This is an assumption based on text links being basically
+            // the only tag that shows up in lyrics.) Since this text is
+            // following content that was already inline, restore that
+            // initial line break.
+            if (node.data[0] === '\n' && index !== 0) {
+              content = '<br>' + content;
+            }
+
+            return content;
           })
           .join(''),
         markedOptions);