@agentshouse/mdmodel 0.1.0-c3208a08ee314 → 0.1.0-c42f1957e050b

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/dist/validate.js CHANGED
@@ -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);
31
35
  }
32
- function itemHrefs(content, items) {
33
- const bodyStart = bodyStartOf(content);
34
- const { uses } = linkInventory(content.slice(bodyStart));
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
+ };
52
+ }
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,96 @@ export function verify(schema) {
53
70
  ...(read.location === undefined ? {} : { location: read.location }),
54
71
  };
55
72
  }
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({ section: name, to: spec.items.reference, hrefs });
100
+ }
101
+ }
102
+ return { sections };
103
+ }
104
+ function linked(file, content) {
105
+ return hrefOccurrences(bodyAfterFrontmatter(content)).every((occurrence) => classifyHref(file, occurrence.href).kind !== 'invalid');
106
+ }
107
+ function reached(file, href, to) {
108
+ const resolved = classifyHref(file, href);
109
+ return { target: resolved.kind === 'path' ? resolved.path : null, to };
110
+ }
111
+ function definitionOf(document, model) {
112
+ const kind = instanceType(document);
113
+ return kind === null || !Object.hasOwn(model.types, kind) ? undefined : model.types[kind];
114
+ }
115
+ export function checkDocument(file, content, model) {
116
+ const parsed = parse(content, file);
117
+ if (!parsed.ok)
118
+ return { ok: false };
119
+ const document = parsed.document;
120
+ const type = document.type ?? BUILT_IN_TYPE;
121
+ if (instanceType(document) === BUILT_IN_TYPE)
122
+ return { ok: true, type, typed: false, references: [] };
123
+ const definition = definitionOf(document, model);
124
+ const read = structure(file, content, document, definition);
125
+ if ('refusal' in read || definition === undefined || !linked(file, content))
126
+ return { ok: false };
127
+ return {
128
+ ok: true,
129
+ type,
130
+ typed: true,
131
+ references: [
132
+ ...references(document, definition).map(({ value, to }) => reached(file, value, to)),
133
+ ...read.sections.flatMap(({ to, hrefs }) => hrefs.map((href) => reached(file, href, to))),
134
+ ],
135
+ };
136
+ }
137
+ export function sectionReferences(file, content, definition) {
138
+ const referring = (name) => {
139
+ const spec = Object.hasOwn(definition.sections, name) ? definition.sections[name] : undefined;
140
+ return spec?.kind === 'list' ? spec.items.reference : undefined;
141
+ };
142
+ if (!Object.keys(definition.sections).some((name) => referring(name) !== undefined))
143
+ return [];
144
+ const uses = usesIn(content);
145
+ const found = [];
146
+ for (const span of headingSpans(content, bodyStartOf(content))) {
147
+ const to = referring(span.name);
148
+ if (to === undefined)
149
+ continue;
150
+ const items = listItems(content, span.bodyStart, span.bodyEnd);
151
+ const hrefs = items === null ? null : itemHrefs(content, items, uses);
152
+ for (const href of hrefs ?? [])
153
+ found.push(reached(file, href, to));
154
+ }
155
+ return found;
156
+ }
56
157
  export function validate(library, context = { binaryTargets: [] }, model = { types: {} }) {
57
158
  const binaryTargets = new Set(context.binaryTargets);
159
+ const binary = (path) => binaryTargets.has(path);
58
160
  const files = Object.keys(library).sort();
59
161
  const types = new Map();
162
+ const typeOf = (path) => types.get(path);
60
163
  const documents = new Map();
61
164
  for (const file of files) {
62
165
  if (binaryTargets.has(file))
@@ -68,98 +171,49 @@ export function validate(library, context = { binaryTargets: [] }, model = { typ
68
171
  if (instanceType(parsed.document) !== BUILT_IN_TYPE)
69
172
  documents.set(file, parsed.document);
70
173
  }
71
- const definitions = new Map(Object.entries(model.types));
72
174
  const sectionReferences = new Map();
73
175
  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);
176
+ const read = structure(file, library[file], document, definitionOf(document, model));
177
+ if ('refusal' in read)
178
+ return read.refusal;
179
+ sectionReferences.set(file, read.sections);
107
180
  }
181
+ const named = new Map();
108
182
  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 });
183
+ const definition = definitionOf(document, model);
184
+ const fields = references(document, definition).map(({ pointer, value, to }) => ({
185
+ reference: reached(file, value, to),
186
+ location: pointer,
187
+ }));
188
+ for (const { reference, location } of fields) {
189
+ if (!reaches(reference, typeOf, binary))
190
+ return refuse(file, 'unresolved-reference', { location });
118
191
  }
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');
124
- }
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
- }
192
+ if (!linked(file, library[file]))
193
+ return refuse(file, 'unresolved-link');
194
+ const sections = sectionReferences
195
+ .get(file)
196
+ .flatMap(({ section, to, hrefs }) => hrefs.map((href) => ({ reference: reached(file, href, to), location: section })));
197
+ for (const { reference, location } of sections) {
198
+ if (!reaches(reference, typeOf, binary))
199
+ return refuse(file, 'unresolved-reference', { location });
130
200
  }
201
+ named.set(file, [...fields, ...sections]);
131
202
  }
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))
203
+ const absent = new Map();
204
+ for (const [file, references] of named) {
205
+ for (const { reference } of references) {
206
+ if (!missing(reference, typeOf, binary))
153
207
  continue;
154
- const referrers = missing.get(resolved.path) ?? [];
208
+ const referrers = absent.get(reference.target) ?? [];
155
209
  referrers.push(file);
156
- missing.set(resolved.path, referrers);
210
+ absent.set(reference.target, referrers);
157
211
  }
158
212
  }
159
- const missingPaths = [...missing.keys()].sort();
213
+ const missingPaths = [...absent.keys()].sort();
160
214
  if (missingPaths.length > 0) {
161
215
  const path = missingPaths[0];
162
- const referrers = [...new Set(missing.get(path) ?? [])].sort();
216
+ const referrers = [...new Set(absent.get(path) ?? [])].sort();
163
217
  return refuse(path, 'referenced-missing-file', { referrers });
164
218
  }
165
219
  return { verdict: 'accept' };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentshouse/mdmodel",
3
- "version": "0.1.0-c3208a08ee314",
3
+ "version": "0.1.0-c42f1957e050b",
4
4
  "description": "MD Type language, parser, validator, structured change application, Markdown syntax profile, and conformance fixtures",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -26,6 +26,8 @@
26
26
  "README.md"
27
27
  ],
28
28
  "dependencies": {
29
- "@cfworker/json-schema": "4.1.1"
29
+ "@cfworker/json-schema": "4.1.1",
30
+ "decode-named-character-reference": "1.3.0",
31
+ "micromark-util-decode-numeric-character-reference": "2.0.2"
30
32
  }
31
33
  }