« get me outta code hell

yaml: preserve line breaks through dividers - 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>2025-04-16 14:28:22 -0300
committer(quasar) nebula <qznebula@protonmail.com>2025-04-16 14:28:22 -0300
commit59616fad077931e61529db8180258144a97afae2 (patch)
treef49a189a6d6bd08b46fb50799593185e975ad2db /src
parent9d4892044bda2e9f9d875e6c953b1fdaacf5c48f (diff)
yaml: preserve line breaks through dividers staging
...when sorting YAML documents
Diffstat (limited to 'src')
-rw-r--r--src/data/yaml.js11
1 files changed, 7 insertions, 4 deletions
diff --git a/src/data/yaml.js b/src/data/yaml.js
index 50317238..af1d5740 100644
--- a/src/data/yaml.js
+++ b/src/data/yaml.js
@@ -1781,14 +1781,16 @@ export function flattenThingLayoutToDocumentOrder(layout) {
 }
 
 export function* splitDocumentsInYAMLSourceText(sourceText) {
-  const dividerRegex = /^-{3,}\n?/gm;
+  // Not multiline!
+  const dividerRegex = /(?:\r\n|\n|^)-{3,}(?:\r\n|\n|$)/g;
+
   let previousDivider = '';
 
   while (true) {
     const {lastIndex} = dividerRegex;
     const match = dividerRegex.exec(sourceText);
     if (match) {
-      const nextDivider = match[0].trim();
+      const nextDivider = match[0];
 
       yield {
         previousDivider,
@@ -1799,11 +1801,12 @@ export function* splitDocumentsInYAMLSourceText(sourceText) {
       previousDivider = nextDivider;
     } else {
       const nextDivider = '';
+      const lineBreak = previousDivider.match(/\r?\n/)?.[0] ?? '';
 
       yield {
         previousDivider,
         nextDivider,
-        text: sourceText.slice(lastIndex).replace(/(?<!\n)$/, '\n'),
+        text: sourceText.slice(lastIndex).replace(/(?<!\n)$/, lineBreak),
       };
 
       return;
@@ -1829,7 +1832,7 @@ export function recombineDocumentsIntoYAMLSourceText(documents) {
 
   for (const document of documents) {
     if (sourceText) {
-      sourceText += divider + '\n';
+      sourceText += divider;
     }
 
     sourceText += document.text;