« 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/composite/things/content
diff options
context:
space:
mode:
Diffstat (limited to 'src/data/composite/things/content')
-rw-r--r--src/data/composite/things/content/index.js1
-rw-r--r--src/data/composite/things/content/withSourceText.js76
2 files changed, 77 insertions, 0 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,
+      }),
+    },
+  ],
+});