« get me outta code hell

withSourceText.js « content « things « composite « data « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/data/composite/things/content/withSourceText.js
blob: cfab64a8b4488d88ba33961aada10a977f9a3e1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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,
      }),
    },
  ],
});