« get me outta code hell

hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/yaml.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/yaml.js')
-rw-r--r--src/data/yaml.js90
1 files changed, 68 insertions, 22 deletions
diff --git a/src/data/yaml.js b/src/data/yaml.js
index c6600121..f3f422f7 100644
--- a/src/data/yaml.js
+++ b/src/data/yaml.js
@@ -8,6 +8,7 @@ import {inspect as nodeInspect} from 'node:util';
 import yaml from 'js-yaml';
 
 import {colors, ENABLE_COLOR, logInfo, logWarn} from '#cli';
+import {parseInput, splitContentNodesAround} from '#replacer';
 import {sortByName} from '#sort';
 import Thing from '#thing';
 import thingConstructors from '#things';
@@ -828,36 +829,81 @@ export function parseArtwork({
 }
 
 export function parseContentEntries(thingClass, sourceText, {subdoc}) {
-  const map = matchEntry => ({
-    'Artists':
-      matchEntry.artistReferences
-        .split(',')
-        .map(ref => ref.trim()),
+  function map(matchEntry) {
+    let artistText = null, artistReferences = null;
+
+    const artistTextNodes =
+      Array.from(
+        splitContentNodesAround(
+          parseInput(matchEntry.artistText),
+          /\|/g));
+
+    const separatorIndices =
+      artistTextNodes
+        .filter(node => node.type === 'separator')
+        .map(node => artistTextNodes.indexOf(node));
+
+    if (empty(separatorIndices)) {
+      if (artistTextNodes.length === 1 && artistTextNodes[0].type === 'text') {
+        artistReferences = matchEntry.artistText;
+      } else {
+        artistText = matchEntry.artistText;
+      }
+    } else {
+      const firstSeparatorIndex =
+        separatorIndices.at(0);
+
+      const secondSeparatorIndex =
+        separatorIndices.at(1) ??
+        artistTextNodes.length;
+
+      artistReferences =
+        matchEntry.artistText.slice(
+          artistTextNodes.at(0).i,
+          artistTextNodes.at(firstSeparatorIndex - 1).iEnd);
+
+      artistText =
+        matchEntry.artistText.slice(
+          artistTextNodes.at(firstSeparatorIndex).iEnd,
+          artistTextNodes.at(secondSeparatorIndex - 1).iEnd);
+    }
 
-    'Artist Text':
-      matchEntry.artistText,
+    if (artistReferences) {
+      artistReferences =
+        artistReferences
+          .split(',')
+          .map(ref => ref.trim());
+    }
 
-    'Annotation':
-      matchEntry.annotation,
+    return {
+      'Artists':
+        artistReferences,
 
-    'Date':
-      matchEntry.date,
+      'Artist Text':
+        artistText,
 
-    'Second Date':
-      matchEntry.secondDate,
+      'Annotation':
+        matchEntry.annotation,
 
-    'Date Kind':
-      matchEntry.dateKind,
+      'Date':
+        matchEntry.date,
 
-    'Access Date':
-      matchEntry.accessDate,
+      'Second Date':
+        matchEntry.secondDate,
 
-    'Access Kind':
-      matchEntry.accessKind,
+      'Date Kind':
+        matchEntry.dateKind,
 
-    'Body':
-      matchEntry.body,
-  });
+      'Access Date':
+        matchEntry.accessDate,
+
+      'Access Kind':
+        matchEntry.accessKind,
+
+      'Body':
+        matchEntry.body,
+    };
+  }
 
   const documents =
     matchContentEntries(sourceText)