@agentshouse/mdmodel 0.1.0-c04c0d681d2d4 → 0.1.0-c08b2adadd2fe
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 +150 -110
- package/README.md +8 -6
- package/dist/definition.d.ts +3 -1
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +138 -26
- package/dist/divide.d.ts.map +1 -1
- package/dist/divide.js +5 -4
- package/dist/document.d.ts +9 -5
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +169 -67
- package/dist/event.d.ts +78 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +201 -0
- package/dist/facts.d.ts +15 -0
- package/dist/facts.d.ts.map +1 -0
- package/dist/facts.js +41 -0
- package/dist/index.d.ts +23 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -7
- package/dist/kinds.d.ts +18 -0
- package/dist/kinds.d.ts.map +1 -0
- package/dist/kinds.js +150 -0
- package/dist/markdown.d.ts +51 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +271 -0
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +9 -2
- package/dist/pattern.d.ts +10 -0
- package/dist/pattern.d.ts.map +1 -0
- package/dist/pattern.js +356 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +16 -21
- package/dist/query/language.d.ts +254 -0
- package/dist/query/language.d.ts.map +1 -0
- package/dist/query/language.js +620 -0
- package/dist/query/match.d.ts +14 -0
- package/dist/query/match.d.ts.map +1 -0
- package/dist/query/match.js +150 -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 +285 -0
- package/dist/reserved.d.ts +6 -0
- package/dist/reserved.d.ts.map +1 -0
- package/dist/reserved.js +21 -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 +19 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +29 -5
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +327 -161
- package/dist/yaml.d.ts.map +1 -1
- package/dist/yaml.js +40 -14
- package/fixtures/README.md +25 -3
- 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/lazy-list-item/context.json +1 -0
- package/fixtures/cases/lazy-list-item/expected.json +3 -0
- package/fixtures/cases/lazy-list-item/library/item.md +8 -0
- package/fixtures/cases/lazy-list-item/schema.yaml +3 -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/thematic-break-in-a-list-section/context.json +1 -0
- package/fixtures/cases/thematic-break-in-a-list-section/expected.json +6 -0
- package/fixtures/cases/thematic-break-in-a-list-section/library/item.md +7 -0
- package/fixtures/cases/thematic-break-in-a-list-section/schema.yaml +3 -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/division.json +47 -2
- package/fixtures/preamble.json +24 -3
- package/fixtures/projection.json +105 -2
- package/fixtures/queries.json +964 -0
- package/package.json +16 -2
- package/dist/blocks.d.ts +0 -11
- package/dist/blocks.d.ts.map +0 -1
- package/dist/blocks.js +0 -69
- package/dist/condition.d.ts +0 -162
- package/dist/condition.d.ts.map +0 -1
- package/dist/condition.js +0 -616
- package/dist/links.d.ts +0 -29
- package/dist/links.d.ts.map +0 -1
- package/dist/links.js +0 -788
- package/dist/profile.d.ts +0 -6
- package/dist/profile.d.ts.map +0 -1
- package/dist/profile.js +0 -5
- package/dist/sections.d.ts +0 -11
- package/dist/sections.d.ts.map +0 -1
- package/dist/sections.js +0 -57
package/dist/document.js
CHANGED
|
@@ -1,38 +1,62 @@
|
|
|
1
1
|
import { ARRAY_INDEX, pointerSegments } from './definition.js';
|
|
2
|
-
import {
|
|
2
|
+
import { TYPE_FIELD } from './fields.js';
|
|
3
|
+
import { destinations, hrefOccurrences, read, sectionSpans, sectionsRecord, } from './markdown.js';
|
|
3
4
|
import { destinationPath, relative, retarget, roomPath, roomTarget, SHELL_ROOMS, shellTarget, target, } from './paths.js';
|
|
4
5
|
import { references } from './references.js';
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
const HELD = Symbol('held');
|
|
9
|
+
function heldOf(document) {
|
|
10
|
+
return document[HELD];
|
|
11
|
+
}
|
|
12
|
+
function bodyReading(content, bodyStart) {
|
|
13
|
+
return read(content.slice(bodyStart), bodyStart);
|
|
14
|
+
}
|
|
15
|
+
function heldReading(content, held) {
|
|
16
|
+
held.reading ??= bodyReading(content, held.internal.bodyStart);
|
|
17
|
+
return held.reading;
|
|
18
|
+
}
|
|
19
|
+
export function readingOf(document) {
|
|
20
|
+
const held = heldOf(document);
|
|
21
|
+
return held === undefined
|
|
22
|
+
? bodyReading(document.content, bodyStartOf(document.content))
|
|
23
|
+
: heldReading(document.content, held);
|
|
24
|
+
}
|
|
25
|
+
export function linkDestinations(document) {
|
|
26
|
+
const bodyStart = heldOf(document)?.internal.bodyStart ?? bodyStartOf(document.content);
|
|
27
|
+
return document.content.includes('[', bodyStart) ? destinations(readingOf(document)) : [];
|
|
28
|
+
}
|
|
29
|
+
function heldParts(content, held) {
|
|
30
|
+
if (held.parts !== undefined)
|
|
31
|
+
return held.parts;
|
|
32
|
+
const spans = sectionSpans(heldReading(content, held), content.length);
|
|
33
|
+
held.parts = {
|
|
34
|
+
preamble: content.slice(held.internal.bodyStart, spans[0]?.headingStart),
|
|
35
|
+
sections: Object.freeze(sectionsRecord(spans, content)),
|
|
36
|
+
};
|
|
37
|
+
return held.parts;
|
|
38
|
+
}
|
|
39
|
+
function bodyStartIn(document) {
|
|
40
|
+
return heldOf(document)?.internal.bodyStart ?? bodyStartOf(document.content);
|
|
41
|
+
}
|
|
42
|
+
export function textSpans(document) {
|
|
43
|
+
const start = bodyStartIn(document);
|
|
44
|
+
return [{ start, end: document.content.length, text: document.content.slice(start) }];
|
|
17
45
|
}
|
|
18
46
|
function refuse(file, rule, location) {
|
|
19
47
|
return location === undefined ? refusal(file, rule) : refusal(file, rule, { location });
|
|
20
48
|
}
|
|
21
|
-
function freezeDocument(content, type, fields,
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
});
|
|
29
|
-
internals.set(document, internal);
|
|
30
|
-
return document;
|
|
49
|
+
function freezeDocument(content, type, fields, internal) {
|
|
50
|
+
const held = { internal };
|
|
51
|
+
return Object.freeze(Object.defineProperties({ content, type, fields: Object.freeze(fields) }, {
|
|
52
|
+
preamble: { enumerable: true, get: () => heldParts(content, held).preamble },
|
|
53
|
+
sections: { enumerable: true, get: () => heldParts(content, held).sections },
|
|
54
|
+
text: { enumerable: true, get: () => content.slice(internal.bodyStart) },
|
|
55
|
+
[HELD]: { value: held },
|
|
56
|
+
}));
|
|
31
57
|
}
|
|
32
58
|
function declaredType(fields) {
|
|
33
59
|
const value = fields.type;
|
|
34
|
-
if (value === undefined || value === null)
|
|
35
|
-
return undefined;
|
|
36
60
|
if (typeof value !== 'string' || value === '')
|
|
37
61
|
return undefined;
|
|
38
62
|
return value;
|
|
@@ -46,9 +70,6 @@ function opened(content, file) {
|
|
|
46
70
|
try {
|
|
47
71
|
const root = parseYamlMapping(content, fence.yamlStart, fence.yamlEnd);
|
|
48
72
|
const fields = jsValue(root);
|
|
49
|
-
if (fields === null || typeof fields !== 'object' || Array.isArray(fields)) {
|
|
50
|
-
return { ok: false, refusal: refuse(file, 'frontmatter-fence') };
|
|
51
|
-
}
|
|
52
73
|
return { ok: true, root, fields: fields, yamlEnd: fence.yamlEnd };
|
|
53
74
|
}
|
|
54
75
|
catch (error) {
|
|
@@ -58,29 +79,38 @@ function opened(content, file) {
|
|
|
58
79
|
}
|
|
59
80
|
}
|
|
60
81
|
export function parseFrontmatter(content, file = '') {
|
|
61
|
-
const
|
|
62
|
-
if (!
|
|
63
|
-
return
|
|
82
|
+
const opening = opened(content, file);
|
|
83
|
+
if (!opening.ok)
|
|
84
|
+
return opening;
|
|
64
85
|
return {
|
|
65
86
|
ok: true,
|
|
66
87
|
frontmatter: Object.freeze({
|
|
67
88
|
content,
|
|
68
|
-
type: declaredType(
|
|
69
|
-
fields: Object.freeze(
|
|
89
|
+
type: declaredType(opening.fields),
|
|
90
|
+
fields: Object.freeze(opening.fields),
|
|
70
91
|
}),
|
|
71
92
|
};
|
|
72
93
|
}
|
|
73
|
-
export function parse(content, file = '') {
|
|
74
|
-
const
|
|
75
|
-
if (!
|
|
76
|
-
return
|
|
77
|
-
const bodyStart =
|
|
78
|
-
const spans = headingSpans(content, bodyStart);
|
|
94
|
+
export function parse(content, file = '', definition) {
|
|
95
|
+
const opening = opened(content, file);
|
|
96
|
+
if (!opening.ok)
|
|
97
|
+
return opening;
|
|
98
|
+
const bodyStart = opening.root === null ? 0 : bodyStartAfterYaml(content, opening.yamlEnd);
|
|
79
99
|
return {
|
|
80
100
|
ok: true,
|
|
81
|
-
document: freezeDocument(content, declaredType(
|
|
101
|
+
document: freezeDocument(content, declaredType(opening.fields), opening.fields, {
|
|
102
|
+
root: opening.root,
|
|
103
|
+
yamlEnd: opening.yamlEnd,
|
|
104
|
+
bodyStart,
|
|
105
|
+
}),
|
|
82
106
|
};
|
|
83
107
|
}
|
|
108
|
+
export function create(type, definition) {
|
|
109
|
+
const parsed = parse(type === undefined ? '---\n---\n' : `---\n${TYPE_FIELD}: ${serializeJs(type)}\n---\n`, '', definition);
|
|
110
|
+
if (!parsed.ok)
|
|
111
|
+
throw new Error(`a document of type ${type} does not read`);
|
|
112
|
+
return parsed.document;
|
|
113
|
+
}
|
|
84
114
|
function fitsBare(to) {
|
|
85
115
|
if (to === '' || to.startsWith('<'))
|
|
86
116
|
return false;
|
|
@@ -168,12 +198,12 @@ export function relativize(document, file, handle, definition, kept = new Set())
|
|
|
168
198
|
prior: value,
|
|
169
199
|
});
|
|
170
200
|
}
|
|
171
|
-
for (const href of new Set(
|
|
201
|
+
for (const href of new Set(linkDestinations(document).map(({ href }) => href))) {
|
|
172
202
|
const path = relativized(href);
|
|
173
203
|
if (path !== null)
|
|
174
204
|
changes.push({ kind: 'link', href, to: relative(file, path) });
|
|
175
205
|
}
|
|
176
|
-
return apply(document, changes);
|
|
206
|
+
return apply(document, changes, definition);
|
|
177
207
|
}
|
|
178
208
|
export function relativizeBody(body, file, handle) {
|
|
179
209
|
const relinkedBody = shellRelinked(body, file, handle);
|
|
@@ -201,6 +231,30 @@ export function relinkBody(body, file, moves) {
|
|
|
201
231
|
}
|
|
202
232
|
return { outcome: 'applied', body: rewritten + body.slice(cursor) };
|
|
203
233
|
}
|
|
234
|
+
export function unlinkBody(body, hrefs) {
|
|
235
|
+
const { uses, definitions } = read(body);
|
|
236
|
+
const links = new Map();
|
|
237
|
+
for (const use of uses) {
|
|
238
|
+
const { href, bracketed } = use.destination;
|
|
239
|
+
if (!hrefs.has(href) || links.has(href))
|
|
240
|
+
continue;
|
|
241
|
+
const label = body.slice(use.labelStart, use.labelEnd);
|
|
242
|
+
const image = body[use.start] === '!' ? '!' : '';
|
|
243
|
+
links.set(href, `${image}[${label}](${destinationText(href, bracketed) ?? href})`);
|
|
244
|
+
}
|
|
245
|
+
const cut = [...uses, ...definitions]
|
|
246
|
+
.filter((span) => hrefs.has(span.destination.href))
|
|
247
|
+
.sort((left, right) => left.start - right.start);
|
|
248
|
+
let kept = '';
|
|
249
|
+
let cursor = 0;
|
|
250
|
+
for (const span of cut) {
|
|
251
|
+
if (span.start < cursor)
|
|
252
|
+
continue;
|
|
253
|
+
kept += body.slice(cursor, span.start);
|
|
254
|
+
cursor = span.end;
|
|
255
|
+
}
|
|
256
|
+
return { body: kept + body.slice(cursor), links };
|
|
257
|
+
}
|
|
204
258
|
function linksIn(occurrences, file) {
|
|
205
259
|
const found = [];
|
|
206
260
|
const seen = new Set();
|
|
@@ -216,22 +270,22 @@ function linksIn(occurrences, file) {
|
|
|
216
270
|
return found;
|
|
217
271
|
}
|
|
218
272
|
export function links(document, file) {
|
|
219
|
-
return linksIn(
|
|
273
|
+
return linksIn(linkDestinations(document), file);
|
|
220
274
|
}
|
|
221
275
|
export function bodyLinks(body, file) {
|
|
222
276
|
return linksIn(hrefOccurrences(body), file);
|
|
223
277
|
}
|
|
224
278
|
function internalOf(document) {
|
|
225
|
-
const held =
|
|
279
|
+
const held = heldOf(document);
|
|
226
280
|
if (held !== undefined)
|
|
227
|
-
return held;
|
|
228
|
-
const
|
|
229
|
-
if (!
|
|
230
|
-
return
|
|
281
|
+
return held.internal;
|
|
282
|
+
const opening = opened(document.content, '');
|
|
283
|
+
if (!opening.ok)
|
|
284
|
+
return opening.refusal;
|
|
231
285
|
return {
|
|
232
|
-
root:
|
|
233
|
-
yamlEnd:
|
|
234
|
-
bodyStart:
|
|
286
|
+
root: opening.root,
|
|
287
|
+
yamlEnd: opening.yamlEnd,
|
|
288
|
+
bodyStart: opening.root === null ? 0 : bodyStartAfterYaml(document.content, opening.yamlEnd),
|
|
235
289
|
};
|
|
236
290
|
}
|
|
237
291
|
function partOf(change) {
|
|
@@ -241,7 +295,7 @@ function partOf(change) {
|
|
|
241
295
|
return `link ${change.href}`;
|
|
242
296
|
if (change.kind === 'reference')
|
|
243
297
|
return `reference ${change.pointer}`;
|
|
244
|
-
return
|
|
298
|
+
return change.kind;
|
|
245
299
|
}
|
|
246
300
|
function planner(document, internal) {
|
|
247
301
|
const content = document.content;
|
|
@@ -291,7 +345,7 @@ function planner(document, internal) {
|
|
|
291
345
|
const section = (name, value, prior, at) => {
|
|
292
346
|
if (spans === undefined) {
|
|
293
347
|
spans = new Map();
|
|
294
|
-
for (const span of
|
|
348
|
+
for (const span of sectionSpans(readingOf(document), content.length))
|
|
295
349
|
if (!spans.has(span.name))
|
|
296
350
|
spans.set(span.name, span);
|
|
297
351
|
}
|
|
@@ -305,7 +359,7 @@ function planner(document, internal) {
|
|
|
305
359
|
const link = (href, to, at) => {
|
|
306
360
|
if (occurrences === undefined) {
|
|
307
361
|
occurrences = new Map();
|
|
308
|
-
for (const occurrence of
|
|
362
|
+
for (const occurrence of linkDestinations(document)) {
|
|
309
363
|
const same = occurrences.get(occurrence.href);
|
|
310
364
|
if (same === undefined)
|
|
311
365
|
occurrences.set(occurrence.href, [occurrence]);
|
|
@@ -320,8 +374,8 @@ function planner(document, internal) {
|
|
|
320
374
|
if (destinationText(written, false) === null)
|
|
321
375
|
return refuse('', 'unwritable-link', written);
|
|
322
376
|
return found.map((occurrence) => ({
|
|
323
|
-
start:
|
|
324
|
-
end:
|
|
377
|
+
start: occurrence.start,
|
|
378
|
+
end: occurrence.end,
|
|
325
379
|
text: destinationText(written, occurrence.bracketed),
|
|
326
380
|
at,
|
|
327
381
|
}));
|
|
@@ -343,6 +397,11 @@ function planner(document, internal) {
|
|
|
343
397
|
return link(change.href, change.to, at);
|
|
344
398
|
if (change.kind === 'reference')
|
|
345
399
|
return reference(change.pointer, change.value, change.prior, at);
|
|
400
|
+
if (change.kind === 'text') {
|
|
401
|
+
if (document.text !== change.prior)
|
|
402
|
+
return 'stale';
|
|
403
|
+
return [{ start: internal.bodyStart, end: content.length, text: change.value, at }];
|
|
404
|
+
}
|
|
346
405
|
if (document.preamble !== change.prior)
|
|
347
406
|
return 'stale';
|
|
348
407
|
const start = internal.bodyStart;
|
|
@@ -361,19 +420,19 @@ function overlapping(edits, stale) {
|
|
|
361
420
|
}
|
|
362
421
|
}
|
|
363
422
|
}
|
|
364
|
-
function firstHeadings(
|
|
423
|
+
function firstHeadings(reading) {
|
|
365
424
|
const first = new Map();
|
|
366
|
-
for (const span of
|
|
425
|
+
for (const span of sectionSpans(reading, 0)) {
|
|
367
426
|
if (!first.has(span.name))
|
|
368
427
|
first.set(span.name, span.headingStart);
|
|
369
428
|
}
|
|
370
429
|
return first;
|
|
371
430
|
}
|
|
372
|
-
function hidden(before,
|
|
373
|
-
const placed = firstHeadings(after
|
|
431
|
+
function hidden(before, after, shifts) {
|
|
432
|
+
const placed = firstHeadings(after);
|
|
374
433
|
let shift = 0;
|
|
375
434
|
let index = 0;
|
|
376
|
-
for (const [name, at] of firstHeadings(before
|
|
435
|
+
for (const [name, at] of firstHeadings(before)) {
|
|
377
436
|
while (index < shifts.length && shifts[index][0] <= at) {
|
|
378
437
|
shift += shifts[index][1];
|
|
379
438
|
index += 1;
|
|
@@ -409,7 +468,48 @@ function spliced(source, edits) {
|
|
|
409
468
|
pieces.push(source.slice(cursor));
|
|
410
469
|
return { content: pieces.join(''), shifts };
|
|
411
470
|
}
|
|
412
|
-
|
|
471
|
+
function checkedReading(source, replaced) {
|
|
472
|
+
const checked = spliced(source, replaced);
|
|
473
|
+
return {
|
|
474
|
+
reading: bodyReading(checked.content, bodyStartOf(checked.content)),
|
|
475
|
+
shifts: checked.shifts,
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
function opening(change) {
|
|
479
|
+
return change.kind === 'section' && change.prior === undefined;
|
|
480
|
+
}
|
|
481
|
+
function withSections(document, changes, definition) {
|
|
482
|
+
const opened = [];
|
|
483
|
+
const held = [];
|
|
484
|
+
for (const [at, change] of changes.entries()) {
|
|
485
|
+
if (!opening(change))
|
|
486
|
+
continue;
|
|
487
|
+
if (Object.hasOwn(document.sections, change.name) || opened.includes(change.name))
|
|
488
|
+
held.push(at);
|
|
489
|
+
else
|
|
490
|
+
opened.push(change.name);
|
|
491
|
+
}
|
|
492
|
+
const source = document.content;
|
|
493
|
+
const ending = lineEnding(source);
|
|
494
|
+
const lead = source === '' || source.endsWith('\n') ? '' : ending;
|
|
495
|
+
const skeleton = parse(`${source}${lead}${opened.map((name) => `## ${name}${ending}`).join('')}`, '', definition);
|
|
496
|
+
if (!skeleton.ok)
|
|
497
|
+
return { outcome: 'refuse', refusal: skeleton.refusal };
|
|
498
|
+
const kept = changes.flatMap((change, at) => held.includes(at) ? [] : [[opening(change) ? { ...change, prior: '' } : change, at]]);
|
|
499
|
+
const applied = apply(skeleton.document, kept.map(([change]) => change), definition);
|
|
500
|
+
if (applied.outcome === 'refuse')
|
|
501
|
+
return applied;
|
|
502
|
+
const stale = [
|
|
503
|
+
...held,
|
|
504
|
+
...(applied.outcome === 'stale' ? applied.stale.map((at) => kept[at][1]) : []),
|
|
505
|
+
];
|
|
506
|
+
if (stale.length > 0)
|
|
507
|
+
return { outcome: 'stale', document, stale: stale.sort((left, right) => left - right) };
|
|
508
|
+
return applied;
|
|
509
|
+
}
|
|
510
|
+
export function apply(document, changes, definition) {
|
|
511
|
+
if (changes.some(opening))
|
|
512
|
+
return withSections(document, changes, definition);
|
|
413
513
|
const internal = internalOf(document);
|
|
414
514
|
if (!('bodyStart' in internal))
|
|
415
515
|
return { outcome: 'refuse', refusal: internal };
|
|
@@ -439,27 +539,29 @@ export function apply(document, changes) {
|
|
|
439
539
|
if (stale.size > 0)
|
|
440
540
|
return { outcome: 'stale', document, stale: [...stale].sort((left, right) => left - right) };
|
|
441
541
|
const source = document.content;
|
|
442
|
-
const { content } = spliced(source, edits);
|
|
542
|
+
const { content, shifts } = spliced(source, edits);
|
|
443
543
|
if (content === source)
|
|
444
544
|
return { outcome: 'applied', document };
|
|
545
|
+
const parsed = parse(content, '', definition);
|
|
546
|
+
if (!parsed.ok)
|
|
547
|
+
return { outcome: 'refuse', refusal: parsed.refusal };
|
|
445
548
|
const replaced = edits.filter((edit) => {
|
|
446
549
|
const kind = changes[edit.at].kind;
|
|
447
550
|
return kind === 'section' || kind === 'preamble';
|
|
448
551
|
});
|
|
449
552
|
if (replaced.length > 0) {
|
|
450
|
-
const checked =
|
|
451
|
-
|
|
553
|
+
const checked = replaced.length === edits.length
|
|
554
|
+
? { reading: readingOf(parsed.document), shifts }
|
|
555
|
+
: checkedReading(source, replaced);
|
|
556
|
+
const name = hidden(readingOf(document), checked.reading, checked.shifts);
|
|
452
557
|
if (name !== undefined)
|
|
453
558
|
return { outcome: 'refuse', refusal: refuse('', 'overlapping-section', name) };
|
|
454
559
|
}
|
|
455
|
-
const parsed = parse(content);
|
|
456
|
-
if (!parsed.ok)
|
|
457
|
-
return { outcome: 'refuse', refusal: parsed.refusal };
|
|
458
560
|
return { outcome: 'applied', document: parsed.document };
|
|
459
561
|
}
|
|
460
562
|
export function serialize(document) {
|
|
461
563
|
return document.content;
|
|
462
564
|
}
|
|
463
565
|
export function frontmatter(document) {
|
|
464
|
-
return
|
|
566
|
+
return heldOf(document)?.internal.root ?? null;
|
|
465
567
|
}
|
package/dist/event.d.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { type Definition } from './definition.js';
|
|
2
|
+
import { type Kind } from './kinds.js';
|
|
3
|
+
import type { Candidate } from './query/match.js';
|
|
4
|
+
import type { Document, VerifiedModel } from './types.js';
|
|
5
|
+
type Scalar = string | number | boolean;
|
|
6
|
+
export type Fact = {
|
|
7
|
+
readonly kind: Kind;
|
|
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":"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"}
|
package/dist/event.js
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { ARRAY_INDEX, escapePointer, fieldFacts, itemFacts, memberFacts, pointerFacts, pointerSegments, } from './definition.js';
|
|
2
|
+
import { fitsKind, 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 referenceFact(value, place) {
|
|
11
|
+
if (typeof value !== 'string')
|
|
12
|
+
return UNREADABLE;
|
|
13
|
+
const reached = target(place.path, value) ?? roomPath(value);
|
|
14
|
+
const resolved = reached === null ? undefined : place.resolve(reached);
|
|
15
|
+
if (resolved === undefined)
|
|
16
|
+
return UNREADABLE;
|
|
17
|
+
return resolved.member === undefined
|
|
18
|
+
? { kind: 'reference', value: resolved.id }
|
|
19
|
+
: { kind: 'reference', value: resolved.id, member: resolved.member };
|
|
20
|
+
}
|
|
21
|
+
function factOf(value, declared, place) {
|
|
22
|
+
if (value === null)
|
|
23
|
+
return NULL;
|
|
24
|
+
if (Array.isArray(value)) {
|
|
25
|
+
if (declared?.type !== undefined && declared.type !== 'array')
|
|
26
|
+
return UNREADABLE;
|
|
27
|
+
return {
|
|
28
|
+
kind: 'list',
|
|
29
|
+
items: value.map((item, index) => factOf(item, declared === undefined ? undefined : itemFacts(declared, index), place)),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (plain(value)) {
|
|
33
|
+
if (declared?.type !== undefined && declared.type !== 'object')
|
|
34
|
+
return UNREADABLE;
|
|
35
|
+
const fields = Object.create(null);
|
|
36
|
+
for (const [key, held] of Object.entries(value))
|
|
37
|
+
fields[key] = factOf(held, declared === undefined ? undefined : memberFacts(declared, key), place);
|
|
38
|
+
return { kind: 'object', fields };
|
|
39
|
+
}
|
|
40
|
+
if (declared?.type === 'array' || declared?.type === 'object')
|
|
41
|
+
return UNREADABLE;
|
|
42
|
+
const kind = declared === undefined || declared.prose ? null : kindOf(declared);
|
|
43
|
+
if (kind === null)
|
|
44
|
+
return PROSE;
|
|
45
|
+
if (kind === 'reference')
|
|
46
|
+
return referenceFact(value, place);
|
|
47
|
+
return fitsKind(kind, declared, value) ? { kind, value: value } : UNREADABLE;
|
|
48
|
+
}
|
|
49
|
+
export function facts(document, at, model, resolve) {
|
|
50
|
+
const type = document.type ?? null;
|
|
51
|
+
const definition = type !== null && Object.hasOwn(model.types, type) ? model.types[type] : null;
|
|
52
|
+
const fields = Object.create(null);
|
|
53
|
+
const place = { path: at.path, resolve };
|
|
54
|
+
for (const [name, value] of Object.entries(document.fields)) {
|
|
55
|
+
const declared = definition === null ? undefined : fieldFacts(definition, name);
|
|
56
|
+
if (declared !== undefined)
|
|
57
|
+
fields[name] = factOf(value, declared, place);
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
id: at.id,
|
|
61
|
+
path: `/${at.path}`,
|
|
62
|
+
type,
|
|
63
|
+
fields,
|
|
64
|
+
sections: Object.keys(document.sections).sort(),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function rootOf(facts, prefix) {
|
|
68
|
+
return { pointer: prefix, fact: { kind: 'object', fields: facts.fields } };
|
|
69
|
+
}
|
|
70
|
+
export function prosePaths(facts) {
|
|
71
|
+
const paths = facts.sections.map((heading) => `/sections/${heading}`);
|
|
72
|
+
const visit = ({ pointer, fact }) => {
|
|
73
|
+
if (fact.kind === 'prose')
|
|
74
|
+
paths.push(pointer);
|
|
75
|
+
if (fact.kind === 'list')
|
|
76
|
+
fact.items.forEach((item, index) => visit({ pointer: `${pointer}/${index}`, fact: item }));
|
|
77
|
+
if (fact.kind === 'object')
|
|
78
|
+
for (const [key, held] of Object.entries(fact.fields))
|
|
79
|
+
visit({ pointer: `${pointer}/${escapePointer(key)}`, fact: held });
|
|
80
|
+
};
|
|
81
|
+
visit(rootOf(facts, '/fields'));
|
|
82
|
+
return paths.sort();
|
|
83
|
+
}
|
|
84
|
+
function walked(start, pointer) {
|
|
85
|
+
let reached = [start];
|
|
86
|
+
for (const token of pointerSegments(pointer)) {
|
|
87
|
+
const step = ({ pointer: at, fact }) => {
|
|
88
|
+
if (fact.kind === 'object')
|
|
89
|
+
return Object.hasOwn(fact.fields, token)
|
|
90
|
+
? [{ pointer: `${at}/${escapePointer(token)}`, fact: fact.fields[token] }]
|
|
91
|
+
: [];
|
|
92
|
+
if (fact.kind !== 'list')
|
|
93
|
+
return [];
|
|
94
|
+
if (ARRAY_INDEX.test(token)) {
|
|
95
|
+
const item = fact.items[Number(token)];
|
|
96
|
+
return item === undefined ? [] : [{ pointer: `${at}/${token}`, fact: item }];
|
|
97
|
+
}
|
|
98
|
+
return fact.items
|
|
99
|
+
.map((item, index) => ({ pointer: `${at}/${index}`, fact: item }))
|
|
100
|
+
.flatMap(step);
|
|
101
|
+
};
|
|
102
|
+
reached = reached.flatMap(step);
|
|
103
|
+
}
|
|
104
|
+
return reached;
|
|
105
|
+
}
|
|
106
|
+
function expand(fact) {
|
|
107
|
+
return fact.kind === 'list' ? fact.items.flatMap(expand) : [fact];
|
|
108
|
+
}
|
|
109
|
+
export function members(document, path) {
|
|
110
|
+
if (!path.startsWith('/members/'))
|
|
111
|
+
return [];
|
|
112
|
+
return [
|
|
113
|
+
...new Set(walked(rootOf(document, '/members'), path.slice('/members'.length))
|
|
114
|
+
.flatMap(({ fact }) => expand(fact))
|
|
115
|
+
.flatMap((fact) => fact.kind === 'reference' && fact.member !== undefined ? [fact.member] : [])),
|
|
116
|
+
].sort();
|
|
117
|
+
}
|
|
118
|
+
export function pointed(value, pointer) {
|
|
119
|
+
let reached = value;
|
|
120
|
+
for (const token of pointerSegments(pointer)) {
|
|
121
|
+
if (Array.isArray(reached)) {
|
|
122
|
+
if (!ARRAY_INDEX.test(token))
|
|
123
|
+
return undefined;
|
|
124
|
+
reached = reached[Number(token)];
|
|
125
|
+
}
|
|
126
|
+
else if (plain(reached) && Object.hasOwn(reached, token)) {
|
|
127
|
+
reached = reached[token];
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return reached;
|
|
134
|
+
}
|
|
135
|
+
export function valuedChange(definition, pointer) {
|
|
136
|
+
const reached = pointerFacts(definition, pointer);
|
|
137
|
+
return (reached !== undefined &&
|
|
138
|
+
!reached.through &&
|
|
139
|
+
reached.facts.type !== 'array' &&
|
|
140
|
+
!reached.facts.prose &&
|
|
141
|
+
kindOf(reached.facts) !== null);
|
|
142
|
+
}
|
|
143
|
+
export function datePointers(definition) {
|
|
144
|
+
return Object.entries(definition.fields)
|
|
145
|
+
.filter(([, held]) => kindOf(held) === 'date')
|
|
146
|
+
.map(([name]) => `/${escapePointer(name)}`)
|
|
147
|
+
.sort();
|
|
148
|
+
}
|
|
149
|
+
export function memberPointers(definition, memberType) {
|
|
150
|
+
return Object.entries(definition.fields)
|
|
151
|
+
.filter(([, held]) => (held.type === 'array' ? held.items : held)?.reference?.includes(memberType) === true)
|
|
152
|
+
.map(([name]) => `/members/${escapePointer(name)}`);
|
|
153
|
+
}
|
|
154
|
+
function plainOf(fact) {
|
|
155
|
+
if (fact.kind === 'list')
|
|
156
|
+
return fact.items.map(plainOf);
|
|
157
|
+
if (fact.kind === 'object')
|
|
158
|
+
return Object.fromEntries(Object.entries(fact.fields).flatMap(([key, held]) => {
|
|
159
|
+
const value = plainOf(held);
|
|
160
|
+
return value === undefined ? [] : [[key, value]];
|
|
161
|
+
}));
|
|
162
|
+
if (fact.kind === 'prose')
|
|
163
|
+
return true;
|
|
164
|
+
if (fact.kind === 'null')
|
|
165
|
+
return null;
|
|
166
|
+
if (fact.kind === 'unreadable')
|
|
167
|
+
return undefined;
|
|
168
|
+
return fact.value;
|
|
169
|
+
}
|
|
170
|
+
function shapeOf(fact) {
|
|
171
|
+
if (fact.kind === 'list')
|
|
172
|
+
return { type: 'array', prose: false, prefix: fact.items.map(shapeOf) };
|
|
173
|
+
if (fact.kind === 'object')
|
|
174
|
+
return {
|
|
175
|
+
type: 'object',
|
|
176
|
+
prose: false,
|
|
177
|
+
properties: Object.fromEntries(Object.entries(fact.fields).map(([key, held]) => [key, shapeOf(held)])),
|
|
178
|
+
};
|
|
179
|
+
if (fact.kind === 'date-time')
|
|
180
|
+
return { type: 'string', format: 'date-time', prose: false };
|
|
181
|
+
return { prose: false };
|
|
182
|
+
}
|
|
183
|
+
export function candidateOf(document) {
|
|
184
|
+
const fields = {};
|
|
185
|
+
const properties = {};
|
|
186
|
+
for (const [name, fact] of Object.entries(document.fields)) {
|
|
187
|
+
const value = plainOf(fact);
|
|
188
|
+
if (value === undefined)
|
|
189
|
+
continue;
|
|
190
|
+
fields[name] = value;
|
|
191
|
+
properties[name] = shapeOf(fact);
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
path: document.path.slice(1),
|
|
195
|
+
type: document.type,
|
|
196
|
+
fileId: document.id,
|
|
197
|
+
fields,
|
|
198
|
+
definition: { frontmatter: { type: 'object', prose: false, properties } },
|
|
199
|
+
text: null,
|
|
200
|
+
};
|
|
201
|
+
}
|