« 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
diff options
context:
space:
mode:
Diffstat (limited to 'src/data')
-rw-r--r--src/data/composite/things/content/index.js1
-rw-r--r--src/data/composite/things/content/withSourceText.js76
-rw-r--r--src/data/things/content.js10
3 files changed, 86 insertions, 1 deletions
diff --git a/src/data/composite/things/content/index.js b/src/data/composite/things/content/index.js
index 091bae1a..1ee188c5 100644
--- a/src/data/composite/things/content/index.js
+++ b/src/data/composite/things/content/index.js
@@ -1 +1,2 @@
+export {default as withSourceText} from './withSourceText.js';
 export {default as withWebArchiveDate} from './withWebArchiveDate.js';
diff --git a/src/data/composite/things/content/withSourceText.js b/src/data/composite/things/content/withSourceText.js
new file mode 100644
index 00000000..cfab64a8
--- /dev/null
+++ b/src/data/composite/things/content/withSourceText.js
@@ -0,0 +1,76 @@
+import * as marked from 'marked';
+
+import {input, templateCompositeFrom} from '#composite';
+import {matchMarkdownLinks} from '#wiki-data';
+
+import {raiseOutputWithoutDependency} from '#composite/control-flow';
+
+export default templateCompositeFrom({
+  annotation: `withSourceText`,
+
+  outputs: ['#sourceText'],
+
+  steps: () => [
+    raiseOutputWithoutDependency({
+      dependency: 'annotation',
+      output: input.value({'#sourceText': null}),
+    }),
+
+    {
+      dependencies: ['annotation'],
+      compute: (continuation, {
+        ['annotation']: annotation,
+      }) => continuation({
+        ['#matches']:
+          Array.from(matchMarkdownLinks(annotation, {marked})),
+      }),
+    },
+
+    raiseOutputWithoutDependency({
+      dependency: '#matches',
+      output: input.value({'#sourceText': null}),
+      mode: input.value('empty'),
+    }),
+
+    {
+      dependencies: ['#matches'],
+      compute: (continuation, {
+        ['#matches']: matches,
+      }) =>
+        continuation({
+          ['#startIndex']:
+            matches.at(0).index,
+
+          ['#endIndex']:
+            matches.at(-1).index +
+            matches.at(-1).length,
+        }),
+    },
+
+    {
+      dependencies: ['annotation', '#endIndex'],
+      compute: (continuation, {
+        ['annotation']: annotation,
+        ['#endIndex']: endIndex,
+      }) => continuation({
+        ['#rest']:
+          annotation.slice(endIndex)
+            .match(/^[^,]*(?=,|$)/),
+      }),
+    },
+
+    {
+      dependencies: ['annotation', '#startIndex', '#endIndex', '#rest'],
+      compute: (continuation, {
+        ['annotation']: annotation,
+        ['#startIndex']: startIndex,
+        ['#endIndex']: endIndex,
+        ['#rest']: rest,
+      }) => continuation({
+        ['#sourceText']:
+          annotation.slice(startIndex, startIndex + endIndex) +
+          rest,
+      }),
+    },
+  ],
+});
diff --git a/src/data/things/content.js b/src/data/things/content.js
index 7cf487f1..d68fd5be 100644
--- a/src/data/things/content.js
+++ b/src/data/things/content.js
@@ -9,12 +9,13 @@ import {contentString, referenceList, simpleDate, soupyFind, thing}
 
 import {
   exposeConstant,
+  exposeDependency,
   exposeDependencyOrContinue,
   exposeUpdateValueOrContinue,
   withResultOfAvailabilityCheck,
 } from '#composite/control-flow';
 
-import {withWebArchiveDate} from '#composite/things/content';
+import {withWebArchiveDate, withSourceText} from '#composite/things/content';
 
 export class ContentEntry extends Thing {
   static [Thing.getPropertyDescriptors] = ({Artist}) => ({
@@ -96,6 +97,13 @@ export class ContentEntry extends Thing {
     // Update only
 
     find: soupyFind(),
+
+    // Expose only
+
+    sourceText: [
+      withSourceText(),
+      exposeDependency({dependency: '#sourceText'}),
+    ],
   });
 
   static [Thing.yamlDocumentSpec] = {