diff options
Diffstat (limited to 'src/data/composite/things/content/withSourceText.js')
-rw-r--r-- | src/data/composite/things/content/withSourceText.js | 76 |
1 files changed, 76 insertions, 0 deletions
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, + }), + }, + ], +}); |