From aefa7862874e51c07d265e8505ead5662d2158e1 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Thu, 1 May 2025 13:07:22 -0300 Subject: data: ContentEntry.withSourceText (first try) --- src/data/composite/things/content/index.js | 1 + .../composite/things/content/withSourceText.js | 76 ++++++++++++++++++++++ src/data/things/content.js | 10 ++- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/data/composite/things/content/withSourceText.js (limited to 'src/data') 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] = { -- cgit 1.3.0-6-gf8a5