@agentshouse/mdmodel 0.1.0-alpha.15 → 0.1.0-alpha.17

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 (124) hide show
  1. package/API.md +296 -0
  2. package/README.md +25 -287
  3. package/dist/definition.d.ts +7 -3
  4. package/dist/definition.d.ts.map +1 -1
  5. package/dist/definition.js +83 -38
  6. package/dist/definition.js.map +1 -1
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +1 -1
  10. package/dist/index.js.map +1 -1
  11. package/dist/paths.d.ts +0 -2
  12. package/dist/paths.d.ts.map +1 -1
  13. package/dist/paths.js +0 -7
  14. package/dist/paths.js.map +1 -1
  15. package/dist/project.d.ts.map +1 -1
  16. package/dist/project.js +3 -15
  17. package/dist/project.js.map +1 -1
  18. package/dist/types.d.ts +6 -5
  19. package/dist/types.d.ts.map +1 -1
  20. package/dist/types.js.map +1 -1
  21. package/dist/validate.d.ts +2 -3
  22. package/dist/validate.d.ts.map +1 -1
  23. package/dist/validate.js +42 -86
  24. package/dist/validate.js.map +1 -1
  25. package/dist/version.d.ts +1 -1
  26. package/dist/version.js +1 -1
  27. package/dist/version.js.map +1 -1
  28. package/dist/yaml.js +1 -1
  29. package/dist/yaml.js.map +1 -1
  30. package/fixtures/README.md +46 -49
  31. package/fixtures/cases/binary-any-reference/schema.yaml +4 -0
  32. package/fixtures/cases/binary-document-collision/schema.yaml +2 -0
  33. package/fixtures/cases/binary-link-outside-collection/schema.yaml +2 -0
  34. package/fixtures/cases/binary-prototype-reference/schema.yaml +4 -0
  35. package/fixtures/cases/binary-typed-reference/schema.yaml +5 -0
  36. package/fixtures/cases/bracketed-link-definition/schema.yaml +6 -0
  37. package/fixtures/cases/declared-binary-links/schema.yaml +2 -0
  38. package/fixtures/cases/duplicate-link-definition/schema.yaml +6 -0
  39. package/fixtures/cases/fenced-link-definition/schema.yaml +6 -0
  40. package/fixtures/cases/field-enum/schema.yaml +7 -0
  41. package/fixtures/cases/frontmatter-schema/expected.json +6 -0
  42. package/fixtures/cases/frontmatter-schema/schema.yaml +6 -0
  43. package/fixtures/cases/interrupted-link-definition/schema.yaml +2 -0
  44. package/fixtures/cases/long-fence-link-definition/schema.yaml +2 -0
  45. package/fixtures/cases/malformed-definition/expected.json +1 -2
  46. package/fixtures/cases/malformed-definition/schema.yaml +4 -0
  47. package/fixtures/cases/member-document/context.json +1 -0
  48. package/fixtures/cases/member-document/library/people/ana.md +5 -0
  49. package/fixtures/cases/member-document/schema.yaml +5 -0
  50. package/fixtures/cases/missing-binary-image/schema.yaml +2 -0
  51. package/fixtures/cases/missing-binary-link/schema.yaml +2 -0
  52. package/fixtures/cases/missing-required-field/schema.yaml +7 -0
  53. package/fixtures/cases/missing-section-item-kind/expected.json +1 -2
  54. package/fixtures/cases/missing-section-item-kind/schema.yaml +6 -0
  55. package/fixtures/cases/paragraph-link-definition/schema.yaml +2 -0
  56. package/fixtures/cases/prototype-field/schema.yaml +6 -0
  57. package/fixtures/cases/quoted-link-definition/schema.yaml +2 -0
  58. package/fixtures/cases/referenced-missing-file/schema.yaml +10 -0
  59. package/fixtures/cases/second-member-document/context.json +1 -0
  60. package/fixtures/cases/second-member-document/expected.json +6 -0
  61. package/fixtures/cases/second-member-document/schema.yaml +5 -0
  62. package/fixtures/cases/structured-section/schema.yaml +7 -0
  63. package/fixtures/cases/submission-keys/schema.yaml +4 -0
  64. package/fixtures/cases/truncated-link-definition/schema.yaml +2 -0
  65. package/fixtures/cases/types-folder-content/schema.yaml +4 -0
  66. package/fixtures/cases/undeclared-field/schema.yaml +6 -0
  67. package/fixtures/cases/unknown-type/expected.json +7 -0
  68. package/fixtures/cases/unknown-type/schema.yaml +4 -0
  69. package/fixtures/cases/unlisted-reference-type/context.json +1 -0
  70. package/fixtures/cases/unlisted-reference-type/expected.json +6 -0
  71. package/fixtures/cases/unlisted-reference-type/schema.yaml +4 -0
  72. package/fixtures/cases/unresolved-link/schema.yaml +6 -0
  73. package/fixtures/cases/valid-typed-document/schema.yaml +15 -0
  74. package/fixtures/cases/wide-item-fence-link/schema.yaml +2 -0
  75. package/fixtures/division.json +1 -21
  76. package/fixtures/projection.json +1 -24
  77. package/package.json +3 -12
  78. package/fixtures/cases/binary-any-reference/library/types/note.md +0 -5
  79. package/fixtures/cases/binary-document-collision/library/types/note.md +0 -3
  80. package/fixtures/cases/binary-link-outside-collection/library/types/note.md +0 -3
  81. package/fixtures/cases/binary-prototype-reference/library/types/note.md +0 -5
  82. package/fixtures/cases/binary-typed-reference/library/types/note.md +0 -5
  83. package/fixtures/cases/binary-typed-reference/library/types/person.md +0 -3
  84. package/fixtures/cases/bracketed-link-definition/library/types/task.md +0 -7
  85. package/fixtures/cases/declared-binary-links/library/types/note.md +0 -3
  86. package/fixtures/cases/duplicate-link-definition/library/types/task.md +0 -7
  87. package/fixtures/cases/external-invalid-model/definitions/schema/task.md +0 -5
  88. package/fixtures/cases/external-invalid-model/expected.json +0 -1
  89. package/fixtures/cases/external-invalid-model/library/note.md +0 -1
  90. package/fixtures/cases/external-unknown-type/definitions/schema/task.md +0 -5
  91. package/fixtures/cases/external-unknown-type/expected.json +0 -1
  92. package/fixtures/cases/external-valid/context.json +0 -1
  93. package/fixtures/cases/external-valid/definitions/types/task.md +0 -5
  94. package/fixtures/cases/fenced-link-definition/library/types/task.md +0 -7
  95. package/fixtures/cases/field-enum/library/types/task.md +0 -8
  96. package/fixtures/cases/interrupted-link-definition/library/types/note.md +0 -3
  97. package/fixtures/cases/long-fence-link-definition/library/types/note.md +0 -3
  98. package/fixtures/cases/malformed-definition/library/types/task.md +0 -5
  99. package/fixtures/cases/missing-binary-image/library/types/note.md +0 -3
  100. package/fixtures/cases/missing-binary-link/library/types/note.md +0 -3
  101. package/fixtures/cases/missing-required-field/library/types/task.md +0 -8
  102. package/fixtures/cases/missing-section-item-kind/library/types/task.md +0 -7
  103. package/fixtures/cases/paragraph-link-definition/library/types/note.md +0 -3
  104. package/fixtures/cases/prd-definition/library/types/task.md +0 -16
  105. package/fixtures/cases/prototype-field/library/types/task.md +0 -7
  106. package/fixtures/cases/quoted-link-definition/library/types/note.md +0 -3
  107. package/fixtures/cases/referenced-missing-file/library/types/person.md +0 -5
  108. package/fixtures/cases/referenced-missing-file/library/types/task.md +0 -8
  109. package/fixtures/cases/structured-section/library/types/task.md +0 -8
  110. package/fixtures/cases/submission-keys/library/types/note.md +0 -5
  111. package/fixtures/cases/truncated-link-definition/library/types/note.md +0 -3
  112. package/fixtures/cases/undeclared-field/library/types/task.md +0 -7
  113. package/fixtures/cases/unresolved-link/library/types/task.md +0 -7
  114. package/fixtures/cases/valid-typed-document/library/types/person.md +0 -5
  115. package/fixtures/cases/valid-typed-document/library/types/task.md +0 -13
  116. package/fixtures/cases/wide-item-fence-link/library/types/note.md +0 -3
  117. package/markdown-syntax-profile.md +0 -9
  118. /package/fixtures/cases/{prd-definition → frontmatter-schema}/context.json +0 -0
  119. /package/fixtures/cases/{prd-definition → member-document}/expected.json +0 -0
  120. /package/fixtures/cases/{external-invalid-model → types-folder-content}/context.json +0 -0
  121. /package/fixtures/cases/{external-valid → types-folder-content}/expected.json +0 -0
  122. /package/fixtures/cases/{external-valid → types-folder-content}/library/types/task.md +0 -0
  123. /package/fixtures/cases/{external-unknown-type → unknown-type}/context.json +0 -0
  124. /package/fixtures/cases/{external-unknown-type → unknown-type}/library/item.md +0 -0
package/API.md ADDED
@@ -0,0 +1,296 @@
1
+ # `@agentshouse/mdmodel` API reference
2
+
3
+ ```ts
4
+ import {
5
+ apply,
6
+ divide,
7
+ links,
8
+ MARKDOWN_SYNTAX_PROFILE,
9
+ parse,
10
+ project,
11
+ retarget,
12
+ serialize,
13
+ target,
14
+ validate,
15
+ verify,
16
+ VERSION,
17
+ } from '@agentshouse/mdmodel';
18
+
19
+ validate({ 'note.md': '# Hello\n' });
20
+ const checked = verify('types:\n task:\n fields:\n title: { kind: string }\n');
21
+ if (checked.verdict === 'accept') {
22
+ validate(
23
+ { 'tasks/one.md': '---\ntype: task\ntitle: Ship\n---\n![Figure](assets/figure.png)\n' },
24
+ { binaryTargets: ['assets/figure.png'] },
25
+ checked.model,
26
+ );
27
+ const task = checked.model.types.task;
28
+ const titleKind = task?.fields.title?.kind;
29
+ }
30
+ const parsed = parse(content);
31
+ if (parsed.ok) {
32
+ const next = apply(parsed.document, {
33
+ kind: 'field',
34
+ name: 'title',
35
+ value: 'World',
36
+ prior: 'Hello',
37
+ });
38
+ if (next.outcome === 'applied') serialize(next.document);
39
+ for (const link of links(parsed.document, 'tasks/two.md')) {
40
+ if (link.target !== 'data/export.csv') continue;
41
+ const moved = apply(parsed.document, {
42
+ kind: 'link',
43
+ href: link.href,
44
+ to: '../files/export.csv',
45
+ });
46
+ if (moved.outcome === 'applied') serialize(moved.document);
47
+ }
48
+ }
49
+ ```
50
+
51
+ `verify(schema)` reads one schema text, the MD Types of one Room or App, and
52
+ returns a verified model whose `types` expose each MD Type's `name`,
53
+ `memberDocument` flag, parsed field specs, and declared sections. The text is a
54
+ YAML mapping in the frontmatter subset whose one key, `types`, maps each MD Type
55
+ name to an entry of optional `fields`, `sections`, and boolean `member_document`:
56
+
57
+ ```yaml
58
+ types:
59
+ plan:
60
+ fields:
61
+ title: { kind: string, required: true }
62
+ status: { kind: enum, values: [active, done] }
63
+ owner: { kind: reference, to: contact }
64
+ sections:
65
+ Goals: { kind: text }
66
+ contact:
67
+ member_document: true
68
+ fields:
69
+ name: { kind: string, required: true }
70
+ ```
71
+
72
+ A field spec names its `kind` and `required` flag; a list carries `of`, an enum
73
+ carries `values`, and a reference carries `to`, which names `any` or a type of
74
+ the same schema. At most one type is marked `member_document: true`. A
75
+ malformed schema is refused whole, naming its `rule`, `VERSION`, and, where the
76
+ rule concerns one part of the text, its `location`: a dotted path such as
77
+ `types.plan.fields.status`, or `line <n>` where the text leaves the YAML subset.
78
+
79
+ `validate(library, context?, model?)` takes the resulting text Library and one
80
+ `ValidationContext` containing `binaryTargets: readonly string[]`. Omitting the
81
+ context declares no binary targets. Library keys and binary targets use the
82
+ same collection-relative path namespace; callers supply canonical paths, and
83
+ a path declared as both text and binary is refused. Ordinary links and images
84
+ in typed Documents may resolve to either membership, including relative
85
+ normalization from the source Document. A binary target supplies no Document
86
+ or MD Type and cannot satisfy a typed reference, including one to `any`.
87
+ The existing `any` Document rules are unchanged.
88
+ `model` is the verified schema that types the Library, and every Library member
89
+ is content, whatever its Path. Without `model` the Library declares no type.
90
+
91
+ A typed Document carries the frontmatter fields its MD type declares, plus
92
+ `type`, `site`, and `received`; a Document of the type marked
93
+ `member_document` also carries `member`, a string, unless that type declares
94
+ it. Any other field is refused as `undeclared-field` at that field name. A field
95
+ name YAML carries to a record prototype, `__proto__` among them, is an ordinary
96
+ undeclared field and is refused as one. A declared list section is validated as
97
+ a list; its declared item kind governs projection eligibility rather than the
98
+ value of each item, which validation reads no further than that list form.
99
+ Validation returns acceptance or one refusal naming the file, the field or
100
+ section, the violated rule, and `VERSION`. `parse`, `apply`, and `serialize` operate on a single text
101
+ Document; they do not validate collection membership or consume binary bytes.
102
+ `apply` sets one typed field, including a list field as a whole, replaces
103
+ one existing section body or the preamble against that value's prior content,
104
+ or rewrites one Markdown link destination. A section need not be declared by
105
+ the Document's MD Type to be replaced. A field change does not reformat the rest of the file.
106
+
107
+ `Document.preamble` is a readonly string containing the exact source after
108
+ frontmatter and before the first section heading recognized by the existing
109
+ section parser. Without a section it is the entire post-frontmatter body;
110
+ without frontmatter it starts at the beginning of the Document. An empty span
111
+ is `''`. It includes whitespace, newlines, a leading H1, and any fenced text
112
+ before the first section. The closing frontmatter delimiter and its line ending
113
+ belong to frontmatter. Section recognition and the syntax profile are unchanged.
114
+
115
+ ```ts
116
+ const parsed = parse(content);
117
+ if (parsed.ok) {
118
+ const change = {
119
+ kind: 'preamble' as const,
120
+ prior: parsed.document.preamble,
121
+ value: 'Updated introduction.\n\n',
122
+ };
123
+ const result = apply(parsed.document, change);
124
+ if (result.outcome === 'applied') serialize(result.document);
125
+ }
126
+ ```
127
+
128
+ `apply(document, { kind: 'preamble', prior, value })` compares `prior` to the
129
+ current preamble exactly. A mismatch returns `{ outcome: 'stale', document }`
130
+ with the unchanged current Document. A match replaces only that source span,
131
+ then parses a new immutable Document; the original is unchanged. Frontmatter
132
+ and section source outside the span retain their exact bytes, including newline
133
+ and heading syntax. As with section replacement, `value` is literal source:
134
+ no newline or separator is inserted, and the resulting source is parsed again.
135
+ An invalid frontmatter result receives the existing parse refusal. Independent
136
+ field, section, and preamble edits keep their own prior values valid.
137
+
138
+ For a moved link, use `links` and the `link` change below to obtain the rewritten
139
+ Document, then compare its public `preamble` and `sections` to the original.
140
+ Submit the changed values with their original priors. Consumers need no source
141
+ span calculation or private Model import. Collection validation remains a
142
+ separate call against the resulting Library.
143
+
144
+ `links(document, file)` is the inventory of that Document's ordinary link and
145
+ image destinations that resolve to a collection Path, each with the destination
146
+ `href` exactly as written and the `target` Path it resolves to relative to
147
+ `file`. A `?query` or `#fragment` the destination carries is part of `href` and
148
+ not part of `target`. Entries are ordered by the position of the destination
149
+ text a rewrite would change, and one destination written the same way appears
150
+ once however many times it occurs. A destination that the syntax profile does not read as a
151
+ link, one that is empty, a bare fragment, or carries a URL scheme, and one that
152
+ leaves the collection are not in the inventory. Frontmatter field values,
153
+ including typed references, are not link destinations and are never enumerated.
154
+
155
+ `target(file, href)` is the collection Path one written destination resolves to
156
+ relative to `file`, by the rule `links` and `validate` read: surrounding spaces
157
+ and the `?query` or `#fragment` it carries are not part of that Path. A
158
+ destination that is empty, a bare fragment, carries a URL scheme, starts at `/`,
159
+ or leaves the collection resolves to `null`. It reads a destination written
160
+ anywhere, such as a typed reference field value, which `links` never
161
+ enumerates. `retarget(href, to)` is that destination rewritten to `to`, keeping
162
+ the `?query` or `#fragment` it carried, which is the destination the `link`
163
+ change below writes.
164
+
165
+ ```ts
166
+ target('items/six.md', ' ../projects/atlas.md#overview '); // 'projects/atlas.md'
167
+ retarget(' ../projects/atlas.md#overview ', '../archive/atlas.md'); // '../archive/atlas.md#overview'
168
+ ```
169
+
170
+ `apply(document, { kind: 'link', href, to })` rewrites every body occurrence of
171
+ that written destination and changes nothing else, so a caller that moves a Path
172
+ rewrites its referring Documents without a second Markdown parser. `to` is the
173
+ Path the destination must resolve to instead; the `?query` or `#fragment` the
174
+ written destination carried is not part of that Path and is kept. A Document
175
+ that does not carry the destination is `stale`. A reference definition is
176
+ rewritten once however many uses name it. The destination is
177
+ written in the form the syntax profile reads back: bare when it carries no
178
+ whitespace, does not open with `<`, and balances its parentheses, otherwise
179
+ inside angle brackets, which the original form is kept in when it still fits. A
180
+ destination that neither form can carry is refused as `unwritable-link`.
181
+
182
+ `project(document, file, context?)` is the Embedding projection input of one
183
+ Document: the prose a consumer embeds, with every internal Markdown-document
184
+ link reduced to the label a reader sees. A Document that declares no MD type
185
+ contributes its body after the frontmatter fence. A Document that declares one
186
+ contributes its free-text field values, its preamble, and every H2 section its
187
+ MD type does not declare as structure, each section with its own heading line
188
+ and every occurrence of a repeated name, in the order the Document carries
189
+ them. A declared free-text section, a declared list section whose items are
190
+ free text, and a section the MD type does not declare at all are all inside
191
+ that input; a declared list section of any other item kind is not, so
192
+ eligibility follows the declared kind rather than the form the author wrote.
193
+ Each free-text field value is projected bare, without its field name and
194
+ followed by a blank line, in the order the MD Type declares its fields, and
195
+ every item of a free-text list field is one such value, projected as the author
196
+ wrote it: the link inventory reads the body, so link syntax inside a
197
+ frontmatter value is text like any other. The Document's Path and its typed
198
+ fields of every other kind stay outside that input.
199
+
200
+ ```ts
201
+ const parsed = parse(content);
202
+ if (parsed.ok) {
203
+ const projected = project(parsed.document, 'tasks/two.md', {
204
+ documentTargets: ['plans/q3.md', 'people/ana.md'],
205
+ definition: model.types.task,
206
+ });
207
+ if (projected.ok) embed(projected.text);
208
+ }
209
+ ```
210
+
211
+ `ProjectionContext` carries `documentTargets`, the collection-relative Paths the
212
+ caller holds as Markdown documents, and `definition`, the verified MD Type the
213
+ Document's `type` names, taken from the model `verify` returned. A Document that
214
+ declares a type the caller supplies no definition for is refused as
215
+ `unknown-type`. `documentTargets` is what makes a destination internal: a
216
+ destination resolving to a supplied Path is internal, and every other
217
+ destination stays authored text, a retained binary target and a Path nobody
218
+ supplied alike. The caller supplies that membership, as it does for `validate`;
219
+ the model reads no bytes, resolves no authority, and classifies nothing it was
220
+ not given.
221
+
222
+ An internal link contributes its visible label and no destination, fragment, or
223
+ title, in the inline `[label](path)`, full `[label][id]`, and collapsed
224
+ `[label][]` forms, and the link reference definition an enumerated use resolves
225
+ through leaves the projection with it. An image at a supplied document target
226
+ contributes its alt text the same way, because a move rewrites that destination
227
+ too. External URLs, retained binary targets, unsupplied destinations, bare
228
+ fragments, code spans, fenced code, and escaped syntax stay exactly as authored,
229
+ read through the same syntax profile `links` and `validate` read. `project`
230
+ returns text and never rewrites the Document.
231
+
232
+ Because a moved Path changes destinations alone, applying a `link` change leaves
233
+ the projection of every Document it rewrites byte-identical, while a changed
234
+ visible label changes it.
235
+
236
+ `divide(document, file, context)` divides that same Embedding projection input
237
+ into ordered Spans under the budget the caller supplies. Each Span carries the
238
+ `text` to embed and the `start` and `end` of the extent it addresses in the
239
+ Document's own source, as UTF-8 byte offsets into that source and never string
240
+ positions, so a caller stores the extent and hydrates the text it shows from
241
+ the Document itself.
242
+
243
+ ```ts
244
+ const parsed = parse(content);
245
+ if (parsed.ok) {
246
+ const divided = divide(parsed.document, 'tasks/two.md', {
247
+ documentTargets: ['plans/q3.md', 'people/ana.md'],
248
+ definition: model.types.task,
249
+ budget: 2000,
250
+ });
251
+ if (divided.ok) for (const span of divided.spans) embed(span.text);
252
+ }
253
+ ```
254
+
255
+ `DivisionContext` is a `ProjectionContext` and one `budget`, the greatest number
256
+ of characters a Span's embedded text may carry, counted the way JavaScript
257
+ counts a string's length and not in the bytes the extent is measured in. No
258
+ Span's text is ever longer than the budget. A bound cut never divides a
259
+ character: where the budget ends between the two halves of a surrogate pair,
260
+ the cut falls before the pair. A cut never falls inside link syntax either: it
261
+ falls before an internal link, and a label longer than a whole piece's room is
262
+ divided at the bound like any other text. The smallest budget is `2`, the
263
+ length of one astral character; a budget that is not a whole number of at least
264
+ `2` is refused with `division-budget` before any division. The budget is the
265
+ caller's number alone: the model holds no tokenizer, counts nothing else, and
266
+ refuses no input for being too long. A Document the caller supplies no
267
+ definition for carries the same `unknown-type` refusal `project` returns.
268
+
269
+ The division descends one level at a time, and only into the piece that does not
270
+ fit: the whole input, a heading node, adjacent sibling nodes packed under one
271
+ parent, adjacent blocks inside a node, sentences inside a block, and a bound cut
272
+ inside a single sentence. A Span covers whole heading nodes or one contiguous
273
+ piece inside a single node, never a partial node beside another. A Span that
274
+ continues a node repeats that node's own heading line, projected like any other
275
+ text and so carrying an internal link's label rather than its destination, and
276
+ never an ancestor's heading, so renaming a heading costs what editing that
277
+ heading's own body costs. A heading the budget cannot hold beside any text is
278
+ not repeated, a continuation piece that begins with an astral character the
279
+ room beside its heading cannot hold is carried without the heading, and a
280
+ heading with no body of its own leads its first child node rather than standing
281
+ as a Span of nothing but itself. Overlap exists at the
282
+ sentence level alone, where each Span after the first repeats its predecessor's
283
+ last sentence whenever the budget holds both; when it cannot hold both, the
284
+ Span carries its own sentence alone. An untyped input arrives without a
285
+ definition and descends the same ladder.
286
+
287
+ Division is total: every authored character of the input belongs to at least one
288
+ Span, and an input carrying no authored text divides into no Span at all. A
289
+ Span's extent is the smallest range of the source that contains everything the
290
+ Span covers, so it spans what the input excludes between two eligible pieces,
291
+ and the extent addresses the authored source where the embedded text carries a
292
+ repeated heading, an internal link's visible label, or a frontmatter value the
293
+ author quoted. A Span divided out of one free-text field value addresses that
294
+ whole value, because a YAML scalar's own positions are the only ones its source
295
+ has, and a Span carrying part of one internal link's label addresses that whole
296
+ link, because a cut never falls inside link syntax.
package/README.md CHANGED
@@ -1,14 +1,12 @@
1
1
  # `@agentshouse/mdmodel`
2
2
 
3
- The standalone MD Model: the MD Type language, its parser, its validator, the
4
- deterministic application of one structured change to Document content, the
5
- Markdown syntax profile, and the conformance fixtures.
3
+ Typed Markdown documents: the MD Type language, a parser, a validator, and the
4
+ deterministic application of one structured change to a Document, plus a
5
+ conformance fixture corpus.
6
6
 
7
- The package runs in a browser and in Node. It makes no network call and reads
8
- no filesystem on its own. Callers pass text Library state as path-to-content and declare retained binary
9
- link targets separately in a `ValidationContext`.
10
- The public root exposes one ESM artifact. Node 24.21.0 and later can load it
11
- through either `import` or `require`; browser bundlers consume the same artifact.
7
+ It runs in the browser and in Node, makes no network call, and reads no
8
+ filesystem on its own. It ships one ESM artifact that Node 24.21.0 and later
9
+ loads through `import` or `require`.
12
10
 
13
11
  ## Install
14
12
 
@@ -16,44 +14,17 @@ through either `import` or `require`; browser bundlers consume the same artifact
16
14
  npm install @agentshouse/mdmodel
17
15
  ```
18
16
 
19
- Package releases follow the repository release handoff after merge.
20
-
21
- ## API
17
+ ## Usage
22
18
 
23
19
  ```ts
24
- import {
25
- apply,
26
- divide,
27
- links,
28
- MARKDOWN_SYNTAX_PROFILE,
29
- parse,
30
- project,
31
- retarget,
32
- serialize,
33
- target,
34
- validate,
35
- verify,
36
- verifyDocuments,
37
- VERSION,
38
- } from '@agentshouse/mdmodel';
20
+ import { apply, parse, serialize, validate, verify } from '@agentshouse/mdmodel';
39
21
 
40
- validate({ 'note.md': '# Hello\n' });
41
- validate(
42
- {
43
- 'types/note.md': '---\ntype: definition\n---\n',
44
- 'note.md': '---\ntype: note\n---\n![Figure](assets/figure.png)\n',
45
- },
46
- { binaryTargets: ['assets/figure.png'] },
47
- );
48
- const checked = verify({
49
- 'model/task.md': '---\ntype: definition\nfields:\n title: { kind: string }\n---\n',
50
- });
22
+ const checked = verify('types:\n task:\n fields:\n title: { kind: string }\n');
51
23
  if (checked.verdict === 'accept') {
52
24
  validate({ 'tasks/one.md': '---\ntype: task\ntitle: Ship\n---\n' }, undefined, checked.model);
53
- const task = checked.model.types.task;
54
- const titleKind = task?.fields.title?.kind;
55
25
  }
56
- const parsed = parse(content);
26
+
27
+ const parsed = parse('---\ntype: note\ntitle: Hello\n---\n');
57
28
  if (parsed.ok) {
58
29
  const next = apply(parsed.document, {
59
30
  kind: 'field',
@@ -62,257 +33,24 @@ if (parsed.ok) {
62
33
  prior: 'Hello',
63
34
  });
64
35
  if (next.outcome === 'applied') serialize(next.document);
65
- for (const link of links(parsed.document, 'tasks/two.md')) {
66
- if (link.target !== 'data/export.csv') continue;
67
- const moved = apply(parsed.document, {
68
- kind: 'link',
69
- href: link.href,
70
- to: '../files/export.csv',
71
- });
72
- if (moved.outcome === 'applied') serialize(moved.document);
73
- }
74
- }
75
- ```
76
-
77
- `verify(definitions)` accepts definition text keyed by canonical definition
78
- address. It returns a verified model whose `types` expose each MD Type's name,
79
- address, parsed field specs, and declared sections. A field spec names its
80
- `kind` and `required` flag; a list carries `of`, an enum carries `values`, and a
81
- reference carries `to`. `verifyDocuments(definitions)` accepts already parsed
82
- definition Documents keyed by canonical definition address and yields the same
83
- verified facts without parsing their content again. A verification refusal names
84
- `source: 'model'` and the definition address.
85
-
86
- `validate(library, context?, model?)` takes the resulting text Library and one
87
- `ValidationContext` containing `binaryTargets: readonly string[]`. Omitting the
88
- context declares no binary targets. Library keys and binary targets use the
89
- same collection-relative path namespace; callers supply canonical paths, and
90
- a path declared as both text and binary is refused. Ordinary links and images
91
- in typed Documents may resolve to either membership, including relative
92
- normalization from the source Document. A binary target supplies no Document
93
- or MD Type and cannot satisfy a typed reference, including one to `any`.
94
- The existing `any` Document rules are unchanged.
95
- When `model` is supplied, validation uses only its definitions and treats every
96
- Library member as content, even when its Path equals a definition address. A
97
- content refusal then names `source: 'content'` and its Library Path. Without
98
- `model`, definition Documents under `types/` are read from the Library.
99
-
100
- A typed Document carries the frontmatter fields its MD type declares, plus
101
- `type`, `site`, and `received`; any other field is refused as
102
- `undeclared-field` at that field name. A field
103
- name YAML carries to a record prototype, `__proto__` among them, is an ordinary
104
- undeclared field and is refused as one. A declared list section is validated as
105
- a list; its declared item kind governs projection eligibility rather than the
106
- value of each item, which validation reads no further than that list form.
107
- Validation returns acceptance or one refusal naming the file, the field or
108
- section, the violated rule, and `VERSION`. Every caller operation that validates
109
- a resulting collection supplies that collection's context to this same
110
- `validate` function. `parse`, `apply`, and `serialize` operate on a single text
111
- Document; they do not validate collection membership or consume binary bytes.
112
- `apply` sets one typed field, including a list field as a whole, replaces
113
- one existing section body or the preamble against that value's prior content,
114
- or rewrites one Markdown link destination. A section need not be declared by
115
- the Document's MD Type to be replaced. A field change does not reformat the rest of the file.
116
-
117
- `Document.preamble` is a readonly string containing the exact source after
118
- frontmatter and before the first section heading recognized by the existing
119
- section parser. Without a section it is the entire post-frontmatter body;
120
- without frontmatter it starts at the beginning of the Document. An empty span
121
- is `''`. It includes whitespace, newlines, a leading H1, and any fenced text
122
- before the first section. The closing frontmatter delimiter and its line ending
123
- belong to frontmatter. Section recognition and the syntax profile are unchanged.
124
-
125
- ```ts
126
- const parsed = parse(content);
127
- if (parsed.ok) {
128
- const change = {
129
- kind: 'preamble' as const,
130
- prior: parsed.document.preamble,
131
- value: 'Updated introduction.\n\n',
132
- };
133
- const result = apply(parsed.document, change);
134
- if (result.outcome === 'applied') serialize(result.document);
135
- }
136
- ```
137
-
138
- `apply(document, { kind: 'preamble', prior, value })` compares `prior` to the
139
- current preamble exactly. A mismatch returns `{ outcome: 'stale', document }`
140
- with the unchanged current Document. A match replaces only that source span,
141
- then parses a new immutable Document; the original is unchanged. Frontmatter
142
- and section source outside the span retain their exact bytes, including newline
143
- and heading syntax. As with section replacement, `value` is literal source:
144
- no newline or separator is inserted, and the resulting source is parsed again.
145
- An invalid frontmatter result receives the existing parse refusal. Independent
146
- field, section, and preamble edits keep their own prior values valid.
147
-
148
- For a moved link, use `links` and the `link` change below to obtain the rewritten
149
- Document, then compare its public `preamble` and `sections` to the original.
150
- Submit the changed values with their original priors. Consumers need no source
151
- span calculation or private Model import. Collection validation remains a
152
- separate call against the resulting Library.
153
-
154
- `links(document, file)` is the inventory of that Document's ordinary link and
155
- image destinations that resolve to a collection Path, each with the destination
156
- `href` exactly as written and the `target` Path it resolves to relative to
157
- `file`. A `?query` or `#fragment` the destination carries is part of `href` and
158
- not part of `target`. Entries are ordered by the position of the destination
159
- text a rewrite would change, and one destination written the same way appears
160
- once however many times it occurs. A destination that the syntax profile does not read as a
161
- link, one that is empty, a bare fragment, or carries a URL scheme, and one that
162
- leaves the collection are not in the inventory. Frontmatter field values,
163
- including typed references, are not link destinations and are never enumerated.
164
-
165
- `target(file, href)` is the collection Path one written destination resolves to
166
- relative to `file`, by the rule `links` and `validate` read: surrounding spaces
167
- and the `?query` or `#fragment` it carries are not part of that Path. A
168
- destination that is empty, a bare fragment, carries a URL scheme, starts at `/`,
169
- or leaves the collection resolves to `null`. It reads a destination written
170
- anywhere, such as a typed reference field value, which `links` never
171
- enumerates. `retarget(href, to)` is that destination rewritten to `to`, keeping
172
- the `?query` or `#fragment` it carried, which is the destination the `link`
173
- change below writes.
174
-
175
- ```ts
176
- target('items/six.md', ' ../projects/atlas.md#overview '); // 'projects/atlas.md'
177
- retarget(' ../projects/atlas.md#overview ', '../archive/atlas.md'); // '../archive/atlas.md#overview'
178
- ```
179
-
180
- `apply(document, { kind: 'link', href, to })` rewrites every body occurrence of
181
- that written destination and changes nothing else, so a caller that moves a Path
182
- rewrites its referring Documents without a second Markdown parser. `to` is the
183
- Path the destination must resolve to instead; the `?query` or `#fragment` the
184
- written destination carried is not part of that Path and is kept. A Document
185
- that does not carry the destination is `stale`. A reference definition is
186
- rewritten once however many uses name it. The destination is
187
- written in the form the syntax profile reads back: bare when it carries no
188
- whitespace, does not open with `<`, and balances its parentheses, otherwise
189
- inside angle brackets, which the original form is kept in when it still fits. A
190
- destination that neither form can carry is refused as `unwritable-link`.
191
-
192
- `project(document, file, context?)` is the Embedding projection input of one
193
- Document: the prose a consumer embeds, with every internal Markdown-document
194
- link reduced to the label a reader sees. A Document that declares no MD type
195
- contributes its body after the frontmatter fence. A Document that declares one
196
- contributes its free-text field values, its preamble, and every H2 section its
197
- MD type does not declare as structure, each section with its own heading line
198
- and every occurrence of a repeated name, in the order the Document carries
199
- them. A declared free-text section, a declared list section whose items are
200
- free text, and a section the MD type does not declare at all are all inside
201
- that input; a declared list section of any other item kind is not, so
202
- eligibility follows the declared kind rather than the form the author wrote.
203
- Each free-text field value is projected bare, without its field name and
204
- followed by a blank line, in the order the MD Type declares its fields, and
205
- every item of a free-text list field is one such value, projected as the author
206
- wrote it: the link inventory reads the body, so link syntax inside a
207
- frontmatter value is text like any other. The Document's Path and its typed
208
- fields of every other kind stay outside that input.
209
-
210
- ```ts
211
- const parsed = parse(content);
212
- if (parsed.ok) {
213
- const projected = project(parsed.document, 'tasks/two.md', {
214
- documentTargets: ['plans/q3.md', 'people/ana.md'],
215
- definition: types['types/task.md'],
216
- });
217
- if (projected.ok) embed(projected.text);
218
- }
219
- ```
220
-
221
- `ProjectionContext` carries `documentTargets`, the collection-relative Paths the
222
- caller holds as Markdown documents, and `definition`, the source of the MD Type
223
- definition Document the Document's `type` names. A Document that declares a type
224
- the caller supplies no definition for is refused as `unknown-type`, and a
225
- definition source the model cannot read carries its own refusal naming
226
- `types/<type>.md`. `documentTargets` is what makes a destination internal: a
227
- destination resolving to a supplied Path is internal, and every other
228
- destination stays authored text, a retained binary target and a Path nobody
229
- supplied alike. The caller supplies that membership, as it does for `validate`;
230
- the model reads no bytes, resolves no authority, and classifies nothing it was
231
- not given.
232
-
233
- An internal link contributes its visible label and no destination, fragment, or
234
- title, in the inline `[label](path)`, full `[label][id]`, and collapsed
235
- `[label][]` forms, and the link reference definition an enumerated use resolves
236
- through leaves the projection with it. An image at a supplied document target
237
- contributes its alt text the same way, because a move rewrites that destination
238
- too. External URLs, retained binary targets, unsupplied destinations, bare
239
- fragments, code spans, fenced code, and escaped syntax stay exactly as authored,
240
- read through the same syntax profile `links` and `validate` read. `project`
241
- returns text and never rewrites the Document.
242
-
243
- Because a moved Path changes destinations alone, applying a `link` change leaves
244
- the projection of every Document it rewrites byte-identical, while a changed
245
- visible label changes it. `fixtures/projection.json` carries every move row of
246
- that corpus; the label change is proven at this package's own `project` seam.
247
-
248
- `divide(document, file, context)` divides that same Embedding projection input
249
- into ordered Spans under the budget the caller supplies. Each Span carries the
250
- `text` to embed and the `start` and `end` of the extent it addresses in the
251
- Document's own source, as UTF-8 byte offsets into that source and never string
252
- positions, so a caller stores the extent and hydrates the text it shows from
253
- the Document itself.
254
-
255
- ```ts
256
- const parsed = parse(content);
257
- if (parsed.ok) {
258
- const divided = divide(parsed.document, 'tasks/two.md', {
259
- documentTargets: ['plans/q3.md', 'people/ana.md'],
260
- definition: types['types/task.md'],
261
- budget: 2000,
262
- });
263
- if (divided.ok) for (const span of divided.spans) embed(span.text);
264
36
  }
265
37
  ```
266
38
 
267
- `DivisionContext` is a `ProjectionContext` and one `budget`, the greatest number
268
- of characters a Span's embedded text may carry, counted the way JavaScript
269
- counts a string's length and not in the bytes the extent is measured in. No
270
- Span's text is ever longer than the budget. A bound cut never divides a
271
- character: where the budget ends between the two halves of a surrogate pair,
272
- the cut falls before the pair. A cut never falls inside link syntax either: it
273
- falls before an internal link, and a label longer than a whole piece's room is
274
- divided at the bound like any other text. The smallest budget is `2`, the
275
- length of one astral character; a budget that is not a whole number of at least
276
- `2` is refused with `division-budget` before any division. The budget is the
277
- caller's number alone: the model holds no tokenizer, counts nothing else, and
278
- refuses no input for being too long. A Document the caller supplies no
279
- definition for carries the same `unknown-type` refusal `project` returns.
280
-
281
- The division descends one level at a time, and only into the piece that does not
282
- fit: the whole input, a heading node, adjacent sibling nodes packed under one
283
- parent, adjacent blocks inside a node, sentences inside a block, and a bound cut
284
- inside a single sentence. A Span covers whole heading nodes or one contiguous
285
- piece inside a single node, never a partial node beside another. A Span that
286
- continues a node repeats that node's own heading line, projected like any other
287
- text and so carrying an internal link's label rather than its destination, and
288
- never an ancestor's heading, so renaming a heading costs what editing that
289
- heading's own body costs. A heading the budget cannot hold beside any text is
290
- not repeated, a continuation piece that begins with an astral character the
291
- room beside its heading cannot hold is carried without the heading, and a
292
- heading with no body of its own leads its first child node rather than standing
293
- as a Span of nothing but itself. Overlap exists at the
294
- sentence level alone, where each Span after the first repeats its predecessor's
295
- last sentence whenever the budget holds both; when it cannot hold both, the
296
- Span carries its own sentence alone. An untyped input arrives without a
297
- definition and descends the same ladder.
298
-
299
- Division is total: every authored character of the input belongs to at least one
300
- Span, and an input carrying no authored text divides into no Span at all. A
301
- Span's extent is the smallest range of the source that contains everything the
302
- Span covers, so it spans what the input excludes between two eligible pieces,
303
- and the extent addresses the authored source where the embedded text carries a
304
- repeated heading, an internal link's visible label, or a frontmatter value the
305
- author quoted. A Span divided out of one free-text field value addresses that
306
- whole value, because a YAML scalar's own positions are the only ones its source
307
- has, and a Span carrying part of one internal link's label addresses that whole
308
- link, because a cut never falls inside link syntax.
39
+ ## API
309
40
 
310
- ## Fixtures
41
+ | Export | Purpose |
42
+ | ------------------------------------ | ---------------------------------------------------------- |
43
+ | `verify` | Verify one schema text into a model of MD Types |
44
+ | `validate` | Validate a text Library; returns acceptance or one refusal |
45
+ | `parse`, `serialize` | Read and write one Document |
46
+ | `apply` | Apply one field, section, preamble, or link change |
47
+ | `links`, `target`, `retarget` | Inventory and rewrite link destinations |
48
+ | `project`, `divide` | Embedding projection input and its division into Spans |
49
+ | `MARKDOWN_SYNTAX_PROFILE`, `VERSION` | The pinned Markdown profile (GFM 0.29) and package version |
311
50
 
312
- The conformance corpus ships in `fixtures/`. Its verdict format and rule ids
313
- are documented in [`fixtures/README.md`](fixtures/README.md).
51
+ The full reference is in [`API.md`](API.md). The conformance corpus and its
52
+ verdict format and rule ids are described in [`fixtures/README.md`](fixtures/README.md).
314
53
 
315
- ## Markdown syntax profile
54
+ ## License
316
55
 
317
- GitHub Flavored Markdown Spec 0.29, as documentation, not as validation.
318
- See [`markdown-syntax-profile.md`](markdown-syntax-profile.md).
56
+ MIT
@@ -1,4 +1,4 @@
1
- import type { Document, Refusal } from './types.js';
1
+ import type { VerifiedModel } from './types.js';
2
2
  export type TextSpec = {
3
3
  readonly kind: 'text';
4
4
  readonly required: boolean;
@@ -30,9 +30,13 @@ export type FieldSpec = ScalarSpec | ListSpec;
30
30
  export type SectionSpec = TextSpec | ListSpec;
31
31
  export type Definition = {
32
32
  readonly name: string;
33
- readonly address: string;
33
+ readonly memberDocument: boolean;
34
34
  readonly fields: Readonly<Record<string, FieldSpec>>;
35
35
  readonly sections: Readonly<Record<string, SectionSpec>>;
36
36
  };
37
- export declare function readDefinition(file: string, document: Document): Definition | Refusal;
37
+ export type Failure = {
38
+ readonly rule: string;
39
+ readonly location?: string;
40
+ };
41
+ export declare function readSchema(text: string): VerifiedModel | Failure;
38
42
  //# sourceMappingURL=definition.d.ts.map