@agentshouse/mdmodel 0.1.0-c679f403bf3fb → 0.1.0-c69a8c1d0563b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/API.md +115 -101
- package/README.md +7 -5
- package/dist/blocks.d.ts.map +1 -1
- package/dist/blocks.js +29 -7
- package/dist/definition.d.ts +2 -1
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +80 -26
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +15 -13
- package/dist/event.d.ts +78 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +230 -0
- package/dist/facts.d.ts +15 -0
- package/dist/facts.d.ts.map +1 -0
- package/dist/facts.js +38 -0
- package/dist/index.d.ts +19 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -4
- package/dist/kinds.d.ts +19 -0
- package/dist/kinds.d.ts.map +1 -0
- package/dist/kinds.js +161 -0
- package/dist/links.d.ts.map +1 -1
- package/dist/links.js +42 -11
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +4 -1
- package/dist/pattern.d.ts +10 -0
- package/dist/pattern.d.ts.map +1 -0
- package/dist/pattern.js +355 -0
- package/dist/query/language.d.ts +223 -0
- package/dist/query/language.d.ts.map +1 -0
- package/dist/query/language.js +565 -0
- package/dist/query/match.d.ts +14 -0
- package/dist/query/match.d.ts.map +1 -0
- package/dist/query/match.js +121 -0
- package/dist/query/rows.d.ts +32 -0
- package/dist/query/rows.d.ts.map +1 -0
- package/dist/query/rows.js +161 -0
- package/dist/query/sql.d.ts +39 -0
- package/dist/query/sql.d.ts.map +1 -0
- package/dist/query/sql.js +279 -0
- package/dist/reserved.d.ts +5 -0
- package/dist/reserved.d.ts.map +1 -0
- package/dist/reserved.js +20 -0
- package/dist/schema.d.ts +1 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +55 -4
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +7 -5
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +230 -87
- package/dist/yaml.d.ts.map +1 -1
- package/dist/yaml.js +36 -3
- package/fixtures/README.md +21 -2
- package/fixtures/cases/conditional-section/context.json +1 -0
- package/fixtures/cases/conditional-section/expected.json +6 -0
- package/fixtures/cases/conditional-section/library/questions/hosting.md +9 -0
- package/fixtures/cases/conditional-section/library/questions/payments.md +12 -0
- package/fixtures/cases/conditional-section/schema.yaml +14 -0
- package/fixtures/cases/link-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/link-into-a-reserved-name/expected.json +5 -0
- package/fixtures/cases/link-into-a-reserved-name/library/notes/a.md +6 -0
- package/fixtures/cases/link-into-a-reserved-name/schema.yaml +3 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/context.json +1 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/expected.json +5 -0
- package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/library/notes/b.md +5 -0
- package/fixtures/cases/member-on-an-untyped-document/context.json +1 -0
- package/fixtures/cases/member-on-an-untyped-document/expected.json +6 -0
- package/fixtures/cases/member-on-an-untyped-document/library/note.md +4 -0
- package/fixtures/cases/member-on-another-type/context.json +1 -0
- package/fixtures/cases/member-on-another-type/expected.json +6 -0
- package/fixtures/cases/member-on-another-type/library/note.md +4 -0
- package/fixtures/cases/member-on-another-type/schema.yaml +7 -0
- package/fixtures/cases/reference-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/expected.json +6 -0
- package/fixtures/cases/reference-into-a-reserved-name/library/notes/a.md +4 -0
- package/fixtures/cases/reference-into-a-reserved-name/library/settings/brief.md +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/schema.yaml +3 -0
- package/fixtures/cases/room-md-declares-a-type/context.json +1 -0
- package/fixtures/cases/room-md-declares-a-type/expected.json +6 -0
- package/fixtures/cases/room-md-declares-a-type/library/ROOM.md +5 -0
- package/fixtures/cases/typed-path-names-another-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-another-type/expected.json +6 -0
- package/fixtures/cases/typed-path-names-another-type/library/settings/prefs/one.md +3 -0
- package/fixtures/cases/typed-path-names-another-type/schema.yaml +6 -0
- package/fixtures/cases/typed-path-names-its-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-its-type/expected.json +3 -0
- package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/deeper/two.md +1 -0
- package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/one.md +4 -0
- package/fixtures/cases/typed-path-names-its-type/schema.yaml +6 -0
- package/fixtures/cases/typed-path-names-no-type/context.json +11 -0
- package/fixtures/cases/typed-path-names-no-type/expected.json +6 -0
- package/fixtures/cases/typed-path-names-no-type/library/settings/prefs/one.md +1 -0
- package/fixtures/cases/typed-path-names-no-type/schema.yaml +6 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/expected.json +6 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/library/rooms/team/tasks/plan.md +4 -0
- package/fixtures/cases/unnamed-typed-path-checks-its-fields/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/expected.json +6 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/library/rooms/team/tasks/plan.md +6 -0
- package/fixtures/cases/unnamed-typed-path-names-a-type/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/expected.json +3 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/plan.md +1 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/tasks/plan.md +5 -0
- package/fixtures/cases/unnamed-typed-path-reads-its-type/schema.yaml +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/context.json +11 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/expected.json +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/library/rooms/team/tasks/plan.md +5 -0
- package/fixtures/cases/unnamed-typed-path-without-a-body/schema.yaml +5 -0
- package/fixtures/queries.json +913 -0
- package/package.json +7 -2
- package/dist/condition.d.ts +0 -162
- package/dist/condition.d.ts.map +0 -1
- package/dist/condition.js +0 -616
package/dist/definition.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MEMBER_FIELD, RECEIVED_FIELD, SITE_FIELD, TYPE_FIELD } from './fields.js';
|
|
2
|
+
import { linearPattern } from './pattern.js';
|
|
2
3
|
import { equalValue, isRecord, jsValue, parseYamlMapping, YamlError } from './yaml.js';
|
|
3
4
|
export const JSON_TYPES = [
|
|
4
5
|
'string',
|
|
@@ -27,8 +28,10 @@ const FIELD_RULES = ['change_roles', 'initial_roles', 'transitions'];
|
|
|
27
28
|
const PROVIDED_KEYWORDS = ['create_roles', 'delete_roles'];
|
|
28
29
|
const NO_TYPES = Object.freeze({ types: Object.freeze({}) });
|
|
29
30
|
const DEFS_REFERENCE = '#/$defs/';
|
|
31
|
+
const UNBOUNDED_FORMAT = 'url';
|
|
30
32
|
const UNRESOLVED = /[\u0000-\u001f\u007f%[\]\\^{|}]|\s$/u;
|
|
31
33
|
export const ARRAY_INDEX = /^(?:0|[1-9][0-9]*)$/u;
|
|
34
|
+
export const JSON_POINTER = /^(?:\/(?:[^~/]|~[01])*)*$/u;
|
|
32
35
|
const HOUSE_KEYS = {
|
|
33
36
|
[TYPE_FIELD]: { prose: false },
|
|
34
37
|
[SITE_FIELD]: { prose: false },
|
|
@@ -47,12 +50,12 @@ export function pointerSegments(pointer) {
|
|
|
47
50
|
return pointer.split('/').slice(1).map(unescapePointer);
|
|
48
51
|
}
|
|
49
52
|
export function refTarget(reference, defs) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (segments.some((segment) => escapePointer(unescapePointer(segment)) !== segment))
|
|
53
|
+
const pointer = reference.slice('#'.length);
|
|
54
|
+
if (!reference.startsWith(DEFS_REFERENCE) ||
|
|
55
|
+
UNRESOLVED.test(reference) ||
|
|
56
|
+
!JSON_POINTER.test(pointer))
|
|
55
57
|
return undefined;
|
|
58
|
+
const [, name = '', ...rest] = pointerSegments(pointer);
|
|
56
59
|
if (!Object.hasOwn(defs, name))
|
|
57
60
|
return undefined;
|
|
58
61
|
let node = defs[name];
|
|
@@ -83,15 +86,7 @@ export function refTarget(reference, defs) {
|
|
|
83
86
|
return { name, node };
|
|
84
87
|
}
|
|
85
88
|
function compiles(pattern) {
|
|
86
|
-
|
|
87
|
-
return false;
|
|
88
|
-
try {
|
|
89
|
-
new RegExp(pattern, 'u');
|
|
90
|
-
return true;
|
|
91
|
-
}
|
|
92
|
-
catch {
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
89
|
+
return typeof pattern === 'string' && linearPattern(pattern) !== null;
|
|
95
90
|
}
|
|
96
91
|
function finite(value) {
|
|
97
92
|
return typeof value === 'number' && Number.isFinite(value);
|
|
@@ -105,7 +100,7 @@ function strings(value) {
|
|
|
105
100
|
new Set(value).size === value.length);
|
|
106
101
|
}
|
|
107
102
|
function schemaFailure(value, at, context) {
|
|
108
|
-
if (typeof value === 'boolean')
|
|
103
|
+
if (!context.checked || typeof value === 'boolean')
|
|
109
104
|
return undefined;
|
|
110
105
|
if (!isRecord(value))
|
|
111
106
|
return refuse('schema', at);
|
|
@@ -201,7 +196,7 @@ const KEYWORDS = {
|
|
|
201
196
|
uniqueItems: FLAG,
|
|
202
197
|
required: strings,
|
|
203
198
|
dependentRequired: (value) => isRecord(value) && Object.values(value).every(strings),
|
|
204
|
-
format:
|
|
199
|
+
format: (value) => typeof value === 'string' && value !== UNBOUNDED_FORMAT,
|
|
205
200
|
contentEncoding: TEXT,
|
|
206
201
|
contentMediaType: TEXT,
|
|
207
202
|
title: TEXT,
|
|
@@ -449,7 +444,7 @@ export function itemFacts(facts, index) {
|
|
|
449
444
|
export function memberFacts(facts, key) {
|
|
450
445
|
if (facts.properties !== undefined && Object.hasOwn(facts.properties, key))
|
|
451
446
|
return facts.properties[key];
|
|
452
|
-
const pattern = facts.patterns?.find((one) =>
|
|
447
|
+
const pattern = facts.patterns?.find((one) => linearPattern(one.pattern).test(key));
|
|
453
448
|
return pattern === undefined ? facts.additional : pattern.facts;
|
|
454
449
|
}
|
|
455
450
|
export function crossedFacts(facts) {
|
|
@@ -552,6 +547,56 @@ function readSections(value, at, context) {
|
|
|
552
547
|
}
|
|
553
548
|
return { sections };
|
|
554
549
|
}
|
|
550
|
+
function branchSectionsFailure(sections, declared, where) {
|
|
551
|
+
if (!isRecord(sections))
|
|
552
|
+
return refuse('sections', where);
|
|
553
|
+
for (const [name, spec] of Object.entries(sections)) {
|
|
554
|
+
const place = `${where}/${escapePointer(name)}`;
|
|
555
|
+
const own = isRecord(declared) && Object.hasOwn(declared, name) ? declared[name] : undefined;
|
|
556
|
+
if (!isRecord(spec) || !isRecord(own))
|
|
557
|
+
return refuse('sections', place);
|
|
558
|
+
const { required, ...kind } = spec;
|
|
559
|
+
const { required: _required, ...ownKind } = own;
|
|
560
|
+
if (required !== undefined && typeof required !== 'boolean')
|
|
561
|
+
return refuse('sections', `${place}/required`);
|
|
562
|
+
if (!equalValue(kind, ownKind))
|
|
563
|
+
return refuse('sections', place);
|
|
564
|
+
}
|
|
565
|
+
return undefined;
|
|
566
|
+
}
|
|
567
|
+
function withoutBranchSections(schema, declared, at) {
|
|
568
|
+
const kept = { ...schema };
|
|
569
|
+
if (Array.isArray(schema.allOf)) {
|
|
570
|
+
const entries = [];
|
|
571
|
+
for (const [index, entry] of schema.allOf.entries()) {
|
|
572
|
+
const read = isRecord(entry)
|
|
573
|
+
? withoutBranchSections(entry, declared, `${at}/allOf/${index}`)
|
|
574
|
+
: { schema: entry };
|
|
575
|
+
if ('failure' in read)
|
|
576
|
+
return read;
|
|
577
|
+
entries.push(read.schema);
|
|
578
|
+
}
|
|
579
|
+
kept.allOf = entries;
|
|
580
|
+
}
|
|
581
|
+
if (schema.if === undefined)
|
|
582
|
+
return { schema: kept };
|
|
583
|
+
for (const branch of ['then', 'else']) {
|
|
584
|
+
const held = schema[branch];
|
|
585
|
+
if (!isRecord(held))
|
|
586
|
+
continue;
|
|
587
|
+
const { sections, ...rest } = held;
|
|
588
|
+
const failure = sections === undefined
|
|
589
|
+
? undefined
|
|
590
|
+
: branchSectionsFailure(sections, declared, `${at}/${branch}/sections`);
|
|
591
|
+
if (failure !== undefined)
|
|
592
|
+
return { failure };
|
|
593
|
+
const read = withoutBranchSections(rest, declared, `${at}/${branch}`);
|
|
594
|
+
if ('failure' in read)
|
|
595
|
+
return read;
|
|
596
|
+
kept[branch] = read.schema;
|
|
597
|
+
}
|
|
598
|
+
return { schema: kept };
|
|
599
|
+
}
|
|
555
600
|
function deepFreeze(value) {
|
|
556
601
|
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
557
602
|
Object.freeze(value);
|
|
@@ -659,12 +704,14 @@ function unruled(schema) {
|
|
|
659
704
|
}
|
|
660
705
|
return { schema: written.size === 0 ? schema : { ...schema, properties }, written };
|
|
661
706
|
}
|
|
662
|
-
function providedType(name, value, provided) {
|
|
707
|
+
function providedType(name, value, provided, checked) {
|
|
663
708
|
const at = `/${escapePointer(name)}`;
|
|
709
|
+
if (checked && provided.nameable?.includes(name) !== true)
|
|
710
|
+
return refuse('provided-type', at);
|
|
664
711
|
if (!isRecord(value))
|
|
665
712
|
return refuse('schema', at);
|
|
666
713
|
const declared = Object.keys(value).find((key) => !PROVIDED_KEYWORDS.includes(key));
|
|
667
|
-
if (declared !== undefined)
|
|
714
|
+
if (checked && declared !== undefined)
|
|
668
715
|
return refuse('provided-type', `${at}/${escapePointer(declared)}`);
|
|
669
716
|
const { create_roles: createRoles, delete_roles: deleteRoles } = value;
|
|
670
717
|
if (createRoles !== undefined && !roleNames(createRoles))
|
|
@@ -672,7 +719,7 @@ function providedType(name, value, provided) {
|
|
|
672
719
|
if (deleteRoles !== undefined && !roleNames(deleteRoles))
|
|
673
720
|
return refuse('delete_roles', `${at}/delete_roles`);
|
|
674
721
|
return {
|
|
675
|
-
...provided,
|
|
722
|
+
...provided.types[name],
|
|
676
723
|
...(createRoles === undefined ? {} : { createRoles }),
|
|
677
724
|
...(deleteRoles === undefined ? {} : { deleteRoles }),
|
|
678
725
|
};
|
|
@@ -693,7 +740,10 @@ function readType(name, value, context) {
|
|
|
693
740
|
: readSections(declared, `${at}/sections`, context);
|
|
694
741
|
if ('failure' in read)
|
|
695
742
|
return read.failure;
|
|
696
|
-
const
|
|
743
|
+
const unbranched = withoutBranchSections(ruled, declared, at);
|
|
744
|
+
if ('failure' in unbranched)
|
|
745
|
+
return unbranched.failure;
|
|
746
|
+
const { schema, written } = unruled(unbranched.schema);
|
|
697
747
|
const failure = schemaFailure(schema, at, context);
|
|
698
748
|
if (failure !== undefined)
|
|
699
749
|
return failure;
|
|
@@ -732,7 +782,7 @@ function readType(name, value, context) {
|
|
|
732
782
|
function lineAt(text, offset) {
|
|
733
783
|
return text.slice(0, offset).split(/\r\n|\r|\n/).length;
|
|
734
784
|
}
|
|
735
|
-
export function readSchema(text, provided = NO_TYPES) {
|
|
785
|
+
export function readSchema(text, provided = NO_TYPES, checked = true) {
|
|
736
786
|
let root;
|
|
737
787
|
try {
|
|
738
788
|
root = jsValue(parseYamlMapping(text, 0, text.length));
|
|
@@ -746,11 +796,15 @@ export function readSchema(text, provided = NO_TYPES) {
|
|
|
746
796
|
if (!isRecord(defs))
|
|
747
797
|
return refuse('$defs', '/$defs');
|
|
748
798
|
const names = Object.keys(root).filter((key) => key !== '$defs');
|
|
749
|
-
for (const name of names) {
|
|
799
|
+
for (const name of checked ? names : []) {
|
|
750
800
|
if (name === '' || name.startsWith('$') || name === BUILT_IN_TYPE || name === ATTACHMENT)
|
|
751
801
|
return refuse('schema-type-name', `/${escapePointer(name)}`);
|
|
752
802
|
}
|
|
753
|
-
const context = {
|
|
803
|
+
const context = {
|
|
804
|
+
defs,
|
|
805
|
+
types: new Set([...names, ...Object.keys(provided.types)]),
|
|
806
|
+
checked,
|
|
807
|
+
};
|
|
754
808
|
for (const [name, def] of Object.entries(defs)) {
|
|
755
809
|
const failure = schemaFailure(def, `/$defs/${escapePointer(name)}`, context);
|
|
756
810
|
if (failure !== undefined)
|
|
@@ -760,11 +814,11 @@ export function readSchema(text, provided = NO_TYPES) {
|
|
|
760
814
|
let marked = false;
|
|
761
815
|
for (const name of names) {
|
|
762
816
|
const definition = Object.hasOwn(provided.types, name)
|
|
763
|
-
? providedType(name, root[name], provided
|
|
817
|
+
? providedType(name, root[name], provided, checked)
|
|
764
818
|
: readType(name, root[name], context);
|
|
765
819
|
if ('rule' in definition)
|
|
766
820
|
return definition;
|
|
767
|
-
if (definition.memberDocument) {
|
|
821
|
+
if (checked && definition.memberDocument) {
|
|
768
822
|
if (marked)
|
|
769
823
|
return refuse('member_document', `/${escapePointer(name)}/member_document`);
|
|
770
824
|
marked = true;
|
package/dist/document.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAKL,KAAK,aAAa,EACnB,MAAM,YAAY,CAAC;AAcpB,OAAO,KAAK,EACV,WAAW,EACX,UAAU,EACV,iBAAiB,EACjB,MAAM,EACN,QAAQ,EACR,IAAI,EACJ,WAAW,EAEZ,MAAM,YAAY,CAAC;AACpB,OAAO,EAcL,KAAK,OAAO,EAEb,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"document.d.ts","sourceRoot":"","sources":["../src/document.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgC,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAKL,KAAK,aAAa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AAG,MAAM,CAEpD;AAED,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO,GAAG,IAAI,CAE9D"}
|
package/dist/document.js
CHANGED
|
@@ -5,29 +5,31 @@ import { references } from './references.js';
|
|
|
5
5
|
import { headingSpans, sectionsRecord } from './sections.js';
|
|
6
6
|
import { refusal } from './refuse.js';
|
|
7
7
|
import { bodyStartAfterYaml, bodyStartOf, equalValue, extractFrontmatter, frontmatterValue, jsValue, lineEnding, mapEntry, parseYamlMapping, serializeJs, serializeKey, YamlError, } from './yaml.js';
|
|
8
|
-
const
|
|
9
|
-
|
|
8
|
+
const HELD = Symbol('held');
|
|
9
|
+
function heldOf(document) {
|
|
10
|
+
return document[HELD];
|
|
11
|
+
}
|
|
10
12
|
export function inventoryOf(document) {
|
|
11
|
-
const held =
|
|
12
|
-
if (held !== undefined)
|
|
13
|
-
return held;
|
|
13
|
+
const held = heldOf(document);
|
|
14
|
+
if (held?.inventory !== undefined)
|
|
15
|
+
return held.inventory;
|
|
14
16
|
const inventory = linkInventory(document.content.slice(bodyStartOf(document.content)));
|
|
15
|
-
|
|
17
|
+
if (held !== undefined)
|
|
18
|
+
held.inventory = inventory;
|
|
16
19
|
return inventory;
|
|
17
20
|
}
|
|
18
21
|
function refuse(file, rule, location) {
|
|
19
22
|
return location === undefined ? refusal(file, rule) : refusal(file, rule, { location });
|
|
20
23
|
}
|
|
21
24
|
function freezeDocument(content, type, fields, preamble, sections, internal) {
|
|
22
|
-
const
|
|
25
|
+
const held = { internal, inventory: undefined };
|
|
26
|
+
return Object.freeze(Object.defineProperty({
|
|
23
27
|
content,
|
|
24
28
|
type,
|
|
25
29
|
fields: Object.freeze(fields),
|
|
26
30
|
preamble,
|
|
27
31
|
sections: Object.freeze(sections),
|
|
28
|
-
});
|
|
29
|
-
internals.set(document, internal);
|
|
30
|
-
return document;
|
|
32
|
+
}, HELD, { value: held }));
|
|
31
33
|
}
|
|
32
34
|
function declaredType(fields) {
|
|
33
35
|
const value = fields.type;
|
|
@@ -222,9 +224,9 @@ export function bodyLinks(body, file) {
|
|
|
222
224
|
return linksIn(hrefOccurrences(body), file);
|
|
223
225
|
}
|
|
224
226
|
function internalOf(document) {
|
|
225
|
-
const held =
|
|
227
|
+
const held = heldOf(document);
|
|
226
228
|
if (held !== undefined)
|
|
227
|
-
return held;
|
|
229
|
+
return held.internal;
|
|
228
230
|
const read = opened(document.content, '');
|
|
229
231
|
if (!read.ok)
|
|
230
232
|
return read.refusal;
|
|
@@ -461,5 +463,5 @@ export function serialize(document) {
|
|
|
461
463
|
return document.content;
|
|
462
464
|
}
|
|
463
465
|
export function frontmatter(document) {
|
|
464
|
-
return
|
|
466
|
+
return heldOf(document)?.internal.root ?? null;
|
|
465
467
|
}
|
package/dist/event.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type Definition } from './definition.js';
|
|
2
|
+
import type { Candidate } from './query/match.js';
|
|
3
|
+
import type { Document, VerifiedModel } from './types.js';
|
|
4
|
+
export type FactKind = 'string' | 'number' | 'boolean' | 'date' | 'time' | 'enum' | 'reference';
|
|
5
|
+
type Scalar = string | number | boolean;
|
|
6
|
+
export type Fact = {
|
|
7
|
+
readonly kind: FactKind;
|
|
8
|
+
readonly value: Scalar;
|
|
9
|
+
readonly member?: string;
|
|
10
|
+
} | {
|
|
11
|
+
readonly kind: 'prose';
|
|
12
|
+
} | {
|
|
13
|
+
readonly kind: 'null';
|
|
14
|
+
} | {
|
|
15
|
+
readonly kind: 'unreadable';
|
|
16
|
+
} | {
|
|
17
|
+
readonly kind: 'list';
|
|
18
|
+
readonly items: readonly Fact[];
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: 'object';
|
|
21
|
+
readonly fields: Readonly<Record<string, Fact>>;
|
|
22
|
+
};
|
|
23
|
+
export type DocumentFacts = {
|
|
24
|
+
readonly id: string;
|
|
25
|
+
readonly path: string;
|
|
26
|
+
readonly type: string | null;
|
|
27
|
+
readonly fields: Readonly<Record<string, Fact>>;
|
|
28
|
+
readonly sections: readonly string[];
|
|
29
|
+
};
|
|
30
|
+
export declare const TEXT_FIELD = "/text";
|
|
31
|
+
export declare const WRITE_DOORS: readonly ['browser', 'agent', 'client', 'function'];
|
|
32
|
+
export type WriteDoor = (typeof WRITE_DOORS)[number];
|
|
33
|
+
export type Actor = {
|
|
34
|
+
readonly user?: string;
|
|
35
|
+
readonly site?: string;
|
|
36
|
+
readonly app?: string;
|
|
37
|
+
readonly door?: WriteDoor;
|
|
38
|
+
};
|
|
39
|
+
export type DocumentEvent = {
|
|
40
|
+
readonly before: DocumentFacts | null;
|
|
41
|
+
readonly after: DocumentFacts | null;
|
|
42
|
+
readonly changed: readonly string[];
|
|
43
|
+
readonly by: Actor;
|
|
44
|
+
readonly moved_between_rooms: boolean;
|
|
45
|
+
};
|
|
46
|
+
export type ItemFacts = {
|
|
47
|
+
readonly delivery: string;
|
|
48
|
+
readonly source: string | null;
|
|
49
|
+
readonly address: string | null;
|
|
50
|
+
readonly container: string | null;
|
|
51
|
+
readonly author: string | null;
|
|
52
|
+
readonly headers: Readonly<Record<string, string>>;
|
|
53
|
+
readonly json: {
|
|
54
|
+
readonly value: unknown;
|
|
55
|
+
} | null;
|
|
56
|
+
};
|
|
57
|
+
export type MessageFacts = {
|
|
58
|
+
readonly sender: string;
|
|
59
|
+
readonly agent_function: string | null;
|
|
60
|
+
readonly path: string | null;
|
|
61
|
+
};
|
|
62
|
+
export type Resolved = {
|
|
63
|
+
readonly id: string;
|
|
64
|
+
readonly member?: string;
|
|
65
|
+
};
|
|
66
|
+
export declare function facts(document: Pick<Document, 'type' | 'fields' | 'sections'>, at: {
|
|
67
|
+
readonly id: string;
|
|
68
|
+
readonly path: string;
|
|
69
|
+
}, model: VerifiedModel, resolve: (target: string) => Resolved | undefined): DocumentFacts;
|
|
70
|
+
export declare function prosePaths(facts: DocumentFacts): string[];
|
|
71
|
+
export declare function members(document: DocumentFacts, path: string): readonly string[];
|
|
72
|
+
export declare function pointed(value: unknown, pointer: string): unknown;
|
|
73
|
+
export declare function valuedChange(definition: Definition, pointer: string): boolean;
|
|
74
|
+
export declare function datePointers(definition: Definition): string[];
|
|
75
|
+
export declare function memberPointers(definition: Definition, memberType: string): string[];
|
|
76
|
+
export declare function candidateOf(document: DocumentFacts): Candidate;
|
|
77
|
+
export {};
|
|
78
|
+
//# sourceMappingURL=event.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.d.ts","sourceRoot":"","sources":["../src/event.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,KAAK,UAAU,EAEhB,MAAM,iBAAiB,CAAC;AAGzB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAG1D,MAAM,MAAM,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,WAAW,CAAC;AAEhG,KAAK,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAExC,MAAM,MAAM,IAAI,GACZ;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GAC7E;IAAE,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAA;CAAE,GAC1B;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GACzB;IAAE,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAA;CAAE,GAC/B;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,SAAS,IAAI,EAAE,CAAA;CAAE,GAC1D;IAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IAAC,QAAQ,CAA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AM,GAAG,SAAS,MAAM,EAAE,CAWhF;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAahE;AAED,wBAAgB,YAAY,CAAC,UAAU,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAS7E;AAED,wBAAgB,YAAY,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,EAAE,CAK7D;AAED,wBAAgB,cAAc,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,EAAE,CAOnF;AA+BD,wBAAgB,WAAW,CAAC,QAAQ,EAAE,aAAa,GAAG,SAAS,CAiB9D"}
|
package/dist/event.js
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { ARRAY_INDEX, escapePointer, fieldFacts, itemFacts, memberFacts, pointerFacts, pointerSegments, } from './definition.js';
|
|
2
|
+
import { dated, instant, kindOf } from './kinds.js';
|
|
3
|
+
import { roomPath, target } from './paths.js';
|
|
4
|
+
import { plain } from './yaml.js';
|
|
5
|
+
export const TEXT_FIELD = '/text';
|
|
6
|
+
export const WRITE_DOORS = ['browser', 'agent', 'client', 'function'];
|
|
7
|
+
const PROSE = { kind: 'prose' };
|
|
8
|
+
const NULL = { kind: 'null' };
|
|
9
|
+
const UNREADABLE = { kind: 'unreadable' };
|
|
10
|
+
function factKind(facts) {
|
|
11
|
+
const kind = kindOf(facts);
|
|
12
|
+
if (kind === 'integer')
|
|
13
|
+
return 'number';
|
|
14
|
+
if (kind === 'date-time')
|
|
15
|
+
return 'time';
|
|
16
|
+
if (kind === 'time')
|
|
17
|
+
return 'string';
|
|
18
|
+
return kind;
|
|
19
|
+
}
|
|
20
|
+
function scalar(value) {
|
|
21
|
+
return (typeof value === 'string' ||
|
|
22
|
+
typeof value === 'boolean' ||
|
|
23
|
+
(typeof value === 'number' && Number.isFinite(value)));
|
|
24
|
+
}
|
|
25
|
+
function fits(kind, facts, value) {
|
|
26
|
+
if (!scalar(value))
|
|
27
|
+
return false;
|
|
28
|
+
if (kind === 'enum')
|
|
29
|
+
return (facts.values ?? []).includes(value);
|
|
30
|
+
if (kind === 'number')
|
|
31
|
+
return typeof value === 'number';
|
|
32
|
+
if (kind === 'boolean')
|
|
33
|
+
return typeof value === 'boolean';
|
|
34
|
+
if (typeof value !== 'string')
|
|
35
|
+
return false;
|
|
36
|
+
if (kind === 'date')
|
|
37
|
+
return dated(value);
|
|
38
|
+
if (kind === 'time')
|
|
39
|
+
return instant(value) !== undefined;
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
function factOf(value, declared, place) {
|
|
43
|
+
if (value === null)
|
|
44
|
+
return NULL;
|
|
45
|
+
if (Array.isArray(value)) {
|
|
46
|
+
if (declared?.type !== undefined && declared.type !== 'array')
|
|
47
|
+
return UNREADABLE;
|
|
48
|
+
return {
|
|
49
|
+
kind: 'list',
|
|
50
|
+
items: value.map((item, index) => factOf(item, declared === undefined ? undefined : itemFacts(declared, index), place)),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
if (plain(value)) {
|
|
54
|
+
if (declared?.type !== undefined && declared.type !== 'object')
|
|
55
|
+
return UNREADABLE;
|
|
56
|
+
const fields = Object.create(null);
|
|
57
|
+
for (const [key, held] of Object.entries(value))
|
|
58
|
+
fields[key] = factOf(held, declared === undefined ? undefined : memberFacts(declared, key), place);
|
|
59
|
+
return { kind: 'object', fields };
|
|
60
|
+
}
|
|
61
|
+
if (declared?.type === 'array' || declared?.type === 'object')
|
|
62
|
+
return UNREADABLE;
|
|
63
|
+
const kind = declared === undefined || declared.prose ? null : factKind(declared);
|
|
64
|
+
if (kind === null)
|
|
65
|
+
return PROSE;
|
|
66
|
+
if (!fits(kind, declared, value))
|
|
67
|
+
return UNREADABLE;
|
|
68
|
+
if (kind !== 'reference')
|
|
69
|
+
return { kind, value };
|
|
70
|
+
const reached = target(place.path, value) ?? roomPath(value);
|
|
71
|
+
const resolved = reached === null ? undefined : place.resolve(reached);
|
|
72
|
+
if (resolved === undefined)
|
|
73
|
+
return UNREADABLE;
|
|
74
|
+
return resolved.member === undefined
|
|
75
|
+
? { kind, value: resolved.id }
|
|
76
|
+
: { kind, value: resolved.id, member: resolved.member };
|
|
77
|
+
}
|
|
78
|
+
export function facts(document, at, model, resolve) {
|
|
79
|
+
const type = document.type ?? null;
|
|
80
|
+
const definition = type !== null && Object.hasOwn(model.types, type) ? model.types[type] : null;
|
|
81
|
+
const fields = Object.create(null);
|
|
82
|
+
const place = { path: at.path, resolve };
|
|
83
|
+
for (const [name, value] of Object.entries(document.fields)) {
|
|
84
|
+
const declared = definition === null ? undefined : fieldFacts(definition, name);
|
|
85
|
+
if (declared !== undefined)
|
|
86
|
+
fields[name] = factOf(value, declared, place);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
id: at.id,
|
|
90
|
+
path: `/${at.path}`,
|
|
91
|
+
type,
|
|
92
|
+
fields,
|
|
93
|
+
sections: Object.keys(document.sections).sort(),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
function rootOf(facts, prefix) {
|
|
97
|
+
return { pointer: prefix, fact: { kind: 'object', fields: facts.fields } };
|
|
98
|
+
}
|
|
99
|
+
export function prosePaths(facts) {
|
|
100
|
+
const paths = facts.sections.map((heading) => `/sections/${heading}`);
|
|
101
|
+
const visit = ({ pointer, fact }) => {
|
|
102
|
+
if (fact.kind === 'prose')
|
|
103
|
+
paths.push(pointer);
|
|
104
|
+
if (fact.kind === 'list')
|
|
105
|
+
fact.items.forEach((item, index) => visit({ pointer: `${pointer}/${index}`, fact: item }));
|
|
106
|
+
if (fact.kind === 'object')
|
|
107
|
+
for (const [key, held] of Object.entries(fact.fields))
|
|
108
|
+
visit({ pointer: `${pointer}/${escapePointer(key)}`, fact: held });
|
|
109
|
+
};
|
|
110
|
+
visit(rootOf(facts, '/fields'));
|
|
111
|
+
return paths.sort();
|
|
112
|
+
}
|
|
113
|
+
function walked(start, pointer) {
|
|
114
|
+
let reached = [start];
|
|
115
|
+
for (const token of pointerSegments(pointer)) {
|
|
116
|
+
const step = ({ pointer: at, fact }) => {
|
|
117
|
+
if (fact.kind === 'object')
|
|
118
|
+
return Object.hasOwn(fact.fields, token)
|
|
119
|
+
? [{ pointer: `${at}/${escapePointer(token)}`, fact: fact.fields[token] }]
|
|
120
|
+
: [];
|
|
121
|
+
if (fact.kind !== 'list')
|
|
122
|
+
return [];
|
|
123
|
+
if (ARRAY_INDEX.test(token)) {
|
|
124
|
+
const item = fact.items[Number(token)];
|
|
125
|
+
return item === undefined ? [] : [{ pointer: `${at}/${token}`, fact: item }];
|
|
126
|
+
}
|
|
127
|
+
return fact.items
|
|
128
|
+
.map((item, index) => ({ pointer: `${at}/${index}`, fact: item }))
|
|
129
|
+
.flatMap(step);
|
|
130
|
+
};
|
|
131
|
+
reached = reached.flatMap(step);
|
|
132
|
+
}
|
|
133
|
+
return reached;
|
|
134
|
+
}
|
|
135
|
+
function expand(fact) {
|
|
136
|
+
return fact.kind === 'list' ? fact.items.flatMap(expand) : [fact];
|
|
137
|
+
}
|
|
138
|
+
export function members(document, path) {
|
|
139
|
+
if (!path.startsWith('/members/'))
|
|
140
|
+
return [];
|
|
141
|
+
return [
|
|
142
|
+
...new Set(walked(rootOf(document, '/members'), path.slice('/members'.length))
|
|
143
|
+
.flatMap(({ fact }) => expand(fact))
|
|
144
|
+
.flatMap((fact) => fact.kind === 'reference' && fact.member !== undefined ? [fact.member] : [])),
|
|
145
|
+
].sort();
|
|
146
|
+
}
|
|
147
|
+
export function pointed(value, pointer) {
|
|
148
|
+
let reached = value;
|
|
149
|
+
for (const token of pointerSegments(pointer)) {
|
|
150
|
+
if (Array.isArray(reached)) {
|
|
151
|
+
if (!ARRAY_INDEX.test(token))
|
|
152
|
+
return undefined;
|
|
153
|
+
reached = reached[Number(token)];
|
|
154
|
+
}
|
|
155
|
+
else if (plain(reached) && Object.hasOwn(reached, token)) {
|
|
156
|
+
reached = reached[token];
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
return undefined;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return reached;
|
|
163
|
+
}
|
|
164
|
+
export function valuedChange(definition, pointer) {
|
|
165
|
+
const reached = pointerFacts(definition, pointer);
|
|
166
|
+
return (reached !== undefined &&
|
|
167
|
+
!reached.through &&
|
|
168
|
+
reached.facts.type !== 'array' &&
|
|
169
|
+
!reached.facts.prose &&
|
|
170
|
+
kindOf(reached.facts) !== null);
|
|
171
|
+
}
|
|
172
|
+
export function datePointers(definition) {
|
|
173
|
+
return Object.entries(definition.fields)
|
|
174
|
+
.filter(([, held]) => kindOf(held) === 'date')
|
|
175
|
+
.map(([name]) => `/${escapePointer(name)}`)
|
|
176
|
+
.sort();
|
|
177
|
+
}
|
|
178
|
+
export function memberPointers(definition, memberType) {
|
|
179
|
+
return Object.entries(definition.fields)
|
|
180
|
+
.filter(([, held]) => (held.type === 'array' ? held.items : held)?.reference?.includes(memberType) === true)
|
|
181
|
+
.map(([name]) => `/members/${escapePointer(name)}`);
|
|
182
|
+
}
|
|
183
|
+
function plainOf(fact) {
|
|
184
|
+
if (fact.kind === 'list')
|
|
185
|
+
return fact.items.map(plainOf);
|
|
186
|
+
if (fact.kind === 'object')
|
|
187
|
+
return Object.fromEntries(Object.entries(fact.fields).flatMap(([key, held]) => {
|
|
188
|
+
const value = plainOf(held);
|
|
189
|
+
return value === undefined ? [] : [[key, value]];
|
|
190
|
+
}));
|
|
191
|
+
if (fact.kind === 'prose')
|
|
192
|
+
return true;
|
|
193
|
+
if (fact.kind === 'null')
|
|
194
|
+
return null;
|
|
195
|
+
if (fact.kind === 'unreadable')
|
|
196
|
+
return undefined;
|
|
197
|
+
return fact.value;
|
|
198
|
+
}
|
|
199
|
+
function shapeOf(fact) {
|
|
200
|
+
if (fact.kind === 'list')
|
|
201
|
+
return { type: 'array', prose: false, prefix: fact.items.map(shapeOf) };
|
|
202
|
+
if (fact.kind === 'object')
|
|
203
|
+
return {
|
|
204
|
+
type: 'object',
|
|
205
|
+
prose: false,
|
|
206
|
+
properties: Object.fromEntries(Object.entries(fact.fields).map(([key, held]) => [key, shapeOf(held)])),
|
|
207
|
+
};
|
|
208
|
+
if (fact.kind === 'time')
|
|
209
|
+
return { type: 'string', format: 'date-time', prose: false };
|
|
210
|
+
return { prose: false };
|
|
211
|
+
}
|
|
212
|
+
export function candidateOf(document) {
|
|
213
|
+
const fields = {};
|
|
214
|
+
const properties = {};
|
|
215
|
+
for (const [name, fact] of Object.entries(document.fields)) {
|
|
216
|
+
const value = plainOf(fact);
|
|
217
|
+
if (value === undefined)
|
|
218
|
+
continue;
|
|
219
|
+
fields[name] = value;
|
|
220
|
+
properties[name] = shapeOf(fact);
|
|
221
|
+
}
|
|
222
|
+
return {
|
|
223
|
+
path: document.path.slice(1),
|
|
224
|
+
type: document.type,
|
|
225
|
+
fileId: document.id,
|
|
226
|
+
fields,
|
|
227
|
+
definition: { frontmatter: { type: 'object', prose: false, properties } },
|
|
228
|
+
text: null,
|
|
229
|
+
};
|
|
230
|
+
}
|
package/dist/facts.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Document, ParseResult, VerifiedModel } from './types.js';
|
|
2
|
+
export type SavedFacts = {
|
|
3
|
+
readonly type: string;
|
|
4
|
+
readonly fields: Readonly<Record<string, unknown>>;
|
|
5
|
+
readonly links: readonly string[];
|
|
6
|
+
};
|
|
7
|
+
export type ReferenceTarget = {
|
|
8
|
+
readonly location: string;
|
|
9
|
+
readonly to: readonly string[];
|
|
10
|
+
readonly target: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function referenceTarget(file: string, value: string): string | null;
|
|
13
|
+
export declare function referenceTargets(document: Document, file: string, model: VerifiedModel): readonly ReferenceTarget[];
|
|
14
|
+
export declare function documentFacts(file: string, parsed: ParseResult, model: VerifiedModel): SavedFacts | null;
|
|
15
|
+
//# sourceMappingURL=facts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"facts.d.ts","sourceRoot":"","sources":["../src/facts.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAEvE,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACnD,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC,CAAC;AAEF,MAAM,MAAM,eAAe,GAAG;IAC5B,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,EAAE,EAAE,SAAS,MAAM,EAAE,CAAC;IAC/B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB,CAAC;AAIF,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAK1E;AAED,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,aAAa,GACnB,SAAS,eAAe,EAAE,CAO5B;AAED,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,WAAW,EACnB,KAAK,EAAE,aAAa,GACnB,UAAU,GAAG,IAAI,CAenB"}
|
package/dist/facts.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { BUILT_IN_TYPE } from './definition.js';
|
|
2
|
+
import { links, parse } from './document.js';
|
|
3
|
+
import { references } from './references.js';
|
|
4
|
+
const SETTINGS_AREA = 'settings/';
|
|
5
|
+
export function referenceTarget(file, value) {
|
|
6
|
+
if (value.trim() === '' || /[<>\n\r]/.test(value))
|
|
7
|
+
return null;
|
|
8
|
+
const parsed = parse(`[](<${value}>)\n`, file);
|
|
9
|
+
if (!parsed.ok)
|
|
10
|
+
return null;
|
|
11
|
+
return links(parsed.document, file)[0]?.target ?? null;
|
|
12
|
+
}
|
|
13
|
+
export function referenceTargets(document, file, model) {
|
|
14
|
+
const type = document.type;
|
|
15
|
+
if (type === undefined || !Object.hasOwn(model.types, type))
|
|
16
|
+
return [];
|
|
17
|
+
return references(document, model.types[type]).flatMap(({ pointer, value, to }) => {
|
|
18
|
+
const target = referenceTarget(file, value);
|
|
19
|
+
return target === null ? [] : [{ location: pointer, to, target }];
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
export function documentFacts(file, parsed, model) {
|
|
23
|
+
if (!parsed.ok)
|
|
24
|
+
return null;
|
|
25
|
+
const { document } = parsed;
|
|
26
|
+
return {
|
|
27
|
+
type: document.type ?? BUILT_IN_TYPE,
|
|
28
|
+
fields: document.fields,
|
|
29
|
+
links: file.startsWith(SETTINGS_AREA)
|
|
30
|
+
? []
|
|
31
|
+
: [
|
|
32
|
+
...new Set([
|
|
33
|
+
...links(document, file).map((link) => link.target),
|
|
34
|
+
...referenceTargets(document, file, model).map(({ target }) => target),
|
|
35
|
+
]),
|
|
36
|
+
].sort(),
|
|
37
|
+
};
|
|
38
|
+
}
|