@agentshouse/mdmodel 0.1.0-c37720432b75a → 0.1.0-c3c0e116a89f8

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.
Files changed (143) hide show
  1. package/API.md +122 -100
  2. package/README.md +7 -5
  3. package/dist/definition.d.ts +1 -1
  4. package/dist/definition.d.ts.map +1 -1
  5. package/dist/definition.js +78 -24
  6. package/dist/divide.d.ts.map +1 -1
  7. package/dist/divide.js +5 -4
  8. package/dist/document.d.ts +5 -3
  9. package/dist/document.d.ts.map +1 -1
  10. package/dist/document.js +103 -57
  11. package/dist/event.d.ts +78 -0
  12. package/dist/event.d.ts.map +1 -0
  13. package/dist/event.js +201 -0
  14. package/dist/facts.d.ts +15 -0
  15. package/dist/facts.d.ts.map +1 -0
  16. package/dist/facts.js +41 -0
  17. package/dist/index.d.ts +21 -9
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +13 -6
  20. package/dist/kinds.d.ts +18 -0
  21. package/dist/kinds.d.ts.map +1 -0
  22. package/dist/kinds.js +150 -0
  23. package/dist/markdown.d.ts +51 -0
  24. package/dist/markdown.d.ts.map +1 -0
  25. package/dist/markdown.js +271 -0
  26. package/dist/paths.d.ts.map +1 -1
  27. package/dist/paths.js +9 -2
  28. package/dist/pattern.d.ts +10 -0
  29. package/dist/pattern.d.ts.map +1 -0
  30. package/dist/pattern.js +356 -0
  31. package/dist/project.d.ts.map +1 -1
  32. package/dist/project.js +16 -21
  33. package/dist/query/language.d.ts +254 -0
  34. package/dist/query/language.d.ts.map +1 -0
  35. package/dist/query/language.js +620 -0
  36. package/dist/query/match.d.ts +14 -0
  37. package/dist/query/match.d.ts.map +1 -0
  38. package/dist/query/match.js +150 -0
  39. package/dist/query/rows.d.ts +32 -0
  40. package/dist/query/rows.d.ts.map +1 -0
  41. package/dist/query/rows.js +161 -0
  42. package/dist/query/sql.d.ts +39 -0
  43. package/dist/query/sql.d.ts.map +1 -0
  44. package/dist/query/sql.js +285 -0
  45. package/dist/reserved.d.ts +6 -0
  46. package/dist/reserved.d.ts.map +1 -0
  47. package/dist/reserved.js +21 -0
  48. package/dist/schema.d.ts +1 -0
  49. package/dist/schema.d.ts.map +1 -1
  50. package/dist/schema.js +55 -4
  51. package/dist/types.d.ts +12 -0
  52. package/dist/types.d.ts.map +1 -1
  53. package/dist/validate.d.ts +7 -5
  54. package/dist/validate.d.ts.map +1 -1
  55. package/dist/validate.js +244 -113
  56. package/dist/yaml.d.ts.map +1 -1
  57. package/dist/yaml.js +40 -14
  58. package/fixtures/README.md +24 -3
  59. package/fixtures/cases/conditional-section/context.json +1 -0
  60. package/fixtures/cases/conditional-section/expected.json +6 -0
  61. package/fixtures/cases/conditional-section/library/questions/hosting.md +9 -0
  62. package/fixtures/cases/conditional-section/library/questions/payments.md +12 -0
  63. package/fixtures/cases/conditional-section/schema.yaml +14 -0
  64. package/fixtures/cases/lazy-list-item/context.json +1 -0
  65. package/fixtures/cases/lazy-list-item/expected.json +3 -0
  66. package/fixtures/cases/lazy-list-item/library/item.md +8 -0
  67. package/fixtures/cases/lazy-list-item/schema.yaml +3 -0
  68. package/fixtures/cases/link-into-a-reserved-name/context.json +1 -0
  69. package/fixtures/cases/link-into-a-reserved-name/expected.json +5 -0
  70. package/fixtures/cases/link-into-a-reserved-name/library/notes/a.md +6 -0
  71. package/fixtures/cases/link-into-a-reserved-name/schema.yaml +3 -0
  72. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/context.json +1 -0
  73. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/expected.json +5 -0
  74. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/library/notes/b.md +5 -0
  75. package/fixtures/cases/member-on-an-untyped-document/context.json +1 -0
  76. package/fixtures/cases/member-on-an-untyped-document/expected.json +6 -0
  77. package/fixtures/cases/member-on-an-untyped-document/library/note.md +4 -0
  78. package/fixtures/cases/member-on-another-type/context.json +1 -0
  79. package/fixtures/cases/member-on-another-type/expected.json +6 -0
  80. package/fixtures/cases/member-on-another-type/library/note.md +4 -0
  81. package/fixtures/cases/member-on-another-type/schema.yaml +7 -0
  82. package/fixtures/cases/reference-into-a-reserved-name/context.json +1 -0
  83. package/fixtures/cases/reference-into-a-reserved-name/expected.json +6 -0
  84. package/fixtures/cases/reference-into-a-reserved-name/library/notes/a.md +4 -0
  85. package/fixtures/cases/reference-into-a-reserved-name/library/settings/brief.md +1 -0
  86. package/fixtures/cases/reference-into-a-reserved-name/schema.yaml +3 -0
  87. package/fixtures/cases/room-md-declares-a-type/context.json +1 -0
  88. package/fixtures/cases/room-md-declares-a-type/expected.json +6 -0
  89. package/fixtures/cases/room-md-declares-a-type/library/ROOM.md +5 -0
  90. package/fixtures/cases/thematic-break-in-a-list-section/context.json +1 -0
  91. package/fixtures/cases/thematic-break-in-a-list-section/expected.json +6 -0
  92. package/fixtures/cases/thematic-break-in-a-list-section/library/item.md +7 -0
  93. package/fixtures/cases/thematic-break-in-a-list-section/schema.yaml +3 -0
  94. package/fixtures/cases/typed-path-names-another-type/context.json +11 -0
  95. package/fixtures/cases/typed-path-names-another-type/expected.json +6 -0
  96. package/fixtures/cases/typed-path-names-another-type/library/settings/prefs/one.md +3 -0
  97. package/fixtures/cases/typed-path-names-another-type/schema.yaml +6 -0
  98. package/fixtures/cases/typed-path-names-its-type/context.json +11 -0
  99. package/fixtures/cases/typed-path-names-its-type/expected.json +3 -0
  100. package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/deeper/two.md +1 -0
  101. package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/one.md +4 -0
  102. package/fixtures/cases/typed-path-names-its-type/schema.yaml +6 -0
  103. package/fixtures/cases/typed-path-names-no-type/context.json +11 -0
  104. package/fixtures/cases/typed-path-names-no-type/expected.json +6 -0
  105. package/fixtures/cases/typed-path-names-no-type/library/settings/prefs/one.md +1 -0
  106. package/fixtures/cases/typed-path-names-no-type/schema.yaml +6 -0
  107. package/fixtures/cases/unnamed-typed-path-checks-its-fields/context.json +11 -0
  108. package/fixtures/cases/unnamed-typed-path-checks-its-fields/expected.json +6 -0
  109. package/fixtures/cases/unnamed-typed-path-checks-its-fields/library/rooms/team/tasks/plan.md +4 -0
  110. package/fixtures/cases/unnamed-typed-path-checks-its-fields/schema.yaml +5 -0
  111. package/fixtures/cases/unnamed-typed-path-names-a-type/context.json +11 -0
  112. package/fixtures/cases/unnamed-typed-path-names-a-type/expected.json +6 -0
  113. package/fixtures/cases/unnamed-typed-path-names-a-type/library/rooms/team/tasks/plan.md +6 -0
  114. package/fixtures/cases/unnamed-typed-path-names-a-type/schema.yaml +5 -0
  115. package/fixtures/cases/unnamed-typed-path-reads-its-type/context.json +11 -0
  116. package/fixtures/cases/unnamed-typed-path-reads-its-type/expected.json +3 -0
  117. package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/plan.md +1 -0
  118. package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/tasks/plan.md +5 -0
  119. package/fixtures/cases/unnamed-typed-path-reads-its-type/schema.yaml +5 -0
  120. package/fixtures/cases/unnamed-typed-path-without-a-body/context.json +11 -0
  121. package/fixtures/cases/unnamed-typed-path-without-a-body/expected.json +5 -0
  122. package/fixtures/cases/unnamed-typed-path-without-a-body/library/rooms/team/tasks/plan.md +5 -0
  123. package/fixtures/cases/unnamed-typed-path-without-a-body/schema.yaml +5 -0
  124. package/fixtures/division.json +47 -2
  125. package/fixtures/preamble.json +24 -3
  126. package/fixtures/projection.json +105 -2
  127. package/fixtures/queries.json +964 -0
  128. package/package.json +15 -1
  129. package/dist/blocks.d.ts +0 -11
  130. package/dist/blocks.d.ts.map +0 -1
  131. package/dist/blocks.js +0 -69
  132. package/dist/condition.d.ts +0 -161
  133. package/dist/condition.d.ts.map +0 -1
  134. package/dist/condition.js +0 -615
  135. package/dist/links.d.ts +0 -29
  136. package/dist/links.d.ts.map +0 -1
  137. package/dist/links.js +0 -800
  138. package/dist/profile.d.ts +0 -6
  139. package/dist/profile.d.ts.map +0 -1
  140. package/dist/profile.js +0 -5
  141. package/dist/sections.d.ts +0 -11
  142. package/dist/sections.d.ts.map +0 -1
  143. package/dist/sections.js +0 -57
package/dist/divide.js CHANGED
@@ -1,4 +1,5 @@
1
- import { blockRanges, headingLines } from './blocks.js';
1
+ import { readingOf } from './document.js';
2
+ import { blockRanges, headingsIn } from './markdown.js';
2
3
  import { firstEditFrom, projectionInput, rangeText, } from './project.js';
3
4
  import { refusal } from './refuse.js';
4
5
  const SMALLEST_BOUND = 2;
@@ -12,8 +13,7 @@ function node(draft) {
12
13
  };
13
14
  }
14
15
  function forest(state, range) {
15
- const content = state.content;
16
- const headings = headingLines(content, range.start, range.end);
16
+ const headings = headingsIn(state.reading, range.start, range.end);
17
17
  const roots = [];
18
18
  const open = [];
19
19
  const first = headings[0]?.start ?? range.end;
@@ -282,7 +282,7 @@ function ownBlocks(subject, end, blocks) {
282
282
  }
283
283
  function divideNode(state, subject) {
284
284
  const end = subject.children[0]?.start ?? subject.end;
285
- const blocks = blockRanges(state.content, subject.bodyStart, end);
285
+ const blocks = blockRanges(state.reading, subject.bodyStart, end);
286
286
  const children = { heading: '', emitted: 0 };
287
287
  if (blocks.length === 0 && subject.children.length > 0) {
288
288
  const [first, ...rest] = subject.children;
@@ -311,6 +311,7 @@ export function divide(document, file, context) {
311
311
  return { ok: false, refusal: result.refusal };
312
312
  const state = {
313
313
  content: document.content,
314
+ reading: readingOf(document),
314
315
  edits: result.input.edits,
315
316
  bound: context.bound,
316
317
  spans: [],
@@ -1,13 +1,15 @@
1
1
  import { type Definition } from './definition.js';
2
- import { type LinkInventory } from './links.js';
3
- import type { ApplyResult, BodyResult, FrontmatterResult, Change, Document, Link, ParseResult } from './types.js';
2
+ import { type HrefOccurrence, type Reading } from './markdown.js';
3
+ import type { ApplyResult, BodyResult, FrontmatterResult, Change, Document, Link, ParseResult, UnlinkedBody } from './types.js';
4
4
  import { type YamlMap } from './yaml.js';
5
- export declare function inventoryOf(document: Document): LinkInventory;
5
+ export declare function readingOf(document: Document): Reading;
6
+ export declare function linkDestinations(document: Document): readonly HrefOccurrence[];
6
7
  export declare function parseFrontmatter(content: string, file?: string): FrontmatterResult;
7
8
  export declare function parse(content: string, file?: string): ParseResult;
8
9
  export declare function relativize(document: Document, file: string, handle: string, definition?: Definition, kept?: ReadonlySet<string>): ApplyResult;
9
10
  export declare function relativizeBody(body: string, file: string, handle: string): BodyResult;
10
11
  export declare function relinkBody(body: string, file: string, moves: ReadonlyMap<string, string>): BodyResult;
12
+ export declare function unlinkBody(body: string, hrefs: ReadonlySet<string>): UnlinkedBody;
11
13
  export declare function links(document: Document, file: string): readonly Link[];
12
14
  export declare function bodyLinks(body: string, file: string): readonly Link[];
13
15
  export declare function apply(document: Document, changes: readonly Change[]): ApplyResult;
@@ -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;AAgBnB,wBAAgB,WAAW,CAAC,QAAQ,EAAE,QAAQ,GAAG,aAAa,CAM7D;AAgED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,SAAK,GAAG,iBAAiB,CAW9E;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,SAAK,GAAG,WAAW,CAgB7D;AAmED,wBAAgB,UAAU,CACxB,QAAQ,EAAE,QAAQ,EAClB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,MAAM,EACd,UAAU,CAAC,EAAE,UAAU,EACv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ED
@@ -1,40 +1,53 @@
1
1
  import { ARRAY_INDEX, pointerSegments } from './definition.js';
2
- import { destinations, hrefOccurrences, linkInventory, } from './links.js';
2
+ import { destinations, hrefOccurrences, read, sectionSpans, sectionsRecord, } from './markdown.js';
3
3
  import { destinationPath, relative, retarget, roomPath, roomTarget, SHELL_ROOMS, shellTarget, target, } from './paths.js';
4
4
  import { references } from './references.js';
5
- import { headingSpans, sectionsRecord } from './sections.js';
6
5
  import { refusal } from './refuse.js';
7
6
  import { bodyStartAfterYaml, bodyStartOf, equalValue, extractFrontmatter, frontmatterValue, jsValue, lineEnding, mapEntry, parseYamlMapping, serializeJs, serializeKey, YamlError, } from './yaml.js';
8
7
  const HELD = Symbol('held');
9
8
  function heldOf(document) {
10
9
  return document[HELD];
11
10
  }
12
- export function inventoryOf(document) {
11
+ function bodyReading(content, bodyStart) {
12
+ return read(content.slice(bodyStart), bodyStart);
13
+ }
14
+ function heldReading(content, held) {
15
+ held.reading ??= bodyReading(content, held.internal.bodyStart);
16
+ return held.reading;
17
+ }
18
+ export function readingOf(document) {
13
19
  const held = heldOf(document);
14
- if (held?.inventory !== undefined)
15
- return held.inventory;
16
- const inventory = linkInventory(document.content.slice(bodyStartOf(document.content)));
17
- if (held !== undefined)
18
- held.inventory = inventory;
19
- return inventory;
20
+ return held === undefined
21
+ ? bodyReading(document.content, bodyStartOf(document.content))
22
+ : heldReading(document.content, held);
23
+ }
24
+ export function linkDestinations(document) {
25
+ const bodyStart = heldOf(document)?.internal.bodyStart ?? bodyStartOf(document.content);
26
+ return document.content.includes('[', bodyStart) ? destinations(readingOf(document)) : [];
27
+ }
28
+ function heldParts(content, held) {
29
+ if (held.parts !== undefined)
30
+ return held.parts;
31
+ const spans = sectionSpans(heldReading(content, held), content.length);
32
+ held.parts = {
33
+ preamble: content.slice(held.internal.bodyStart, spans[0]?.headingStart),
34
+ sections: Object.freeze(sectionsRecord(spans, content)),
35
+ };
36
+ return held.parts;
20
37
  }
21
38
  function refuse(file, rule, location) {
22
39
  return location === undefined ? refusal(file, rule) : refusal(file, rule, { location });
23
40
  }
24
- function freezeDocument(content, type, fields, preamble, sections, internal) {
25
- const held = { internal, inventory: undefined };
26
- return Object.freeze(Object.defineProperty({
27
- content,
28
- type,
29
- fields: Object.freeze(fields),
30
- preamble,
31
- sections: Object.freeze(sections),
32
- }, HELD, { value: held }));
41
+ function freezeDocument(content, type, fields, internal) {
42
+ const held = { internal };
43
+ return Object.freeze(Object.defineProperties({ content, type, fields: Object.freeze(fields) }, {
44
+ preamble: { enumerable: true, get: () => heldParts(content, held).preamble },
45
+ sections: { enumerable: true, get: () => heldParts(content, held).sections },
46
+ [HELD]: { value: held },
47
+ }));
33
48
  }
34
49
  function declaredType(fields) {
35
50
  const value = fields.type;
36
- if (value === undefined || value === null)
37
- return undefined;
38
51
  if (typeof value !== 'string' || value === '')
39
52
  return undefined;
40
53
  return value;
@@ -48,9 +61,6 @@ function opened(content, file) {
48
61
  try {
49
62
  const root = parseYamlMapping(content, fence.yamlStart, fence.yamlEnd);
50
63
  const fields = jsValue(root);
51
- if (fields === null || typeof fields !== 'object' || Array.isArray(fields)) {
52
- return { ok: false, refusal: refuse(file, 'frontmatter-fence') };
53
- }
54
64
  return { ok: true, root, fields: fields, yamlEnd: fence.yamlEnd };
55
65
  }
56
66
  catch (error) {
@@ -60,27 +70,30 @@ function opened(content, file) {
60
70
  }
61
71
  }
62
72
  export function parseFrontmatter(content, file = '') {
63
- const read = opened(content, file);
64
- if (!read.ok)
65
- return read;
73
+ const opening = opened(content, file);
74
+ if (!opening.ok)
75
+ return opening;
66
76
  return {
67
77
  ok: true,
68
78
  frontmatter: Object.freeze({
69
79
  content,
70
- type: declaredType(read.fields),
71
- fields: Object.freeze(read.fields),
80
+ type: declaredType(opening.fields),
81
+ fields: Object.freeze(opening.fields),
72
82
  }),
73
83
  };
74
84
  }
75
85
  export function parse(content, file = '') {
76
- const read = opened(content, file);
77
- if (!read.ok)
78
- return read;
79
- const bodyStart = read.root === null ? 0 : bodyStartAfterYaml(content, read.yamlEnd);
80
- const spans = headingSpans(content, bodyStart);
86
+ const opening = opened(content, file);
87
+ if (!opening.ok)
88
+ return opening;
89
+ const bodyStart = opening.root === null ? 0 : bodyStartAfterYaml(content, opening.yamlEnd);
81
90
  return {
82
91
  ok: true,
83
- document: freezeDocument(content, declaredType(read.fields), read.fields, content.slice(bodyStart, spans[0]?.headingStart), sectionsRecord(spans, content), { root: read.root, yamlEnd: read.yamlEnd, bodyStart }),
92
+ document: freezeDocument(content, declaredType(opening.fields), opening.fields, {
93
+ root: opening.root,
94
+ yamlEnd: opening.yamlEnd,
95
+ bodyStart,
96
+ }),
84
97
  };
85
98
  }
86
99
  function fitsBare(to) {
@@ -170,7 +183,7 @@ export function relativize(document, file, handle, definition, kept = new Set())
170
183
  prior: value,
171
184
  });
172
185
  }
173
- for (const href of new Set(destinations(inventoryOf(document)).map(({ href }) => href))) {
186
+ for (const href of new Set(linkDestinations(document).map(({ href }) => href))) {
174
187
  const path = relativized(href);
175
188
  if (path !== null)
176
189
  changes.push({ kind: 'link', href, to: relative(file, path) });
@@ -203,6 +216,30 @@ export function relinkBody(body, file, moves) {
203
216
  }
204
217
  return { outcome: 'applied', body: rewritten + body.slice(cursor) };
205
218
  }
219
+ export function unlinkBody(body, hrefs) {
220
+ const { uses, definitions } = read(body);
221
+ const links = new Map();
222
+ for (const use of uses) {
223
+ const { href, bracketed } = use.destination;
224
+ if (!hrefs.has(href) || links.has(href))
225
+ continue;
226
+ const label = body.slice(use.labelStart, use.labelEnd);
227
+ const image = body[use.start] === '!' ? '!' : '';
228
+ links.set(href, `${image}[${label}](${destinationText(href, bracketed) ?? href})`);
229
+ }
230
+ const cut = [...uses, ...definitions]
231
+ .filter((span) => hrefs.has(span.destination.href))
232
+ .sort((left, right) => left.start - right.start);
233
+ let kept = '';
234
+ let cursor = 0;
235
+ for (const span of cut) {
236
+ if (span.start < cursor)
237
+ continue;
238
+ kept += body.slice(cursor, span.start);
239
+ cursor = span.end;
240
+ }
241
+ return { body: kept + body.slice(cursor), links };
242
+ }
206
243
  function linksIn(occurrences, file) {
207
244
  const found = [];
208
245
  const seen = new Set();
@@ -218,7 +255,7 @@ function linksIn(occurrences, file) {
218
255
  return found;
219
256
  }
220
257
  export function links(document, file) {
221
- return linksIn(destinations(inventoryOf(document)), file);
258
+ return linksIn(linkDestinations(document), file);
222
259
  }
223
260
  export function bodyLinks(body, file) {
224
261
  return linksIn(hrefOccurrences(body), file);
@@ -227,13 +264,13 @@ function internalOf(document) {
227
264
  const held = heldOf(document);
228
265
  if (held !== undefined)
229
266
  return held.internal;
230
- const read = opened(document.content, '');
231
- if (!read.ok)
232
- return read.refusal;
267
+ const opening = opened(document.content, '');
268
+ if (!opening.ok)
269
+ return opening.refusal;
233
270
  return {
234
- root: read.root,
235
- yamlEnd: read.yamlEnd,
236
- bodyStart: read.root === null ? 0 : bodyStartAfterYaml(document.content, read.yamlEnd),
271
+ root: opening.root,
272
+ yamlEnd: opening.yamlEnd,
273
+ bodyStart: opening.root === null ? 0 : bodyStartAfterYaml(document.content, opening.yamlEnd),
237
274
  };
238
275
  }
239
276
  function partOf(change) {
@@ -293,7 +330,7 @@ function planner(document, internal) {
293
330
  const section = (name, value, prior, at) => {
294
331
  if (spans === undefined) {
295
332
  spans = new Map();
296
- for (const span of headingSpans(content, internal.bodyStart))
333
+ for (const span of sectionSpans(readingOf(document), content.length))
297
334
  if (!spans.has(span.name))
298
335
  spans.set(span.name, span);
299
336
  }
@@ -307,7 +344,7 @@ function planner(document, internal) {
307
344
  const link = (href, to, at) => {
308
345
  if (occurrences === undefined) {
309
346
  occurrences = new Map();
310
- for (const occurrence of destinations(inventoryOf(document))) {
347
+ for (const occurrence of linkDestinations(document)) {
311
348
  const same = occurrences.get(occurrence.href);
312
349
  if (same === undefined)
313
350
  occurrences.set(occurrence.href, [occurrence]);
@@ -322,8 +359,8 @@ function planner(document, internal) {
322
359
  if (destinationText(written, false) === null)
323
360
  return refuse('', 'unwritable-link', written);
324
361
  return found.map((occurrence) => ({
325
- start: internal.bodyStart + occurrence.start,
326
- end: internal.bodyStart + occurrence.end,
362
+ start: occurrence.start,
363
+ end: occurrence.end,
327
364
  text: destinationText(written, occurrence.bracketed),
328
365
  at,
329
366
  }));
@@ -363,19 +400,19 @@ function overlapping(edits, stale) {
363
400
  }
364
401
  }
365
402
  }
366
- function firstHeadings(content, bodyStart) {
403
+ function firstHeadings(reading) {
367
404
  const first = new Map();
368
- for (const span of headingSpans(content, bodyStart)) {
405
+ for (const span of sectionSpans(reading, 0)) {
369
406
  if (!first.has(span.name))
370
407
  first.set(span.name, span.headingStart);
371
408
  }
372
409
  return first;
373
410
  }
374
- function hidden(before, bodyStart, after, shifts) {
375
- const placed = firstHeadings(after, bodyStartOf(after));
411
+ function hidden(before, after, shifts) {
412
+ const placed = firstHeadings(after);
376
413
  let shift = 0;
377
414
  let index = 0;
378
- for (const [name, at] of firstHeadings(before, bodyStart)) {
415
+ for (const [name, at] of firstHeadings(before)) {
379
416
  while (index < shifts.length && shifts[index][0] <= at) {
380
417
  shift += shifts[index][1];
381
418
  index += 1;
@@ -411,6 +448,13 @@ function spliced(source, edits) {
411
448
  pieces.push(source.slice(cursor));
412
449
  return { content: pieces.join(''), shifts };
413
450
  }
451
+ function checkedReading(source, replaced) {
452
+ const checked = spliced(source, replaced);
453
+ return {
454
+ reading: bodyReading(checked.content, bodyStartOf(checked.content)),
455
+ shifts: checked.shifts,
456
+ };
457
+ }
414
458
  export function apply(document, changes) {
415
459
  const internal = internalOf(document);
416
460
  if (!('bodyStart' in internal))
@@ -441,22 +485,24 @@ export function apply(document, changes) {
441
485
  if (stale.size > 0)
442
486
  return { outcome: 'stale', document, stale: [...stale].sort((left, right) => left - right) };
443
487
  const source = document.content;
444
- const { content } = spliced(source, edits);
488
+ const { content, shifts } = spliced(source, edits);
445
489
  if (content === source)
446
490
  return { outcome: 'applied', document };
491
+ const parsed = parse(content);
492
+ if (!parsed.ok)
493
+ return { outcome: 'refuse', refusal: parsed.refusal };
447
494
  const replaced = edits.filter((edit) => {
448
495
  const kind = changes[edit.at].kind;
449
496
  return kind === 'section' || kind === 'preamble';
450
497
  });
451
498
  if (replaced.length > 0) {
452
- const checked = spliced(source, replaced);
453
- const name = hidden(source, internal.bodyStart, checked.content, checked.shifts);
499
+ const checked = replaced.length === edits.length
500
+ ? { reading: readingOf(parsed.document), shifts }
501
+ : checkedReading(source, replaced);
502
+ const name = hidden(readingOf(document), checked.reading, checked.shifts);
454
503
  if (name !== undefined)
455
504
  return { outcome: 'refuse', refusal: refuse('', 'overlapping-section', name) };
456
505
  }
457
- const parsed = parse(content);
458
- if (!parsed.ok)
459
- return { outcome: 'refuse', refusal: parsed.refusal };
460
506
  return { outcome: 'applied', document: parsed.document };
461
507
  }
462
508
  export function serialize(document) {
@@ -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":"AAAA,OAAO,EAQL,KAAK,UAAU,EAEhB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAoB,KAAK,IAAI,EAAE,MAAM,YAAY,CAAC;AAEzD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAG1D,KAAK,MAAM,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QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC;AAmDzE,wBAAgB,KAAK,CACnB,QAAQ,EAAE,IAAI,CAAC,QAAQ,EAAE,MAAM,GAAG,QAAQ,GAAG,UAAU,CAAC,EACxD,EAAE,EAAE;IAAE,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;CAAE,EAClD,KAAK,EAAE,aAAa,EACpB,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,QAAQ,GAAG,SAAS,GAChD,aAAa,CAgBf;AAMD,wBAAgB,UAAU,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,EAAE,CAYzD;AA4BD,wBAAgB,OAAO,CAAC,QAAQ,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,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,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
+ }
@@ -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":"AAIA,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,CAM1E;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"}