diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2025-05-01 13:07:22 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-05-06 12:29:05 -0300 |
commit | aefa7862874e51c07d265e8505ead5662d2158e1 (patch) | |
tree | fa412c62ad9b32c1a3bdd004d6d37499dc840849 /src/data | |
parent | cad44bfd247272ea8c11764d3b65dfd6a1b2b224 (diff) |
data: ContentEntry.withSourceText (first try)
Diffstat (limited to 'src/data')
-rw-r--r-- | src/data/composite/things/content/index.js | 1 | ||||
-rw-r--r-- | src/data/composite/things/content/withSourceText.js | 76 | ||||
-rw-r--r-- | src/data/things/content.js | 10 |
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] = { |