« get me outta code hell

content: transformContent: handle line breaks around lists properly - 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-05-26 14:09:05 -0300
committer(quasar) nebula <qznebula@protonmail.com>2023-05-26 14:09:05 -0300
commitbb7faef7695fa7f7a00b9b1b4fb99267813d9bfa (patch)
treee1952a9189b551f4fedd72aa05e33caa52e50031
parent270622e7984732178217f0d4e6de2e30ea74aedc (diff)
content: transformContent: handle line breaks around lists properly
-rw-r--r--src/content/dependencies/transformContent.js19
1 files changed, 14 insertions, 5 deletions
diff --git a/src/content/dependencies/transformContent.js b/src/content/dependencies/transformContent.js
index 751c58c..bf4233f 100644
--- a/src/content/dependencies/transformContent.js
+++ b/src/content/dependencies/transformContent.js
@@ -260,18 +260,27 @@ export default {
 
         // This is separated into its own function just since we're gonna reuse
         // it in a minute if everything goes to heck in lyrics mode.
-        const transformMultiline = () =>
-          marked.parse(
+        const transformMultiline = () => {
+          const markedInput =
             contentFromNodes
               .map(node => {
                 if (node.type === 'text') {
-                  return node.data.replace(/\n+/g, '\n\n');
+                  return node.data;
                 } else {
                   return node.data.toString();
                 }
               })
-              .join(''),
-            markedOptions);
+              .join('')
+
+              // Compress multiple line breaks into single line breaks.
+              .replace(/\n{2,}/g, '\n')
+              // Expand line breaks which don't follow a list.
+              .replace(/(?<!^ *-.*)\n+/gm, '\n\n')
+              // Expand line breaks which are at the end of a list.
+              .replace(/(?<=^ *-.*)\n+(?!^ *-)/gm, '\n\n');
+
+          return marked.parse(markedInput, markedOptions);
+        }
 
         if (slots.mode === 'multiline') {
           // Unfortunately, we kind of have to be super evil here and stringify