@agentshouse/mdmodel 0.1.0-c64dbada0730d → 0.1.0-c669bdbad8a03

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 (52) hide show
  1. package/API.md +206 -202
  2. package/README.md +15 -18
  3. package/dist/blocks.d.ts.map +1 -1
  4. package/dist/blocks.js +7 -14
  5. package/dist/condition.d.ts +2 -1
  6. package/dist/condition.d.ts.map +1 -1
  7. package/dist/condition.js +30 -15
  8. package/dist/definition.d.ts +15 -0
  9. package/dist/definition.d.ts.map +1 -1
  10. package/dist/definition.js +226 -26
  11. package/dist/divide.d.ts.map +1 -1
  12. package/dist/divide.js +18 -8
  13. package/dist/document.d.ts +5 -2
  14. package/dist/document.d.ts.map +1 -1
  15. package/dist/document.js +315 -165
  16. package/dist/fields.d.ts +2 -0
  17. package/dist/fields.d.ts.map +1 -1
  18. package/dist/fields.js +7 -0
  19. package/dist/index.d.ts +12 -7
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +7 -5
  22. package/dist/links.d.ts +2 -0
  23. package/dist/links.d.ts.map +1 -1
  24. package/dist/links.js +23 -12
  25. package/dist/paths.d.ts +10 -1
  26. package/dist/paths.d.ts.map +1 -1
  27. package/dist/paths.js +115 -12
  28. package/dist/project.d.ts +1 -0
  29. package/dist/project.d.ts.map +1 -1
  30. package/dist/project.js +35 -10
  31. package/dist/references.d.ts +2 -2
  32. package/dist/references.d.ts.map +1 -1
  33. package/dist/references.js +6 -1
  34. package/dist/schema.js +2 -2
  35. package/dist/types.d.ts +44 -1
  36. package/dist/types.d.ts.map +1 -1
  37. package/dist/validate.d.ts +8 -1
  38. package/dist/validate.d.ts.map +1 -1
  39. package/dist/validate.js +205 -100
  40. package/dist/yaml.d.ts +7 -3
  41. package/dist/yaml.d.ts.map +1 -1
  42. package/dist/yaml.js +72 -43
  43. package/fixtures/README.md +1 -1
  44. package/fixtures/cases/cross-room-link/context.json +1 -0
  45. package/fixtures/cases/cross-room-link/expected.json +1 -0
  46. package/fixtures/cases/cross-room-link/library/clients/acme.md +6 -0
  47. package/fixtures/cases/cross-room-link/schema.yaml +3 -0
  48. package/fixtures/cases/cross-room-reference/context.json +1 -0
  49. package/fixtures/cases/cross-room-reference/expected.json +1 -0
  50. package/fixtures/cases/cross-room-reference/library/clients/acme.md +9 -0
  51. package/fixtures/cases/cross-room-reference/schema.yaml +9 -0
  52. package/package.json +4 -2
package/dist/validate.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { ATTACHMENT, BUILT_IN_TYPE, readSchema } from './definition.js';
2
2
  import { parse } from './document.js';
3
3
  import { hrefOccurrences, linkInventory } from './links.js';
4
- import { classifyHref } from './paths.js';
4
+ import { classifyHref, roomTarget } from './paths.js';
5
5
  import { references } from './references.js';
6
6
  import { refusal } from './refuse.js';
7
7
  import { fitsItem, frontmatterRefusal } from './schema.js';
@@ -18,25 +18,42 @@ function instanceType(document) {
18
18
  return null;
19
19
  return raw;
20
20
  }
21
- function reaches(file, href, to, types, binaryTargets) {
22
- const resolved = classifyHref(file, href);
23
- if (resolved.kind !== 'path')
21
+ function reaches(reference, typeOf, binary) {
22
+ if (reference.target === null)
24
23
  return false;
25
- if (to.includes(ATTACHMENT))
24
+ if (reference.to.includes(ATTACHMENT))
26
25
  return true;
27
- if (binaryTargets.has(resolved.path))
26
+ if (binary(reference.target))
27
+ return false;
28
+ const held = typeOf(reference.target);
29
+ return held === undefined || reference.to.includes(held);
30
+ }
31
+ function missing(reference, typeOf, binary) {
32
+ if (reference.to.includes(ATTACHMENT) || reference.target === null)
28
33
  return false;
29
- const held = types.get(resolved.path);
30
- return held === undefined || to.includes(held);
34
+ return typeOf(reference.target) === undefined && !binary(reference.target);
35
+ }
36
+ export function resolves(reference, typeOf, binary) {
37
+ return reaches(reference, typeOf, binary) && !missing(reference, typeOf, binary);
38
+ }
39
+ function usesIn(content) {
40
+ let held;
41
+ return () => {
42
+ if (held !== undefined)
43
+ return held;
44
+ const bodyStart = bodyStartOf(content);
45
+ held = linkInventory(content.slice(bodyStart)).uses.map((use) => ({
46
+ ...use,
47
+ start: bodyStart + use.start,
48
+ end: bodyStart + use.end,
49
+ }));
50
+ return held;
51
+ };
31
52
  }
32
- function itemHrefs(content, items) {
33
- const bodyStart = bodyStartOf(content);
34
- const { uses } = linkInventory(content.slice(bodyStart));
53
+ function itemHrefs(content, items, uses) {
35
54
  const hrefs = [];
36
55
  for (const item of items) {
37
- const link = uses.find((use) => bodyStart + use.start === item.start &&
38
- bodyStart + use.end === item.end &&
39
- content[item.start] === '[');
56
+ const link = uses().find((use) => use.start === item.start && use.end === item.end && content[item.start] === '[');
40
57
  if (link === undefined)
41
58
  return null;
42
59
  hrefs.push(link.destination.href);
@@ -53,10 +70,142 @@ export function verify(schema) {
53
70
  ...(read.location === undefined ? {} : { location: read.location }),
54
71
  };
55
72
  }
56
- export function validate(library, context = { binaryTargets: [] }, model = { types: {} }) {
73
+ function structure(file, content, document, definition) {
74
+ if (definition === undefined)
75
+ return { refusal: refuse(file, 'unknown-type', { location: '/type' }) };
76
+ const fieldFailure = frontmatterRefusal(definition, document.fields);
77
+ if (fieldFailure !== undefined) {
78
+ return { refusal: refuse(file, fieldFailure.rule, { location: fieldFailure.location }) };
79
+ }
80
+ const spans = headingSpans(content, bodyStartOf(content));
81
+ const uses = usesIn(content);
82
+ const sections = [];
83
+ for (const [name, spec] of Object.entries(definition.sections)) {
84
+ const occurrences = spans.filter((one) => one.name === name);
85
+ if (occurrences.length === 0 && spec.required)
86
+ return { refusal: refuse(file, 'required-section', { location: name }) };
87
+ if (spec.kind !== 'list')
88
+ continue;
89
+ for (const span of occurrences) {
90
+ const items = listItems(content, span.bodyStart, span.bodyEnd);
91
+ if (items === null ||
92
+ items.some((item) => !fitsItem(definition, name, content.slice(item.start, item.end))))
93
+ return { refusal: refuse(file, 'structured-section', { location: name }) };
94
+ if (spec.items.reference === undefined)
95
+ continue;
96
+ const hrefs = itemHrefs(content, items, uses);
97
+ if (hrefs === null)
98
+ return { refusal: refuse(file, 'structured-section', { location: name }) };
99
+ sections.push({
100
+ section: name,
101
+ to: spec.items.reference,
102
+ crossRoom: spec.items.crossRoom === true,
103
+ hrefs,
104
+ });
105
+ }
106
+ }
107
+ return { sections };
108
+ }
109
+ function linked(file, content) {
110
+ return hrefOccurrences(bodyAfterFrontmatter(content)).every((occurrence) => classifyHref(file, occurrence.href).kind !== 'invalid');
111
+ }
112
+ function reached(file, href, to) {
113
+ const resolved = classifyHref(file, href);
114
+ return { target: resolved.kind === 'path' ? resolved.path : null, to };
115
+ }
116
+ function crossing(file, href, crossRoom) {
117
+ return crossRoom === true && classifyHref(file, href).kind === 'room';
118
+ }
119
+ function definitionOf(document, model) {
120
+ const kind = instanceType(document);
121
+ return kind === null || !Object.hasOwn(model.types, kind) ? undefined : model.types[kind];
122
+ }
123
+ export function checkDocument(file, content, model) {
124
+ const parsed = parse(content, file);
125
+ if (!parsed.ok)
126
+ return { ok: false };
127
+ const document = parsed.document;
128
+ const type = document.type ?? BUILT_IN_TYPE;
129
+ if (instanceType(document) === BUILT_IN_TYPE)
130
+ return { ok: true, type, typed: false, references: [] };
131
+ const definition = definitionOf(document, model);
132
+ const read = structure(file, content, document, definition);
133
+ if ('refusal' in read || definition === undefined || !linked(file, content))
134
+ return { ok: false };
135
+ return {
136
+ ok: true,
137
+ type,
138
+ typed: true,
139
+ references: [
140
+ ...references(document, definition).flatMap(({ value, to, crossRoom }) => crossing(file, value, crossRoom) ? [] : [reached(file, value, to)]),
141
+ ...read.sections.flatMap(({ to, crossRoom, hrefs }) => hrefs.flatMap((href) => (crossing(file, href, crossRoom) ? [] : [reached(file, href, to)]))),
142
+ ],
143
+ };
144
+ }
145
+ export function sectionReferences(file, content, definition) {
146
+ const referring = (name) => {
147
+ const spec = Object.hasOwn(definition.sections, name) ? definition.sections[name] : undefined;
148
+ return spec?.kind === 'list' && spec.items.reference !== undefined ? spec.items : undefined;
149
+ };
150
+ if (!Object.keys(definition.sections).some((name) => referring(name) !== undefined))
151
+ return [];
152
+ const uses = usesIn(content);
153
+ const found = [];
154
+ for (const span of headingSpans(content, bodyStartOf(content))) {
155
+ const items = referring(span.name);
156
+ if (items === undefined)
157
+ continue;
158
+ const listed = listItems(content, span.bodyStart, span.bodyEnd);
159
+ const hrefs = listed === null ? null : itemHrefs(content, listed, uses);
160
+ for (const href of hrefs ?? [])
161
+ if (!crossing(file, href, items.crossRoom))
162
+ found.push(reached(file, href, items.reference));
163
+ }
164
+ return found;
165
+ }
166
+ export function bodyRoomLinks(body) {
167
+ const found = [];
168
+ for (const href of new Set(hrefOccurrences(body).map((occurrence) => occurrence.href))) {
169
+ const room = roomTarget(href);
170
+ if (room !== null)
171
+ found.push({ kind: 'link', href, ...room });
172
+ }
173
+ return found;
174
+ }
175
+ export function roomReferences(file, content, definition) {
176
+ const found = bodyRoomLinks(bodyAfterFrontmatter(content));
177
+ if (definition === undefined)
178
+ return found;
179
+ const parsed = parse(content, file);
180
+ if (!parsed.ok)
181
+ return found;
182
+ for (const { pointer, value, to, crossRoom } of references(parsed.document, definition)) {
183
+ const room = crossRoom === true ? roomTarget(value) : null;
184
+ if (room !== null)
185
+ found.push({ kind: 'field', pointer, value, to, ...room });
186
+ }
187
+ const uses = usesIn(content);
188
+ for (const span of headingSpans(content, bodyStartOf(content))) {
189
+ const spec = Object.hasOwn(definition.sections, span.name)
190
+ ? definition.sections[span.name]
191
+ : undefined;
192
+ if (spec?.kind !== 'list' || spec.items.reference === undefined || !spec.items.crossRoom)
193
+ continue;
194
+ const listed = listItems(content, span.bodyStart, span.bodyEnd);
195
+ for (const href of (listed === null ? null : itemHrefs(content, listed, uses)) ?? []) {
196
+ const room = roomTarget(href);
197
+ if (room !== null)
198
+ found.push({ kind: 'item', section: span.name, href, to: spec.items.reference, ...room });
199
+ }
200
+ }
201
+ return found;
202
+ }
203
+ export function* validating(library, context = { binaryTargets: [] }, model = { types: {} }) {
57
204
  const binaryTargets = new Set(context.binaryTargets);
205
+ const binary = (path) => binaryTargets.has(path);
58
206
  const files = Object.keys(library).sort();
59
207
  const types = new Map();
208
+ const typeOf = (path) => types.get(path);
60
209
  const documents = new Map();
61
210
  for (const file of files) {
62
211
  if (binaryTargets.has(file))
@@ -67,100 +216,56 @@ export function validate(library, context = { binaryTargets: [] }, model = { typ
67
216
  types.set(file, parsed.document.type ?? BUILT_IN_TYPE);
68
217
  if (instanceType(parsed.document) !== BUILT_IN_TYPE)
69
218
  documents.set(file, parsed.document);
219
+ yield file;
70
220
  }
71
- const definitions = new Map(Object.entries(model.types));
72
221
  const sectionReferences = new Map();
73
222
  for (const [file, document] of documents) {
74
- const kind = instanceType(document);
75
- if (kind === BUILT_IN_TYPE)
76
- continue;
77
- const definition = kind === null ? undefined : definitions.get(kind);
78
- if (definition === undefined)
79
- return refuse(file, 'unknown-type', { location: '/type' });
80
- const fieldFailure = frontmatterRefusal(definition, document.fields);
81
- if (fieldFailure !== undefined) {
82
- return refuse(file, fieldFailure.rule, { location: fieldFailure.location });
83
- }
84
- const content = library[file];
85
- const spans = headingSpans(content, bodyStartOf(content));
86
- const references = [];
87
- for (const [name, spec] of Object.entries(definition.sections)) {
88
- const occurrences = spans.filter((one) => one.name === name);
89
- if (occurrences.length === 0 && spec.required)
90
- return refuse(file, 'required-section', { location: name });
91
- if (spec.kind !== 'list')
92
- continue;
93
- for (const span of occurrences) {
94
- const items = listItems(content, span.bodyStart, span.bodyEnd);
95
- if (items === null ||
96
- items.some((item) => !fitsItem(definition, name, content.slice(item.start, item.end))))
97
- return refuse(file, 'structured-section', { location: name });
98
- if (spec.items.reference === undefined)
99
- continue;
100
- const hrefs = itemHrefs(content, items);
101
- if (hrefs === null)
102
- return refuse(file, 'structured-section', { location: name });
103
- references.push({ section: name, to: spec.items.reference, hrefs });
104
- }
105
- }
106
- sectionReferences.set(file, references);
223
+ const read = structure(file, library[file], document, definitionOf(document, model));
224
+ if ('refusal' in read)
225
+ return read.refusal;
226
+ sectionReferences.set(file, read.sections);
227
+ yield file;
107
228
  }
229
+ let least;
108
230
  for (const [file, document] of documents) {
109
- const kind = instanceType(document);
110
- if (kind === null || kind === BUILT_IN_TYPE)
111
- continue;
112
- const definition = definitions.get(kind);
113
- if (definition === undefined)
114
- continue;
115
- for (const { pointer, to, value } of references(document, definition)) {
116
- if (!reaches(file, value, to, types, binaryTargets))
117
- return refuse(file, 'unresolved-reference', { location: pointer });
118
- }
119
- const body = bodyAfterFrontmatter(library[file]);
120
- for (const occurrence of hrefOccurrences(body)) {
121
- const resolved = classifyHref(file, occurrence.href);
122
- if (resolved.kind === 'invalid')
123
- return refuse(file, 'unresolved-link');
231
+ const definition = definitionOf(document, model);
232
+ const fields = references(document, definition).flatMap(({ pointer, value, to, crossRoom }) => crossing(file, value, crossRoom)
233
+ ? []
234
+ : [{ reference: reached(file, value, to), location: pointer }]);
235
+ for (const { reference, location } of fields) {
236
+ if (!reaches(reference, typeOf, binary))
237
+ return refuse(file, 'unresolved-reference', { location });
124
238
  }
125
- for (const { section, to, hrefs } of sectionReferences.get(file)) {
126
- for (const href of hrefs) {
127
- if (!reaches(file, href, to, types, binaryTargets))
128
- return refuse(file, 'unresolved-reference', { location: section });
129
- }
239
+ if (!linked(file, library[file]))
240
+ return refuse(file, 'unresolved-link');
241
+ const sections = sectionReferences
242
+ .get(file)
243
+ .flatMap(({ section, to, crossRoom, hrefs }) => hrefs.flatMap((href) => crossing(file, href, crossRoom)
244
+ ? []
245
+ : [{ reference: reached(file, href, to), location: section }]));
246
+ for (const { reference, location } of sections) {
247
+ if (!reaches(reference, typeOf, binary))
248
+ return refuse(file, 'unresolved-reference', { location });
130
249
  }
131
- }
132
- const missing = new Map();
133
- for (const [file, document] of documents) {
134
- const kind = instanceType(document);
135
- if (kind === null || kind === BUILT_IN_TYPE)
136
- continue;
137
- const definition = definitions.get(kind);
138
- if (definition === undefined)
139
- continue;
140
- const named = [
141
- ...references(document, definition),
142
- ...sectionReferences
143
- .get(file)
144
- .flatMap(({ to, hrefs }) => hrefs.map((value) => ({ value, to }))),
145
- ];
146
- for (const { value, to } of named) {
147
- if (to.includes(ATTACHMENT))
148
- continue;
149
- const resolved = classifyHref(file, value);
150
- if (resolved.kind !== 'path')
151
- continue;
152
- if (types.has(resolved.path) || binaryTargets.has(resolved.path))
250
+ for (const { reference } of [...fields, ...sections]) {
251
+ if (!missing(reference, typeOf, binary))
153
252
  continue;
154
- const referrers = missing.get(resolved.path) ?? [];
155
- referrers.push(file);
156
- missing.set(resolved.path, referrers);
253
+ const path = reference.target;
254
+ if (least === undefined || path < least.path)
255
+ least = { path, referrers: [] };
256
+ if (path === least.path && least.referrers.at(-1) !== file)
257
+ least.referrers.push(file);
157
258
  }
259
+ yield file;
158
260
  }
159
- const missingPaths = [...missing.keys()].sort();
160
- if (missingPaths.length > 0) {
161
- const path = missingPaths[0];
162
- const referrers = [...new Set(missing.get(path) ?? [])].sort();
163
- return refuse(path, 'referenced-missing-file', { referrers });
164
- }
261
+ if (least !== undefined)
262
+ return refuse(least.path, 'referenced-missing-file', { referrers: least.referrers });
165
263
  return { verdict: 'accept' };
166
264
  }
265
+ export function validate(library, context = { binaryTargets: [] }, model = { types: {} }) {
266
+ const steps = validating(library, context, model);
267
+ let step = steps.next();
268
+ while (step.done !== true)
269
+ step = steps.next();
270
+ return step.value;
271
+ }
package/dist/yaml.d.ts CHANGED
@@ -16,8 +16,7 @@ export type YamlSeq = {
16
16
  };
17
17
  export type YamlEntry = {
18
18
  readonly key: string;
19
- readonly keyStart: number;
20
- readonly keyEnd: number;
19
+ readonly after: number;
21
20
  readonly value: YamlNode;
22
21
  };
23
22
  export type YamlMap = {
@@ -32,7 +31,12 @@ export declare function parseYamlMapping(text: string, start: number, end: numbe
32
31
  export declare function jsValue(node: YamlNode): unknown;
33
32
  export declare function mapEntry(map: YamlMap, key: string): YamlEntry | undefined;
34
33
  export declare function isPlainSafe(text: string): boolean;
35
- export declare function serializeJs(value: unknown): string;
34
+ export type FrontmatterValue = string | number | boolean | null | readonly FrontmatterValue[] | {
35
+ readonly [name: string]: FrontmatterValue;
36
+ };
37
+ export declare function plain(value: unknown): value is Record<string, unknown>;
38
+ export declare function frontmatterValue(value: unknown): value is FrontmatterValue;
39
+ export declare function serializeJs(value: FrontmatterValue): string;
36
40
  export declare function isRecord(value: unknown): value is Record<string, unknown>;
37
41
  export declare function serializeKey(name: string): string;
38
42
  export declare function bodyStartAfterYaml(content: string, yamlEnd: number): number;
@@ -1 +1 @@
1
- {"version":3,"file":"yaml.d.ts","sourceRoot":"","sources":["../src/yaml.ts"],"names":[],"mappings":"AAAA,qBAAa,SAAU,SAAQ,KAAK;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM;IAAnC,YAAqB,MAAM,EAAE,MAAM,EAGlC;CACF;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;IACjD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,SAAS,QAAQ,EAAE,CAAC;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,QAAQ,CAAC;CAC1B,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,CAAC,IAAI,EAAE,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AED,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAG3E;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAGnD;AAED,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAE5D;AAED,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,MAAM,GACd;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,UAAU,CAAA;CAAE,GAAG;IAAE,IAAI,EAAE,IAAI,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAqB9F;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,GAAG,OAAO,CAgBjE;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,CAEzD"}
package/dist/yaml.js CHANGED
@@ -84,6 +84,30 @@ export function interpretPlain(text) {
84
84
  return Number(text);
85
85
  return text;
86
86
  }
87
+ const ESCAPES = {
88
+ b: '\b',
89
+ f: '\f',
90
+ n: '\n',
91
+ r: '\r',
92
+ t: '\t',
93
+ '"': '"',
94
+ '/': '/',
95
+ '\\': '\\',
96
+ };
97
+ function parseEscape(c) {
98
+ c.i += 1;
99
+ const escaped = peek(c);
100
+ const held = ESCAPES[escaped];
101
+ if (held !== undefined) {
102
+ c.i += 1;
103
+ return held;
104
+ }
105
+ const hex = c.text.slice(c.i + 1, c.i + 5);
106
+ if (escaped !== 'u' || c.i + 5 > c.end || !/^[0-9A-Fa-f]{4}$/.test(hex))
107
+ throw new YamlError(c.i);
108
+ c.i += 5;
109
+ return String.fromCharCode(Number.parseInt(hex, 16));
110
+ }
87
111
  function parseQuoted(c) {
88
112
  const quote = peek(c);
89
113
  const start = c.i;
@@ -91,31 +115,19 @@ function parseQuoted(c) {
91
115
  let out = '';
92
116
  while (c.i < c.end) {
93
117
  const ch = peek(c);
118
+ if (quote === "'" && ch === "'" && c.i + 1 < c.end && c.text[c.i + 1] === "'") {
119
+ out += "'";
120
+ c.i += 2;
121
+ continue;
122
+ }
94
123
  if (ch === quote) {
95
124
  c.i += 1;
96
125
  return scalar(out, start, c.i);
97
126
  }
98
127
  if (ch === '\n' || ch === '\r')
99
128
  throw new YamlError(c.i);
100
- if (quote === "'" && ch === "'" && c.text[c.i + 1] === "'") {
101
- out += "'";
102
- c.i += 2;
103
- continue;
104
- }
105
129
  if (quote === '"' && ch === '\\') {
106
- c.i += 1;
107
- const escaped = peek(c);
108
- if (escaped === 'n')
109
- out += '\n';
110
- else if (escaped === 't')
111
- out += '\t';
112
- else if (escaped === '"')
113
- out += '"';
114
- else if (escaped === '\\')
115
- out += '\\';
116
- else
117
- throw new YamlError(c.i);
118
- c.i += 1;
130
+ out += parseEscape(c);
119
131
  continue;
120
132
  }
121
133
  out += ch;
@@ -208,11 +220,12 @@ function parseFlowMap(c) {
208
220
  if (peek(c) !== ':')
209
221
  throw new YamlError(c.i);
210
222
  c.i += 1;
223
+ const after = c.i;
211
224
  const value = parseFlowValue(c);
212
225
  if (seen.has(key.name))
213
226
  throw new YamlError(key.start);
214
227
  seen.add(key.name);
215
- entries.push({ key: key.name, keyStart: key.start, keyEnd: key.end, value });
228
+ entries.push({ key: key.name, after, value });
216
229
  skipFlowSpace(c);
217
230
  if (peek(c) === ',') {
218
231
  c.i += 1;
@@ -254,6 +267,7 @@ function parseFlowSeq(c) {
254
267
  throw new YamlError(c.i);
255
268
  }
256
269
  function parseValue(c, parentKeyIndent) {
270
+ const at = c.i;
257
271
  skipSpaces(c);
258
272
  if (peek(c) === '#')
259
273
  skipToEol(c);
@@ -262,12 +276,12 @@ function parseValue(c, parentKeyIndent) {
262
276
  skipEol(c);
263
277
  skipBlankAndComments(c);
264
278
  if (c.i >= c.end)
265
- return scalar(null, c.i, c.i);
279
+ return scalar(null, at, at);
266
280
  const nextIndent = indentAt(c);
267
281
  if (nextIndent === parentKeyIndent && seqAhead(c))
268
282
  return parseBlockSeq(c, nextIndent);
269
283
  if (nextIndent <= parentKeyIndent)
270
- return scalar(null, c.i, c.i);
284
+ return scalar(null, at, at);
271
285
  if (peek(c) === '-')
272
286
  return parseBlockSeq(c, nextIndent);
273
287
  if (peek(c) === '[')
@@ -315,9 +329,6 @@ function lineStartOf(c) {
315
329
  j -= 1;
316
330
  return j;
317
331
  }
318
- function valueEnd(value, marked) {
319
- return value.kind === 'scalar' && value.start === value.end ? marked : value.end;
320
- }
321
332
  function parseSeqItem(c, indent) {
322
333
  skipSpaces(c);
323
334
  if (peek(c) !== '#' && peek(c) !== '\n' && peek(c) !== '\r' && c.i < c.end && mappingAhead(c))
@@ -337,10 +348,9 @@ function parseBlockSeq(c, indent) {
337
348
  if (peek(c) !== '-')
338
349
  break;
339
350
  c.i += 1;
340
- const marked = c.i;
341
351
  const item = parseSeqItem(c, indent);
342
352
  items.push(item);
343
- end = valueEnd(item, marked);
353
+ end = item.end;
344
354
  }
345
355
  return { kind: 'seq', items, start, end };
346
356
  }
@@ -366,13 +376,13 @@ function parseBlockMap(c, indent, inline = false) {
366
376
  if (peek(c) !== ':')
367
377
  throw new YamlError(c.i);
368
378
  c.i += 1;
369
- const marked = c.i;
379
+ const after = c.i;
370
380
  const value = parseValue(c, indent);
371
381
  if (seen.has(key.name))
372
382
  throw new YamlError(key.start);
373
383
  seen.add(key.name);
374
- entries.push({ key: key.name, keyStart: key.start, keyEnd: key.end, value });
375
- end = valueEnd(value, marked);
384
+ entries.push({ key: key.name, after, value });
385
+ end = value.end;
376
386
  }
377
387
  return { kind: 'map', entries, start, end };
378
388
  }
@@ -418,24 +428,42 @@ export function isPlainSafe(text) {
418
428
  return false;
419
429
  return true;
420
430
  }
431
+ export function plain(value) {
432
+ if (!isRecord(value))
433
+ return false;
434
+ const prototype = Object.getPrototypeOf(value);
435
+ return prototype === Object.prototype || prototype === null;
436
+ }
437
+ function listed(value) {
438
+ const keys = Object.keys(value);
439
+ return keys.length === value.length && keys.every((key, index) => key === String(index));
440
+ }
441
+ function heldWithin(value, within) {
442
+ if (value === null || typeof value === 'string' || typeof value === 'boolean')
443
+ return true;
444
+ if (typeof value === 'number')
445
+ return Number.isFinite(value);
446
+ if (typeof value !== 'object' || within.includes(value))
447
+ return false;
448
+ const path = [...within, value];
449
+ const held = (item) => heldWithin(item, path);
450
+ if (Array.isArray(value))
451
+ return listed(value) && value.every(held);
452
+ return plain(value) && Object.values(value).every(held);
453
+ }
454
+ export function frontmatterValue(value) {
455
+ return heldWithin(value, []);
456
+ }
421
457
  export function serializeJs(value) {
422
- if (value === null)
423
- return 'null';
424
- if (value === true)
425
- return 'true';
426
- if (value === false)
427
- return 'false';
428
- if (typeof value === 'number' && Number.isFinite(value))
429
- return JSON.stringify(value);
430
458
  if (typeof value === 'string')
431
459
  return isPlainSafe(value) ? value : JSON.stringify(value);
460
+ if (value === null || typeof value !== 'object')
461
+ return JSON.stringify(value);
432
462
  if (Array.isArray(value))
433
463
  return `[${value.map(serializeJs).join(', ')}]`;
434
- if (isRecord(value))
435
- return `{${Object.entries(value)
436
- .map(([key, held]) => `${serializeKey(key)}: ${serializeJs(held)}`)
437
- .join(', ')}}`;
438
- throw new YamlError(0);
464
+ return `{${Object.entries(value)
465
+ .map(([key, held]) => `${serializeKey(key)}: ${serializeJs(held)}`)
466
+ .join(', ')}}`;
439
467
  }
440
468
  export function isRecord(value) {
441
469
  return value !== null && typeof value === 'object' && !Array.isArray(value);
@@ -485,7 +513,8 @@ export function equalValue(left, right) {
485
513
  if (left === right)
486
514
  return true;
487
515
  if (Array.isArray(left) && Array.isArray(right)) {
488
- return (left.length === right.length && left.every((item, index) => equalValue(item, right[index])));
516
+ return (left.length === right.length &&
517
+ Array.from(left).every((item, index) => equalValue(item, right[index])));
489
518
  }
490
519
  if (isRecord(left) && isRecord(right)) {
491
520
  const keys = Object.keys(left);
@@ -136,6 +136,6 @@ Library is accepted too, so `unresolved-link` never names it and
136
136
  ## YAML subset
137
137
 
138
138
  Frontmatter and a schema text are each a mapping in a closed YAML subset: block and flow mappings,
139
- block and flow sequences, a block sequence item that opens a mapping, plain and quoted scalars, `true`, `false`, `null`,
139
+ block and flow sequences, a block sequence item that opens a mapping, plain and quoted scalars (a single-quoted scalar doubles its apostrophe, a double-quoted one takes JSON's escapes), `true`, `false`, `null`,
140
140
  and decimal numbers. It does not take YAML 1.1 boolean or timestamp aliases,
141
141
  octal numbers, anchors, tags, or block scalars. A mapping carries each key once.
@@ -0,0 +1 @@
1
+ { "binaryTargets": [] }
@@ -0,0 +1 @@
1
+ { "verdict": "accept" }
@@ -0,0 +1,6 @@
1
+ ---
2
+ type: client
3
+ name: Acme
4
+ ---
5
+
6
+ Paid by [the March invoice](/rooms/finance/invoices/march.md#total).
@@ -0,0 +1,3 @@
1
+ client:
2
+ properties:
3
+ name: { type: string }
@@ -0,0 +1 @@
1
+ { "binaryTargets": [] }
@@ -0,0 +1 @@
1
+ { "verdict": "accept" }
@@ -0,0 +1,9 @@
1
+ ---
2
+ type: client
3
+ name: Acme
4
+ finance: /rooms/finance/invoices/acme.md
5
+ ---
6
+
7
+ ## Invoices
8
+
9
+ - [March](/rooms/finance/invoices/march.md)
@@ -0,0 +1,9 @@
1
+ client:
2
+ properties:
3
+ name: { type: string }
4
+ finance: { type: string, reference: invoice, cross_room: true }
5
+ sections:
6
+ Invoices: { kind: list, items: { type: string, reference: invoice, cross_room: true } }
7
+ invoice:
8
+ properties:
9
+ name: { type: string }