@agentshouse/mdmodel 0.1.0-ca6c012c54fb0 → 0.1.0-caaa5bad9995d

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 (536) hide show
  1. package/API.md +246 -229
  2. package/README.md +27 -25
  3. package/dist/body.d.ts +85 -0
  4. package/dist/body.d.ts.map +1 -0
  5. package/dist/body.js +1083 -0
  6. package/dist/built-in.d.ts +4 -0
  7. package/dist/built-in.d.ts.map +1 -0
  8. package/dist/built-in.js +19 -0
  9. package/dist/definition.d.ts +25 -11
  10. package/dist/definition.d.ts.map +1 -1
  11. package/dist/definition.js +235 -136
  12. package/dist/document.d.ts +16 -9
  13. package/dist/document.d.ts.map +1 -1
  14. package/dist/document.js +786 -149
  15. package/dist/event.d.ts +77 -0
  16. package/dist/event.d.ts.map +1 -0
  17. package/dist/event.js +200 -0
  18. package/dist/facts.d.ts +15 -0
  19. package/dist/facts.d.ts.map +1 -0
  20. package/dist/facts.js +42 -0
  21. package/dist/fields.d.ts +1 -1
  22. package/dist/fields.d.ts.map +1 -1
  23. package/dist/fields.js +2 -2
  24. package/dist/form.d.ts +4 -0
  25. package/dist/form.d.ts.map +1 -0
  26. package/dist/form.js +153 -0
  27. package/dist/house-model.d.ts +912 -0
  28. package/dist/house-model.d.ts.map +1 -0
  29. package/dist/house-model.js +502 -0
  30. package/dist/house-references/crockford.d.ts +4 -0
  31. package/dist/house-references/crockford.d.ts.map +1 -0
  32. package/dist/house-references/crockford.js +3 -0
  33. package/dist/house-references/patterns.d.ts +13 -0
  34. package/dist/house-references/patterns.d.ts.map +1 -0
  35. package/dist/house-references/patterns.js +20 -0
  36. package/dist/index.d.ts +44 -18
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +27 -12
  39. package/dist/indexed.d.ts +10 -0
  40. package/dist/indexed.d.ts.map +1 -0
  41. package/dist/indexed.js +63 -0
  42. package/dist/kinds.d.ts +26 -0
  43. package/dist/kinds.d.ts.map +1 -0
  44. package/dist/kinds.js +218 -0
  45. package/dist/markdown.d.ts +71 -0
  46. package/dist/markdown.d.ts.map +1 -0
  47. package/dist/markdown.js +232 -0
  48. package/dist/media-type.d.ts +72 -0
  49. package/dist/media-type.d.ts.map +1 -0
  50. package/dist/media-type.js +157 -0
  51. package/dist/moment.d.ts +7 -0
  52. package/dist/moment.d.ts.map +1 -0
  53. package/dist/moment.js +18 -0
  54. package/dist/not-ready.d.ts +2 -0
  55. package/dist/not-ready.d.ts.map +1 -0
  56. package/dist/not-ready.js +5 -0
  57. package/dist/paths.d.ts +16 -1
  58. package/dist/paths.d.ts.map +1 -1
  59. package/dist/paths.js +107 -22
  60. package/dist/pattern.d.ts.map +1 -1
  61. package/dist/pattern.js +4 -3
  62. package/dist/query/language.d.ts +257 -0
  63. package/dist/query/language.d.ts.map +1 -0
  64. package/dist/query/language.js +621 -0
  65. package/dist/query/match.d.ts +14 -0
  66. package/dist/query/match.d.ts.map +1 -0
  67. package/dist/query/match.js +129 -0
  68. package/dist/query/rows.d.ts +36 -0
  69. package/dist/query/rows.d.ts.map +1 -0
  70. package/dist/query/rows.js +179 -0
  71. package/dist/query/sql.d.ts +45 -0
  72. package/dist/query/sql.d.ts.map +1 -0
  73. package/dist/query/sql.js +296 -0
  74. package/dist/query/walk.d.ts +69 -0
  75. package/dist/query/walk.d.ts.map +1 -0
  76. package/dist/query/walk.js +274 -0
  77. package/dist/references.d.ts +2 -2
  78. package/dist/references.d.ts.map +1 -1
  79. package/dist/references.js +1 -1
  80. package/dist/refuse.d.ts +1 -0
  81. package/dist/refuse.d.ts.map +1 -1
  82. package/dist/refuse.js +1 -0
  83. package/dist/reserved.d.ts +2 -0
  84. package/dist/reserved.d.ts.map +1 -1
  85. package/dist/reserved.js +17 -5
  86. package/dist/room-types.d.ts +8 -0
  87. package/dist/room-types.d.ts.map +1 -0
  88. package/dist/room-types.js +144 -0
  89. package/dist/schema.d.ts +1 -3
  90. package/dist/schema.d.ts.map +1 -1
  91. package/dist/schema.js +9 -73
  92. package/dist/types.d.ts +32 -43
  93. package/dist/types.d.ts.map +1 -1
  94. package/dist/validate.d.ts +14 -6
  95. package/dist/validate.d.ts.map +1 -1
  96. package/dist/validate.js +225 -187
  97. package/dist/view.d.ts +5 -0
  98. package/dist/view.d.ts.map +1 -0
  99. package/dist/view.js +27 -0
  100. package/dist/yaml.d.ts +7 -5
  101. package/dist/yaml.d.ts.map +1 -1
  102. package/dist/yaml.js +158 -44
  103. package/fixtures/README.md +103 -70
  104. package/fixtures/cases/absent-link-dangles/library/item.md +1 -1
  105. package/fixtures/cases/absent-link-dangles/schema.md +7 -0
  106. package/fixtures/cases/absent-optional-field/library/companies/acme.md +1 -1
  107. package/fixtures/cases/absent-optional-field/library/deals/lead.md +3 -3
  108. package/fixtures/cases/{date-time-format/schema.yaml → absent-optional-field/schema.md} +12 -11
  109. package/fixtures/cases/absolute-link/library/item.md +1 -1
  110. package/fixtures/cases/absolute-link/schema.md +7 -0
  111. package/fixtures/cases/attachment-reference-any-file/library/companies/acme.md +1 -1
  112. package/fixtures/cases/attachment-reference-any-file/library/deals/renewal.md +13 -10
  113. package/fixtures/cases/attachment-reference-any-file/library/places/depot.md +1 -1
  114. package/fixtures/cases/attachment-reference-any-file/library/products/seats.md +1 -1
  115. package/fixtures/cases/attachment-reference-any-file/library/products/support.md +1 -1
  116. package/fixtures/cases/attachment-reference-any-file/{schema.yaml → schema.md} +2 -0
  117. package/fixtures/cases/attachment-reference-dangles/library/companies/acme.md +1 -1
  118. package/fixtures/cases/attachment-reference-dangles/library/deals/renewal.md +13 -10
  119. package/fixtures/cases/attachment-reference-dangles/library/places/depot.md +1 -1
  120. package/fixtures/cases/attachment-reference-dangles/library/products/seats.md +1 -1
  121. package/fixtures/cases/attachment-reference-dangles/library/products/support.md +1 -1
  122. package/fixtures/cases/attachment-reference-dangles/{schema.yaml → schema.md} +2 -0
  123. package/fixtures/cases/attachment-reference-outside/expected.json +2 -1
  124. package/fixtures/cases/attachment-reference-outside/library/companies/acme.md +1 -1
  125. package/fixtures/cases/attachment-reference-outside/library/deals/renewal.md +13 -10
  126. package/fixtures/cases/attachment-reference-outside/library/places/depot.md +1 -1
  127. package/fixtures/cases/attachment-reference-outside/library/products/seats.md +1 -1
  128. package/fixtures/cases/attachment-reference-outside/library/products/support.md +1 -1
  129. package/fixtures/cases/attachment-reference-outside/{schema.yaml → schema.md} +2 -0
  130. package/fixtures/cases/binary-any-reference/expected.json +2 -1
  131. package/fixtures/cases/binary-any-reference/library/notes/draft.md +1 -1
  132. package/fixtures/cases/binary-any-reference/{schema.yaml → schema.md} +2 -0
  133. package/fixtures/cases/binary-document-collision/{schema.yaml → schema.md} +2 -0
  134. package/fixtures/cases/binary-image-dangles/{schema.yaml → schema.md} +2 -0
  135. package/fixtures/cases/binary-link-dangles/{schema.yaml → schema.md} +2 -0
  136. package/fixtures/cases/binary-link-outside-collection/{schema.yaml → schema.md} +2 -0
  137. package/fixtures/cases/binary-prototype-reference/expected.json +2 -1
  138. package/fixtures/cases/binary-prototype-reference/library/notes/draft.md +1 -1
  139. package/fixtures/cases/binary-prototype-reference/{schema.yaml → schema.md} +2 -0
  140. package/fixtures/cases/binary-typed-reference/expected.json +2 -1
  141. package/fixtures/cases/binary-typed-reference/library/notes/draft.md +1 -1
  142. package/fixtures/cases/binary-typed-reference/{schema.yaml → schema.md} +2 -0
  143. package/fixtures/cases/block-scalar-frontmatter/library/notes/literal.md +17 -0
  144. package/fixtures/cases/block-scalar-frontmatter/library/skills/notes/SKILL.md +10 -0
  145. package/fixtures/cases/boolean-field/expected.json +2 -1
  146. package/fixtures/cases/boolean-field/library/companies/acme.md +1 -1
  147. package/fixtures/cases/boolean-field/library/deals/renewal.md +49 -24
  148. package/fixtures/cases/{absent-optional-field/schema.yaml → boolean-field/schema.md} +12 -11
  149. package/fixtures/cases/bracketed-link-definition/library/item.md +1 -1
  150. package/fixtures/cases/bracketed-link-definition/schema.md +7 -0
  151. package/fixtures/cases/closed-nested-object/expected.json +2 -1
  152. package/fixtures/cases/closed-nested-object/library/companies/acme.md +1 -1
  153. package/fixtures/cases/closed-nested-object/library/deals/renewal.md +50 -25
  154. package/fixtures/cases/closed-nested-object/{schema.yaml → schema.md} +12 -11
  155. package/fixtures/cases/composed-root-reference/library/a.md +2 -2
  156. package/fixtures/cases/composed-root-reference/{schema.yaml → schema.md} +2 -0
  157. package/fixtures/cases/{conditional-section → conditional-long-text}/expected.json +3 -2
  158. package/fixtures/cases/{conditional-section → conditional-long-text}/library/questions/hosting.md +2 -2
  159. package/fixtures/cases/{conditional-section → conditional-long-text}/library/questions/payments.md +2 -2
  160. package/fixtures/cases/conditional-long-text/schema.md +15 -0
  161. package/fixtures/cases/cross-room-link/library/clients/acme.md +1 -1
  162. package/fixtures/cases/cross-room-link/{schema.yaml → schema.md} +2 -0
  163. package/fixtures/cases/cross-room-reference/library/clients/acme.md +4 -4
  164. package/fixtures/cases/cross-room-reference/{schema.yaml → schema.md} +3 -2
  165. package/fixtures/cases/date-time-format/expected.json +2 -1
  166. package/fixtures/cases/date-time-format/library/companies/acme.md +1 -1
  167. package/fixtures/cases/date-time-format/library/deals/renewal.md +49 -24
  168. package/fixtures/cases/{boolean-field/schema.yaml → date-time-format/schema.md} +12 -11
  169. package/fixtures/cases/declared-binary-links/schema.md +3 -0
  170. package/fixtures/cases/default-unfilled/library/companies/acme.md +1 -1
  171. package/fixtures/cases/default-unfilled/library/deals/renewal.md +47 -22
  172. package/fixtures/cases/default-unfilled/schema.md +57 -0
  173. package/fixtures/cases/defs-pattern/expected.json +2 -1
  174. package/fixtures/cases/defs-pattern/library/companies/acme.md +1 -1
  175. package/fixtures/cases/defs-pattern/library/deals/renewal.md +49 -24
  176. package/fixtures/cases/defs-pattern/schema.md +57 -0
  177. package/fixtures/cases/dependent-rule/library/companies/acme.md +1 -1
  178. package/fixtures/cases/dependent-rule/library/deals/renewal.md +48 -23
  179. package/fixtures/cases/dependent-rule/schema.md +57 -0
  180. package/fixtures/cases/duplicate-link-definition/library/item.md +1 -1
  181. package/fixtures/cases/duplicate-link-definition/schema.md +7 -0
  182. package/fixtures/cases/email-format/expected.json +2 -1
  183. package/fixtures/cases/email-format/library/companies/acme.md +1 -1
  184. package/fixtures/cases/email-format/library/deals/renewal.md +49 -24
  185. package/fixtures/cases/email-format/schema.md +57 -0
  186. package/fixtures/cases/equal-labels/expected.json +5 -0
  187. package/fixtures/cases/equal-labels/schema.md +6 -0
  188. package/fixtures/cases/fenced-link-definition/library/item.md +1 -1
  189. package/fixtures/cases/fenced-link-definition/schema.md +7 -0
  190. package/fixtures/cases/field-enum/expected.json +2 -1
  191. package/fixtures/cases/field-enum/library/item.md +2 -2
  192. package/fixtures/cases/field-enum/schema.md +8 -0
  193. package/fixtures/cases/frontmatter-construct/expected.json +7 -0
  194. package/fixtures/cases/frontmatter-construct/library/note.md +6 -0
  195. package/fixtures/cases/integer-field/expected.json +2 -1
  196. package/fixtures/cases/integer-field/library/companies/acme.md +1 -1
  197. package/fixtures/cases/integer-field/library/deals/renewal.md +49 -24
  198. package/fixtures/cases/integer-field/schema.md +57 -0
  199. package/fixtures/cases/interrupted-link-definition/schema.md +3 -0
  200. package/fixtures/cases/json-schema-constructs/library/companies/acme.md +1 -1
  201. package/fixtures/cases/json-schema-constructs/library/deals/renewal.md +49 -24
  202. package/fixtures/cases/json-schema-constructs/schema.md +57 -0
  203. package/fixtures/cases/labels-in-the-rooms-language/expected.json +1 -0
  204. package/fixtures/cases/labels-in-the-rooms-language/library/deals/atlas.md +7 -0
  205. package/fixtures/cases/labels-in-the-rooms-language/schema.md +11 -0
  206. package/fixtures/cases/lazy-list-item/expected.json +3 -0
  207. package/fixtures/cases/lazy-list-item/library/item.md +10 -0
  208. package/fixtures/cases/lazy-list-item/schema.md +5 -0
  209. package/fixtures/cases/link-into-a-reserved-name/library/notes/a.md +1 -1
  210. package/fixtures/cases/link-into-a-reserved-name/{schema.yaml → schema.md} +2 -0
  211. package/fixtures/cases/list-item-enum/expected.json +7 -0
  212. package/fixtures/cases/list-item-enum/library/task.md +7 -0
  213. package/fixtures/cases/list-item-enum/schema.md +5 -0
  214. package/fixtures/cases/list-item-kind/expected.json +7 -0
  215. package/fixtures/cases/list-item-kind/library/task.md +6 -0
  216. package/fixtures/cases/list-item-kind/schema.md +5 -0
  217. package/fixtures/cases/list-items/expected.json +1 -0
  218. package/fixtures/cases/list-items/library/page.md +26 -0
  219. package/fixtures/cases/list-items/schema.md +12 -0
  220. package/fixtures/cases/list-of-objects-closed/expected.json +2 -1
  221. package/fixtures/cases/list-of-objects-closed/library/companies/acme.md +1 -1
  222. package/fixtures/cases/list-of-objects-closed/library/deals/renewal.md +49 -25
  223. package/fixtures/cases/list-of-objects-closed/schema.md +57 -0
  224. package/fixtures/cases/list-of-objects-item/expected.json +2 -1
  225. package/fixtures/cases/list-of-objects-item/library/companies/acme.md +1 -1
  226. package/fixtures/cases/list-of-objects-item/library/deals/renewal.md +49 -24
  227. package/fixtures/cases/list-of-objects-item/schema.md +57 -0
  228. package/fixtures/cases/list-of-objects-reference/expected.json +2 -1
  229. package/fixtures/cases/list-of-objects-reference/library/companies/acme.md +1 -1
  230. package/fixtures/cases/list-of-objects-reference/library/deals/renewal.md +12 -9
  231. package/fixtures/cases/list-of-objects-reference/library/places/depot.md +1 -1
  232. package/fixtures/cases/list-of-objects-reference/library/products/seats.md +1 -1
  233. package/fixtures/cases/list-of-objects-reference/library/products/support.md +1 -1
  234. package/fixtures/cases/list-of-objects-reference/{schema.yaml → schema.md} +2 -0
  235. package/fixtures/cases/list-reference-path/expected.json +7 -0
  236. package/fixtures/cases/list-reference-path/library/page.md +5 -0
  237. package/fixtures/cases/list-reference-path/schema.md +5 -0
  238. package/fixtures/cases/list-reference-type/expected.json +7 -0
  239. package/fixtures/cases/list-reference-type/library/page.md +6 -0
  240. package/fixtures/cases/list-reference-type/schema.md +5 -0
  241. package/fixtures/cases/list-size/expected.json +2 -1
  242. package/fixtures/cases/list-size/library/companies/acme.md +1 -1
  243. package/fixtures/cases/list-size/library/deals/renewal.md +51 -24
  244. package/fixtures/cases/list-size/schema.md +57 -0
  245. package/fixtures/cases/long-fence-link-definition/schema.md +3 -0
  246. package/fixtures/cases/long-value-past-six-levels/context.json +1 -0
  247. package/fixtures/cases/long-value-past-six-levels/expected.json +5 -0
  248. package/fixtures/cases/long-value-past-six-levels/schema.md +17 -0
  249. package/fixtures/cases/malformed-definition/{schema.yaml → schema.md} +2 -0
  250. package/fixtures/cases/malformed-list/context.json +1 -0
  251. package/fixtures/cases/malformed-list/expected.json +7 -0
  252. package/fixtures/cases/{structured-section → malformed-list}/library/item.md +1 -1
  253. package/fixtures/cases/malformed-list/schema.md +8 -0
  254. package/fixtures/cases/member-document/library/people/ana.md +2 -2
  255. package/fixtures/cases/member-document/{schema.yaml → schema.md} +2 -0
  256. package/fixtures/cases/member-on-another-type/library/note.md +1 -1
  257. package/fixtures/cases/member-on-another-type/{schema.yaml → schema.md} +2 -0
  258. package/fixtures/cases/missing-required-field/library/item.md +1 -1
  259. package/fixtures/cases/missing-required-field/schema.md +8 -0
  260. package/fixtures/cases/misspelled-key/expected.json +2 -1
  261. package/fixtures/cases/misspelled-key/library/companies/acme.md +1 -1
  262. package/fixtures/cases/misspelled-key/library/deals/renewal.md +50 -25
  263. package/fixtures/cases/misspelled-key/schema.md +57 -0
  264. package/fixtures/cases/name-not-a-string/context.json +1 -0
  265. package/fixtures/cases/name-not-a-string/expected.json +5 -0
  266. package/fixtures/cases/name-not-a-string/schema.md +7 -0
  267. package/fixtures/cases/nested-defs/expected.json +2 -1
  268. package/fixtures/cases/nested-defs/library/deal.md +3 -3
  269. package/fixtures/cases/nested-defs/{schema.yaml → schema.md} +2 -0
  270. package/fixtures/cases/nested-object-reference/expected.json +2 -1
  271. package/fixtures/cases/nested-object-reference/library/companies/acme.md +1 -1
  272. package/fixtures/cases/nested-object-reference/library/deals/renewal.md +12 -9
  273. package/fixtures/cases/nested-object-reference/library/places/depot.md +1 -1
  274. package/fixtures/cases/nested-object-reference/library/products/seats.md +1 -1
  275. package/fixtures/cases/nested-object-reference/library/products/support.md +1 -1
  276. package/fixtures/cases/nested-object-reference/schema.md +34 -0
  277. package/fixtures/cases/nested-reference/library/companies/acme.md +1 -1
  278. package/fixtures/cases/nested-reference/library/deals/renewal.md +13 -10
  279. package/fixtures/cases/nested-reference/library/places/depot.md +1 -1
  280. package/fixtures/cases/nested-reference/library/products/seats.md +1 -1
  281. package/fixtures/cases/nested-reference/library/products/support.md +1 -1
  282. package/fixtures/cases/nested-reference/schema.md +34 -0
  283. package/fixtures/cases/nested-required-field/expected.json +2 -1
  284. package/fixtures/cases/nested-required-field/library/companies/acme.md +1 -1
  285. package/fixtures/cases/nested-required-field/library/deals/renewal.md +48 -23
  286. package/fixtures/cases/nested-required-field/schema.md +57 -0
  287. package/fixtures/cases/numeric-range/expected.json +2 -1
  288. package/fixtures/cases/numeric-range/library/companies/acme.md +1 -1
  289. package/fixtures/cases/numeric-range/library/deals/renewal.md +49 -24
  290. package/fixtures/cases/numeric-range/schema.md +57 -0
  291. package/fixtures/cases/one-key-two-labels/context.json +1 -0
  292. package/fixtures/cases/one-key-two-labels/expected.json +5 -0
  293. package/fixtures/cases/one-key-two-labels/schema.md +7 -0
  294. package/fixtures/cases/one-key-two-types/context.json +1 -0
  295. package/fixtures/cases/one-key-two-types/expected.json +5 -0
  296. package/fixtures/cases/one-key-two-types/schema.md +6 -0
  297. package/fixtures/cases/one-of-variant/expected.json +2 -1
  298. package/fixtures/cases/one-of-variant/library/companies/acme.md +1 -1
  299. package/fixtures/cases/one-of-variant/library/deals/renewal.md +49 -24
  300. package/fixtures/cases/one-of-variant/schema.md +57 -0
  301. package/fixtures/cases/other-room-reference/expected.json +2 -1
  302. package/fixtures/cases/other-room-reference/library/companies/acme.md +1 -1
  303. package/fixtures/cases/other-room-reference/library/deals/renewal.md +12 -9
  304. package/fixtures/cases/other-room-reference/library/places/depot.md +1 -1
  305. package/fixtures/cases/other-room-reference/library/products/seats.md +1 -1
  306. package/fixtures/cases/other-room-reference/library/products/support.md +1 -1
  307. package/fixtures/cases/other-room-reference/schema.md +34 -0
  308. package/fixtures/cases/paragraph-link-definition/schema.md +3 -0
  309. package/fixtures/cases/prototype-field/expected.json +2 -1
  310. package/fixtures/cases/prototype-field/library/item.md +2 -2
  311. package/fixtures/cases/prototype-field/schema.md +7 -0
  312. package/fixtures/cases/quoted-link-definition/schema.md +3 -0
  313. package/fixtures/cases/reference-into-a-reserved-name/context.json +1 -0
  314. package/fixtures/cases/reference-into-a-reserved-name/expected.json +7 -0
  315. package/fixtures/cases/reference-into-a-reserved-name/library/notes/a.md +4 -0
  316. package/fixtures/cases/reference-into-a-reserved-name/library/settings/brief.md +1 -0
  317. package/fixtures/cases/reference-into-a-reserved-name/schema.md +5 -0
  318. package/fixtures/cases/reference-to-any-file-name/context.json +1 -0
  319. package/fixtures/cases/reference-to-any-file-name/expected.json +1 -0
  320. package/fixtures/cases/reference-to-any-file-name/library/people/100%.md +4 -0
  321. package/fixtures/cases/reference-to-any-file-name/library/people/C#.md +4 -0
  322. package/fixtures/cases/reference-to-any-file-name/library/people/My notes.md +4 -0
  323. package/fixtures/cases/reference-to-any-file-name/library/people/[draft].md +4 -0
  324. package/fixtures/cases/reference-to-any-file-name/library/people/c (d).md +4 -0
  325. package/fixtures/cases/reference-to-any-file-name/library/people/caf/303/251.md +4 -0
  326. package/fixtures/cases/reference-to-any-file-name/library/people//320/236/321/202/321/207/321/221/321/202.md +4 -0
  327. package/fixtures/cases/reference-to-any-file-name/library/tasks/bracket.md +9 -0
  328. package/fixtures/cases/reference-to-any-file-name/library/tasks/cyrillic.md +9 -0
  329. package/fixtures/cases/reference-to-any-file-name/library/tasks/decomposed.md +9 -0
  330. package/fixtures/cases/reference-to-any-file-name/library/tasks/hash.md +9 -0
  331. package/fixtures/cases/reference-to-any-file-name/library/tasks/parenthesis.md +9 -0
  332. package/fixtures/cases/reference-to-any-file-name/library/tasks/percent.md +9 -0
  333. package/fixtures/cases/reference-to-any-file-name/library/tasks/spaced.md +9 -0
  334. package/fixtures/cases/reference-to-any-file-name/schema.md +12 -0
  335. package/fixtures/cases/referenced-missing-file/library/a.md +2 -2
  336. package/fixtures/cases/referenced-missing-file/library/b.md +2 -2
  337. package/fixtures/cases/referenced-missing-file/schema.md +12 -0
  338. package/fixtures/cases/referenced-missing-non-latin-file/context.json +1 -0
  339. package/fixtures/cases/referenced-missing-non-latin-file/expected.json +6 -0
  340. package/fixtures/cases/referenced-missing-non-latin-file/library/tasks/a.md +9 -0
  341. package/fixtures/cases/referenced-missing-non-latin-file/library/tasks/b.md +9 -0
  342. package/fixtures/cases/referenced-missing-non-latin-file/schema.md +12 -0
  343. package/fixtures/cases/remote-ref/{schema.yaml → schema.md} +2 -0
  344. package/fixtures/cases/schema-body-ignored/context.json +1 -0
  345. package/fixtures/cases/schema-body-ignored/expected.json +1 -0
  346. package/fixtures/cases/schema-body-ignored/schema.md +9 -0
  347. package/fixtures/cases/schema-type-named-folders/context.json +1 -0
  348. package/fixtures/cases/schema-type-named-folders/expected.json +5 -0
  349. package/fixtures/cases/schema-type-named-folders/schema.md +5 -0
  350. package/fixtures/cases/schema-type-named-type/context.json +1 -0
  351. package/fixtures/cases/schema-type-named-type/expected.json +5 -0
  352. package/fixtures/cases/{unknown-type/schema.yaml → schema-type-named-type/schema.md} +3 -1
  353. package/fixtures/cases/second-member-document/{schema.yaml → schema.md} +2 -0
  354. package/fixtures/cases/sections-keyword-refused/context.json +1 -0
  355. package/fixtures/cases/sections-keyword-refused/expected.json +5 -0
  356. package/fixtures/cases/{structured-section/schema.yaml → sections-keyword-refused/schema.md} +3 -2
  357. package/fixtures/cases/shortcut-reference-link/library/item.md +1 -1
  358. package/fixtures/cases/shortcut-reference-link/schema.md +7 -0
  359. package/fixtures/cases/string-length/expected.json +2 -1
  360. package/fixtures/cases/string-length/library/companies/acme.md +1 -1
  361. package/fixtures/cases/string-length/library/deals/renewal.md +49 -24
  362. package/fixtures/cases/string-length/schema.md +57 -0
  363. package/fixtures/cases/submission-keys/library/notes/visitor.md +2 -2
  364. package/fixtures/cases/submission-keys/{schema.yaml → schema.md} +2 -0
  365. package/fixtures/cases/text-field-kind/expected.json +1 -4
  366. package/fixtures/cases/text-field-kind/library/page.md +1 -1
  367. package/fixtures/cases/text-field-kind/{schema.yaml → schema.md} +2 -0
  368. package/fixtures/cases/thematic-break-ends-the-values/context.json +1 -0
  369. package/fixtures/cases/thematic-break-ends-the-values/expected.json +1 -0
  370. package/fixtures/cases/thematic-break-ends-the-values/library/item.md +10 -0
  371. package/fixtures/cases/thematic-break-ends-the-values/schema.md +5 -0
  372. package/fixtures/cases/truncated-link-definition/schema.md +3 -0
  373. package/fixtures/cases/typed-path-names-another-type/context.json +11 -0
  374. package/fixtures/cases/typed-path-names-another-type/expected.json +6 -0
  375. package/fixtures/cases/typed-path-names-another-type/library/settings/prefs/one.md +3 -0
  376. package/fixtures/cases/typed-path-names-another-type/schema.md +8 -0
  377. package/fixtures/cases/typed-path-names-its-type/context.json +11 -0
  378. package/fixtures/cases/typed-path-names-its-type/expected.json +3 -0
  379. package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/deeper/two.md +1 -0
  380. package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/one.md +4 -0
  381. package/fixtures/cases/typed-path-names-its-type/schema.md +8 -0
  382. package/fixtures/cases/typed-path-names-no-type/context.json +11 -0
  383. package/fixtures/cases/typed-path-names-no-type/expected.json +6 -0
  384. package/fixtures/cases/typed-path-names-no-type/library/settings/prefs/one.md +1 -0
  385. package/fixtures/cases/typed-path-names-no-type/schema.md +8 -0
  386. package/fixtures/cases/types-folder-content/library/types/task.md +1 -1
  387. package/fixtures/cases/types-folder-content/{schema.yaml → schema.md} +2 -0
  388. package/fixtures/cases/undeclared-field/expected.json +2 -1
  389. package/fixtures/cases/undeclared-field/library/item.md +2 -2
  390. package/fixtures/cases/undeclared-field/schema.md +7 -0
  391. package/fixtures/cases/unevaluated-items-closed/expected.json +2 -1
  392. package/fixtures/cases/unevaluated-items-closed/library/trip.md +13 -3
  393. package/fixtures/cases/unevaluated-items-closed/{schema.yaml → schema.md} +2 -0
  394. package/fixtures/cases/unique-in-app-schema/context.json +1 -0
  395. package/fixtures/cases/unique-in-app-schema/expected.json +5 -0
  396. package/fixtures/cases/unique-in-app-schema/schema.yaml +7 -0
  397. package/fixtures/cases/unique-indexed-field/context.json +1 -0
  398. package/fixtures/cases/unique-indexed-field/expected.json +1 -0
  399. package/fixtures/cases/unique-indexed-field/library/tickets/first.md +8 -0
  400. package/fixtures/cases/unique-indexed-field/library/tickets/second.md +8 -0
  401. package/fixtures/cases/unique-indexed-field/schema.md +9 -0
  402. package/fixtures/cases/unique-off-indexed-field/context.json +1 -0
  403. package/fixtures/cases/unique-off-indexed-field/expected.json +5 -0
  404. package/fixtures/cases/unique-off-indexed-field/schema.md +9 -0
  405. package/fixtures/cases/unlisted-reference-type/{schema.yaml → schema.md} +2 -0
  406. package/fixtures/cases/unnamed-typed-path-checks-its-fields/context.json +11 -0
  407. package/fixtures/cases/unnamed-typed-path-checks-its-fields/expected.json +6 -0
  408. package/fixtures/cases/unnamed-typed-path-checks-its-fields/library/rooms/team/tasks/plan.md +5 -0
  409. package/fixtures/cases/unnamed-typed-path-checks-its-fields/schema.md +7 -0
  410. package/fixtures/cases/unnamed-typed-path-names-a-type/context.json +11 -0
  411. package/fixtures/cases/unnamed-typed-path-names-a-type/expected.json +6 -0
  412. package/fixtures/cases/unnamed-typed-path-names-a-type/library/rooms/team/tasks/plan.md +7 -0
  413. package/fixtures/cases/unnamed-typed-path-names-a-type/schema.md +7 -0
  414. package/fixtures/cases/unnamed-typed-path-reads-its-type/context.json +11 -0
  415. package/fixtures/cases/unnamed-typed-path-reads-its-type/expected.json +3 -0
  416. package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/plan.md +1 -0
  417. package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/tasks/plan.md +6 -0
  418. package/fixtures/cases/unnamed-typed-path-reads-its-type/schema.md +7 -0
  419. package/fixtures/cases/unnamed-typed-path-without-a-body/context.json +11 -0
  420. package/fixtures/cases/unnamed-typed-path-without-a-body/expected.json +5 -0
  421. package/fixtures/cases/unnamed-typed-path-without-a-body/library/rooms/team/tasks/plan.md +5 -0
  422. package/fixtures/cases/unnamed-typed-path-without-a-body/schema.md +7 -0
  423. package/fixtures/cases/unresolved-link/library/item.md +1 -1
  424. package/fixtures/cases/unresolved-link/schema.md +7 -0
  425. package/fixtures/cases/valid-typed-document/library/people/ada.md +1 -1
  426. package/fixtures/cases/valid-typed-document/library/tasks/one.md +11 -11
  427. package/fixtures/cases/valid-typed-document/schema.md +17 -0
  428. package/fixtures/cases/wide-item-fence-link/schema.md +3 -0
  429. package/fixtures/forms.json +371 -0
  430. package/fixtures/preamble.json +29 -8
  431. package/fixtures/queries.json +1562 -0
  432. package/fixtures/references.json +15 -0
  433. package/package.json +10 -1
  434. package/dist/blocks.d.ts +0 -11
  435. package/dist/blocks.d.ts.map +0 -1
  436. package/dist/blocks.js +0 -91
  437. package/dist/condition.d.ts +0 -181
  438. package/dist/condition.d.ts.map +0 -1
  439. package/dist/condition.js +0 -633
  440. package/dist/divide.d.ts +0 -3
  441. package/dist/divide.d.ts.map +0 -1
  442. package/dist/divide.js +0 -320
  443. package/dist/links.d.ts +0 -29
  444. package/dist/links.d.ts.map +0 -1
  445. package/dist/links.js +0 -831
  446. package/dist/profile.d.ts +0 -6
  447. package/dist/profile.d.ts.map +0 -1
  448. package/dist/profile.js +0 -5
  449. package/dist/project.d.ts +0 -22
  450. package/dist/project.d.ts.map +0 -1
  451. package/dist/project.js +0 -128
  452. package/dist/sections.d.ts +0 -11
  453. package/dist/sections.d.ts.map +0 -1
  454. package/dist/sections.js +0 -57
  455. package/fixtures/cases/absent-link-dangles/schema.yaml +0 -6
  456. package/fixtures/cases/absolute-link/schema.yaml +0 -6
  457. package/fixtures/cases/bracketed-link-definition/schema.yaml +0 -6
  458. package/fixtures/cases/conditional-section/schema.yaml +0 -14
  459. package/fixtures/cases/declared-binary-links/schema.yaml +0 -1
  460. package/fixtures/cases/default-unfilled/schema.yaml +0 -56
  461. package/fixtures/cases/defs-pattern/schema.yaml +0 -56
  462. package/fixtures/cases/dependent-rule/schema.yaml +0 -56
  463. package/fixtures/cases/duplicate-link-definition/schema.yaml +0 -6
  464. package/fixtures/cases/email-format/schema.yaml +0 -56
  465. package/fixtures/cases/fenced-link-definition/schema.yaml +0 -6
  466. package/fixtures/cases/field-enum/schema.yaml +0 -7
  467. package/fixtures/cases/frontmatter-schema/expected.json +0 -5
  468. package/fixtures/cases/integer-field/schema.yaml +0 -56
  469. package/fixtures/cases/interrupted-link-definition/schema.yaml +0 -1
  470. package/fixtures/cases/json-schema-constructs/schema.yaml +0 -56
  471. package/fixtures/cases/list-of-objects-closed/schema.yaml +0 -56
  472. package/fixtures/cases/list-of-objects-item/schema.yaml +0 -56
  473. package/fixtures/cases/list-size/schema.yaml +0 -56
  474. package/fixtures/cases/long-fence-link-definition/schema.yaml +0 -1
  475. package/fixtures/cases/missing-required-field/schema.yaml +0 -7
  476. package/fixtures/cases/missing-section-item-kind/expected.json +0 -5
  477. package/fixtures/cases/missing-section-item-kind/schema.yaml +0 -6
  478. package/fixtures/cases/misspelled-key/schema.yaml +0 -56
  479. package/fixtures/cases/nested-object-reference/schema.yaml +0 -32
  480. package/fixtures/cases/nested-reference/schema.yaml +0 -32
  481. package/fixtures/cases/nested-required-field/schema.yaml +0 -56
  482. package/fixtures/cases/null-refused/expected.json +0 -6
  483. package/fixtures/cases/null-refused/library/companies/acme.md +0 -4
  484. package/fixtures/cases/null-refused/library/deals/renewal.md +0 -31
  485. package/fixtures/cases/null-refused/schema.yaml +0 -56
  486. package/fixtures/cases/numeric-range/schema.yaml +0 -56
  487. package/fixtures/cases/one-of-variant/schema.yaml +0 -56
  488. package/fixtures/cases/open-map-value/expected.json +0 -6
  489. package/fixtures/cases/open-map-value/library/companies/acme.md +0 -4
  490. package/fixtures/cases/open-map-value/library/deals/renewal.md +0 -31
  491. package/fixtures/cases/open-map-value/schema.yaml +0 -56
  492. package/fixtures/cases/other-room-reference/schema.yaml +0 -32
  493. package/fixtures/cases/paragraph-link-definition/schema.yaml +0 -1
  494. package/fixtures/cases/prototype-field/schema.yaml +0 -6
  495. package/fixtures/cases/quoted-link-definition/schema.yaml +0 -1
  496. package/fixtures/cases/referenced-missing-file/schema.yaml +0 -11
  497. package/fixtures/cases/section-item-enum/expected.json +0 -6
  498. package/fixtures/cases/section-item-enum/library/task.md +0 -8
  499. package/fixtures/cases/section-item-enum/schema.yaml +0 -3
  500. package/fixtures/cases/section-item-kind/expected.json +0 -6
  501. package/fixtures/cases/section-item-kind/library/task.md +0 -8
  502. package/fixtures/cases/section-item-kind/schema.yaml +0 -3
  503. package/fixtures/cases/section-items/library/page.md +0 -42
  504. package/fixtures/cases/section-items/schema.yaml +0 -10
  505. package/fixtures/cases/section-reference-path/expected.json +0 -6
  506. package/fixtures/cases/section-reference-path/library/page.md +0 -7
  507. package/fixtures/cases/section-reference-path/schema.yaml +0 -3
  508. package/fixtures/cases/section-reference-type/expected.json +0 -6
  509. package/fixtures/cases/section-reference-type/library/page.md +0 -7
  510. package/fixtures/cases/section-reference-type/schema.yaml +0 -3
  511. package/fixtures/cases/shortcut-reference-link/schema.yaml +0 -6
  512. package/fixtures/cases/string-length/schema.yaml +0 -56
  513. package/fixtures/cases/structured-section/expected.json +0 -6
  514. package/fixtures/cases/truncated-link-definition/schema.yaml +0 -1
  515. package/fixtures/cases/undeclared-field/schema.yaml +0 -6
  516. package/fixtures/cases/unresolved-link/schema.yaml +0 -6
  517. package/fixtures/cases/valid-typed-document/schema.yaml +0 -16
  518. package/fixtures/cases/wide-item-fence-link/schema.yaml +0 -1
  519. package/fixtures/division.json +0 -409
  520. package/fixtures/projection.json +0 -255
  521. /package/fixtures/cases/{conditional-section → block-scalar-frontmatter}/context.json +0 -0
  522. /package/fixtures/cases/{section-items → block-scalar-frontmatter}/expected.json +0 -0
  523. /package/fixtures/cases/{frontmatter-schema → conditional-long-text}/context.json +0 -0
  524. /package/fixtures/cases/{missing-section-item-kind → equal-labels}/context.json +0 -0
  525. /package/fixtures/cases/{null-refused → frontmatter-construct}/context.json +0 -0
  526. /package/fixtures/cases/{open-map-value → labels-in-the-rooms-language}/context.json +0 -0
  527. /package/fixtures/cases/{section-item-enum → lazy-list-item}/context.json +0 -0
  528. /package/fixtures/cases/{section-item-kind → list-item-enum}/context.json +0 -0
  529. /package/fixtures/cases/{section-items → list-item-kind}/context.json +0 -0
  530. /package/fixtures/cases/{section-reference-path → list-items}/context.json +0 -0
  531. /package/fixtures/cases/{section-items → list-items}/library/other.md +0 -0
  532. /package/fixtures/cases/{section-reference-type → list-reference-path}/context.json +0 -0
  533. /package/fixtures/cases/{section-reference-path → list-reference-path}/library/other.md +0 -0
  534. /package/fixtures/cases/{structured-section → list-reference-type}/context.json +0 -0
  535. /package/fixtures/cases/{section-reference-type → list-reference-type}/library/note.md +0 -0
  536. /package/fixtures/cases/{frontmatter-schema/schema.yaml → unknown-type/schema.md} +0 -0
package/dist/document.js CHANGED
@@ -1,40 +1,126 @@
1
- import { ARRAY_INDEX, pointerSegments } from './definition.js';
2
- import { destinations, hrefOccurrences, linkInventory, } from './links.js';
3
- import { destinationPath, relative, retarget, roomPath, roomTarget, SHELL_ROOMS, shellTarget, target, } from './paths.js';
1
+ import { bodyOrder, labelAt, labelOf, labelsIn, lineFits, placedValue, readBody, writtenLabel, writtenValue, } from './body.js';
2
+ import { ARRAY_INDEX, escapePointer, memberFacts, pointerSegments, } from './definition.js';
3
+ import { pointed } from './event.js';
4
+ import { HOUSE_FIELDS, TYPE_FIELD } from './fields.js';
5
+ import { destinations, hrefOccurrences, lineBreaks, lineNumber, linkable, read, sectionSpans, sectionsRecord, } from './markdown.js';
6
+ import { destinationPath, destinationText, keyedLink, relative, retarget, roomPath, roomTarget, SHELL_ROOMS, shellTarget, target, } from './paths.js';
4
7
  import { references } from './references.js';
5
- import { headingSpans, sectionsRecord } from './sections.js';
6
8
  import { refusal } from './refuse.js';
7
- import { bodyAfterFrontmatter, bodyStartAfterYaml, bodyStartOf, equalValue, extractFrontmatter, frontmatterValue, jsValue, lineEnding, mapEntry, parseYamlMapping, serializeJs, serializeKey, YamlError, } from './yaml.js';
9
+ import { bodyStartAfterYaml, bodyStartOf, documentValue, equalValue, extractFrontmatter, isRecord, jsValue, lineEnding, mapEntry, parseYamlMapping, serializeJs, serializeKey, YamlError, } from './yaml.js';
8
10
  const HELD = Symbol('held');
9
11
  function heldOf(document) {
10
12
  return document[HELD];
11
13
  }
12
- export function inventoryOf(document) {
14
+ function bodyReading(content, bodyStart) {
15
+ return read(content.slice(bodyStart), bodyStart);
16
+ }
17
+ function heldReading(content, held) {
18
+ held.reading ??= bodyReading(content, held.internal.bodyStart);
19
+ return held.reading;
20
+ }
21
+ export function readingOf(document) {
13
22
  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
- }
21
- function refuse(file, rule, location) {
22
- return location === undefined ? refusal(file, rule) : refusal(file, rule, { location });
23
- }
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 }));
23
+ return held === undefined
24
+ ? bodyReading(document.content, bodyStartOf(document.content))
25
+ : heldReading(document.content, held);
26
+ }
27
+ function heldBody(content, held) {
28
+ if (held.body !== undefined)
29
+ return held.body;
30
+ const read = readBody(content, held.internal.bodyStart, heldReading(content, held), held.definition.root);
31
+ if (!read.ok)
32
+ throw new Error(`a document the MD model wrote breaks ${read.refusal.rule}`);
33
+ held.body = read.body;
34
+ return held.body;
35
+ }
36
+ export function bodyOf(document) {
37
+ const held = heldOf(document);
38
+ return held === undefined ? null : heldBody(document.content, held);
39
+ }
40
+ export function linkDestinations(document) {
41
+ const bodyStart = heldOf(document)?.internal.bodyStart ?? bodyStartOf(document.content);
42
+ if (!linkable(document.content, bodyStart))
43
+ return [];
44
+ const all = destinations(readingOf(document));
45
+ const body = bodyOf(document);
46
+ if (body === null)
47
+ return all;
48
+ const values = new Set(Object.values(body.links).map((link) => link.start));
49
+ return all.filter((occurrence) => !values.has(occurrence.start));
50
+ }
51
+ function untypedParts(content, held) {
52
+ const spans = sectionSpans(heldReading(content, held), content.length);
53
+ return {
54
+ preamble: content.slice(held.internal.bodyStart, spans[0]?.headingStart),
55
+ sections: Object.freeze(sectionsRecord(spans, content)),
56
+ };
57
+ }
58
+ function heldParts(content, held) {
59
+ if (held.parts !== undefined)
60
+ return held.parts;
61
+ const body = heldBody(content, held);
62
+ held.parts =
63
+ body === null
64
+ ? untypedParts(content, held)
65
+ : {
66
+ preamble: content.slice(body.preamble.start, body.preamble.end),
67
+ sections: Object.freeze(sectionsRecord(body.sections, content)),
68
+ };
69
+ return held.parts;
70
+ }
71
+ function bodyStartIn(document) {
72
+ return heldOf(document)?.internal.bodyStart ?? bodyStartOf(document.content);
73
+ }
74
+ function freeExtents(document) {
75
+ const body = bodyOf(document);
76
+ if (body === null)
77
+ return [{ start: bodyStartIn(document), end: document.content.length }];
78
+ return body.free;
79
+ }
80
+ export function textSpans(document) {
81
+ return freeExtents(document).map(({ start, end }) => ({
82
+ start,
83
+ end,
84
+ text: document.content.slice(start, end),
85
+ }));
86
+ }
87
+ function freeText(content, extents) {
88
+ return extents.map(({ start, end }) => content.slice(start, end)).join('');
89
+ }
90
+ function lineOf(content, offset) {
91
+ return lineNumber(lineBreaks(content), offset);
92
+ }
93
+ function refuse(file, rule, location, line) {
94
+ return refusal(file, rule, {
95
+ ...(location === undefined ? {} : { location }),
96
+ ...(line === undefined ? {} : { line }),
97
+ });
98
+ }
99
+ function freezeDocument(content, type, fields, internal, definition, body) {
100
+ const held = { internal, definition, ...(body === undefined ? {} : { body }) };
101
+ const read = () => heldBody(content, held);
102
+ return Object.freeze(Object.defineProperties({ content, type, fields: Object.freeze(fields) }, {
103
+ preamble: { enumerable: true, get: () => heldParts(content, held).preamble },
104
+ sections: { enumerable: true, get: () => heldParts(content, held).sections },
105
+ text: {
106
+ enumerable: true,
107
+ get: () => {
108
+ const typed = read();
109
+ return typed === null
110
+ ? content.slice(internal.bodyStart)
111
+ : freeText(content, typed.free);
112
+ },
113
+ },
114
+ ranges: {
115
+ enumerable: true,
116
+ get: () => (held.ranges ??= Object.freeze({ ...(read()?.ranges ?? {}) })),
117
+ },
118
+ listEnd: { enumerable: true, get: () => read()?.head ?? internal.bodyStart },
119
+ [HELD]: { value: held },
120
+ }));
33
121
  }
34
122
  function declaredType(fields) {
35
123
  const value = fields.type;
36
- if (value === undefined || value === null)
37
- return undefined;
38
124
  if (typeof value !== 'string' || value === '')
39
125
  return undefined;
40
126
  return value;
@@ -42,88 +128,69 @@ function declaredType(fields) {
42
128
  function opened(content, file) {
43
129
  const fence = extractFrontmatter(content);
44
130
  if (fence.kind === 'none')
45
- return { ok: true, root: null, fields: {}, yamlEnd: 0 };
131
+ return { ok: true, yaml: null, fields: {}, yamlEnd: 0 };
46
132
  if (fence.kind === 'unclosed')
47
133
  return { ok: false, refusal: refuse(file, 'frontmatter-fence') };
48
134
  try {
49
- const root = parseYamlMapping(content, fence.yamlStart, fence.yamlEnd);
50
- 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
- return { ok: true, root, fields: fields, yamlEnd: fence.yamlEnd };
135
+ const yaml = parseYamlMapping(content, fence.yamlStart, fence.yamlEnd);
136
+ const fields = jsValue(yaml);
137
+ return { ok: true, yaml, fields: fields, yamlEnd: fence.yamlEnd };
55
138
  }
56
139
  catch (error) {
57
140
  if (error instanceof YamlError)
58
- return { ok: false, refusal: refuse(file, 'frontmatter-fence') };
141
+ return {
142
+ ok: false,
143
+ refusal: refuse(file, 'frontmatter-fence', error.construct, lineOf(content, error.offset)),
144
+ };
59
145
  throw error;
60
146
  }
61
147
  }
62
- export function parseFrontmatter(content, file = '') {
63
- const read = opened(content, file);
64
- if (!read.ok)
65
- return read;
66
- return {
67
- ok: true,
68
- frontmatter: Object.freeze({
69
- content,
70
- type: declaredType(read.fields),
71
- fields: Object.freeze(read.fields),
72
- }),
73
- };
74
- }
75
- export function outline(content, file) {
76
- const read = parseFrontmatter(content, file);
77
- const targets = bodyLinks(bodyAfterFrontmatter(content), file).map((link) => link.target);
78
- return {
79
- type: read.ok ? (read.frontmatter.type ?? 'any') : null,
80
- fields: read.ok ? read.frontmatter.fields : null,
81
- links: [...new Set(targets)],
82
- };
148
+ function definitionFor(definition, type) {
149
+ return typeof definition === 'function' ? definition(type) : definition;
83
150
  }
84
- export function parse(content, file = '') {
85
- const read = opened(content, file);
86
- if (!read.ok)
87
- return read;
88
- const bodyStart = read.root === null ? 0 : bodyStartAfterYaml(content, read.yamlEnd);
89
- const spans = headingSpans(content, bodyStart);
90
- return {
91
- ok: true,
92
- document: freezeDocument(content, declaredType(read.fields), read.fields, content.slice(bodyStart, spans[0]?.headingStart), sectionsRecord(spans, content), { root: read.root, yamlEnd: read.yamlEnd, bodyStart }),
93
- };
94
- }
95
- function fitsBare(to) {
96
- if (to === '' || to.startsWith('<'))
97
- return false;
98
- let depth = 0;
99
- for (const character of to) {
100
- if (/[\s\u0000-\u001f\u007f]/.test(character))
101
- return false;
102
- if (character === '(')
103
- depth += 1;
104
- else if (character === ')') {
105
- if (depth === 0)
106
- return false;
107
- depth -= 1;
108
- }
151
+ export function parse(content, file = '', definition) {
152
+ const opening = opened(content, file);
153
+ if (!opening.ok)
154
+ return opening;
155
+ const bodyStart = opening.yaml === null ? 0 : bodyStartAfterYaml(content, opening.yamlEnd);
156
+ const type = declaredType(opening.fields);
157
+ const internal = { yaml: opening.yaml, yamlEnd: opening.yamlEnd, bodyStart };
158
+ const typed = definitionFor(definition, type);
159
+ if (typed === undefined)
160
+ return {
161
+ ok: true,
162
+ document: freezeDocument(content, type, opening.fields, internal, undefined, null),
163
+ };
164
+ for (const entry of opening.yaml?.entries ?? []) {
165
+ if (HOUSE_FIELDS.includes(entry.key))
166
+ continue;
167
+ return {
168
+ ok: false,
169
+ refusal: refuse(file, 'malformed-value', `/${escapePointer(entry.key)}`, lineOf(content, entry.value.start)),
170
+ };
109
171
  }
110
- return depth === 0;
111
- }
112
- function fitsBrackets(to) {
113
- return to !== '' && !/[<>\n\r]/.test(to);
172
+ const reading = bodyReading(content, bodyStart);
173
+ const read = readBody(content, bodyStart, reading, typed.root);
174
+ if (!read.ok)
175
+ return {
176
+ ok: false,
177
+ refusal: refuse(file, read.refusal.rule, read.refusal.location, lineOf(content, read.refusal.at)),
178
+ };
179
+ const document = freezeDocument(content, type, { ...opening.fields, ...read.body.values }, internal, typed, read.body);
180
+ heldOf(document).reading = reading;
181
+ return { ok: true, document };
114
182
  }
115
- function destinationText(to, bracketed) {
116
- if (fitsBare(to))
117
- return bracketed && fitsBrackets(to) ? `<${to}>` : to;
118
- if (fitsBrackets(to))
119
- return `<${to}>`;
120
- return null;
183
+ export function create(type, definition) {
184
+ const parsed = parse(type === undefined ? '---\n---\n' : `---\n${TYPE_FIELD}: ${serializeJs(type)}\n---\n`, '', definition);
185
+ if (!parsed.ok)
186
+ throw new Error(`a document of type ${type} does not read`);
187
+ return parsed.document;
121
188
  }
122
189
  function shellRelinked(body, file, handle) {
123
190
  const occurrences = hrefOccurrences(body);
124
191
  const written = new Map();
125
192
  for (const href of new Set(occurrences.map((occurrence) => occurrence.href))) {
126
- const path = shellTarget(href, handle);
193
+ const path = keyedLink(href) === null ? shellTarget(href, handle) : null;
127
194
  if (path === null)
128
195
  continue;
129
196
  const to = retarget(href, relative(file, path));
@@ -142,8 +209,8 @@ function shellRelinked(body, file, handle) {
142
209
  }
143
210
  return rewritten + body.slice(cursor);
144
211
  }
145
- function nodeAt(root, pointer, entryOf = mapEntry) {
146
- let node = root;
212
+ function nodeAt(yaml, pointer, entryOf = mapEntry) {
213
+ let node = yaml;
147
214
  for (const key of pointerSegments(pointer)) {
148
215
  if (node?.kind === 'map')
149
216
  node = entryOf(node, key)?.value;
@@ -163,7 +230,7 @@ function scalarText(source, value) {
163
230
  }
164
231
  export function relativize(document, file, handle, definition, kept = new Set()) {
165
232
  const relativized = (href) => {
166
- const path = shellTarget(href, handle);
233
+ const path = keyedLink(href) === null ? shellTarget(href, handle) : null;
167
234
  return path === null || kept.has(`${SHELL_ROOMS}${handle}/${path}`) ? null : path;
168
235
  };
169
236
  const changes = [];
@@ -179,12 +246,12 @@ export function relativize(document, file, handle, definition, kept = new Set())
179
246
  prior: value,
180
247
  });
181
248
  }
182
- for (const href of new Set(destinations(inventoryOf(document)).map(({ href }) => href))) {
249
+ for (const href of new Set(linkDestinations(document).map(({ href }) => href))) {
183
250
  const path = relativized(href);
184
251
  if (path !== null)
185
252
  changes.push({ kind: 'link', href, to: relative(file, path) });
186
253
  }
187
- return apply(document, changes);
254
+ return apply(document, changes, definition);
188
255
  }
189
256
  export function relativizeBody(body, file, handle) {
190
257
  const relinkedBody = shellRelinked(body, file, handle);
@@ -199,7 +266,7 @@ export function relinkBody(body, file, moves) {
199
266
  let rewritten = '';
200
267
  let cursor = 0;
201
268
  for (const match of hrefOccurrences(body)) {
202
- const from = target(file, match.href) ?? roomPath(match.href);
269
+ const from = target(file, match.href) ?? (keyedLink(match.href) === null ? roomPath(match.href) : null);
203
270
  const to = from === null ? undefined : moves.get(from);
204
271
  if (to === undefined)
205
272
  continue;
@@ -212,6 +279,30 @@ export function relinkBody(body, file, moves) {
212
279
  }
213
280
  return { outcome: 'applied', body: rewritten + body.slice(cursor) };
214
281
  }
282
+ export function unlinkBody(body, hrefs) {
283
+ const { uses, definitions } = read(body);
284
+ const links = new Map();
285
+ for (const use of uses) {
286
+ const { href, bracketed } = use.destination;
287
+ if (!hrefs.has(href) || links.has(href))
288
+ continue;
289
+ const label = body.slice(use.labelStart, use.labelEnd);
290
+ const image = body[use.start] === '!' ? '!' : '';
291
+ links.set(href, `${image}[${label}](${destinationText(href, bracketed) ?? href})`);
292
+ }
293
+ const cut = [...uses, ...definitions]
294
+ .filter((span) => hrefs.has(span.destination.href))
295
+ .sort((left, right) => left.start - right.start);
296
+ let kept = '';
297
+ let cursor = 0;
298
+ for (const span of cut) {
299
+ if (span.start < cursor)
300
+ continue;
301
+ kept += body.slice(cursor, span.start);
302
+ cursor = span.end;
303
+ }
304
+ return { body: kept + body.slice(cursor), links };
305
+ }
215
306
  function linksIn(occurrences, file) {
216
307
  const found = [];
217
308
  const seen = new Set();
@@ -227,7 +318,24 @@ function linksIn(occurrences, file) {
227
318
  return found;
228
319
  }
229
320
  export function links(document, file) {
230
- return linksIn(destinations(inventoryOf(document)), file);
321
+ return linksIn(linkDestinations(document), file);
322
+ }
323
+ export function linkSpans(body) {
324
+ if (!linkable(body))
325
+ return [];
326
+ const { uses, definitions } = read(body);
327
+ const span = (start, end, label, destination) => ({
328
+ start,
329
+ end,
330
+ label,
331
+ href: destination.href,
332
+ destination: { start: destination.start, end: destination.end },
333
+ bracketed: destination.bracketed,
334
+ });
335
+ return [
336
+ ...uses.map((use) => span(use.start, use.end, { start: use.labelStart, end: use.labelEnd }, use.destination)),
337
+ ...definitions.map((definition) => span(definition.start, definition.end, null, definition.destination)),
338
+ ].sort((left, right) => left.start - right.start);
231
339
  }
232
340
  export function bodyLinks(body, file) {
233
341
  return linksIn(hrefOccurrences(body), file);
@@ -236,15 +344,27 @@ function internalOf(document) {
236
344
  const held = heldOf(document);
237
345
  if (held !== undefined)
238
346
  return held.internal;
239
- const read = opened(document.content, '');
240
- if (!read.ok)
241
- return read.refusal;
347
+ const opening = opened(document.content, '');
348
+ if (!opening.ok)
349
+ return opening.refusal;
242
350
  return {
243
- root: read.root,
244
- yamlEnd: read.yamlEnd,
245
- bodyStart: read.root === null ? 0 : bodyStartAfterYaml(document.content, read.yamlEnd),
351
+ yaml: opening.yaml,
352
+ yamlEnd: opening.yamlEnd,
353
+ bodyStart: opening.yaml === null ? 0 : bodyStartAfterYaml(document.content, opening.yamlEnd),
246
354
  };
247
355
  }
356
+ export function bodyWith(document, definition) {
357
+ if (definition === undefined)
358
+ return null;
359
+ const held = heldOf(document);
360
+ if (held !== undefined && held.definition === definition)
361
+ return heldBody(document.content, held);
362
+ const parsed = parse(document.content, '', definition);
363
+ return parsed.ok ? bodyOf(parsed.document) : parsed.refusal;
364
+ }
365
+ function ownField(document, name) {
366
+ return Object.hasOwn(document.fields, name) ? document.fields[name] : undefined;
367
+ }
248
368
  function partOf(change) {
249
369
  if (change.kind === 'field' || change.kind === 'section')
250
370
  return `${change.kind} ${change.name}`;
@@ -252,9 +372,13 @@ function partOf(change) {
252
372
  return `link ${change.href}`;
253
373
  if (change.kind === 'reference')
254
374
  return `reference ${change.pointer}`;
255
- return 'preamble';
375
+ return change.kind;
376
+ }
377
+ function inBody(change, body) {
378
+ return (body !== null &&
379
+ ((change.kind === 'field' && !HOUSE_FIELDS.includes(change.name)) || change.kind === 'text'));
256
380
  }
257
- function planner(document, internal) {
381
+ function planner(document, internal, body, definition) {
258
382
  const content = document.content;
259
383
  const keyed = new Map();
260
384
  let spans;
@@ -273,14 +397,14 @@ function planner(document, internal) {
273
397
  return index.get(key);
274
398
  };
275
399
  const field = (name, value, prior, at) => {
276
- const root = internal.root;
277
- if (root === null)
400
+ const yaml = internal.yaml;
401
+ if (yaml === null)
278
402
  return refuse('', 'frontmatter-fence', name);
279
- if (!equalValue(document.fields[name], prior))
403
+ if (!equalValue(ownField(document, name), prior))
280
404
  return 'stale';
281
- if (!frontmatterValue(value))
405
+ if (!documentValue(value))
282
406
  return refuse('', 'frontmatter-fence', name);
283
- const existing = entryOf(root, name);
407
+ const existing = entryOf(yaml, name);
284
408
  if (existing !== undefined) {
285
409
  const held = existing.value;
286
410
  const inline = held.start > existing.after && !/[\r\n]/.test(content.slice(existing.after, held.start));
@@ -290,33 +414,35 @@ function planner(document, internal) {
290
414
  }
291
415
  const entry = `${serializeKey(name)}: ${serializeJs(value)}`;
292
416
  added += 1;
293
- if (content[root.start] === '{') {
294
- const separator = root.entries.length === 0 && added === 1 ? '' : ', ';
295
- return [{ start: root.end - 1, end: root.end - 1, text: `${separator}${entry}`, at }];
417
+ if (content[yaml.start] === '{') {
418
+ const separator = yaml.entries.length === 0 && added === 1 ? '' : ', ';
419
+ return [{ start: yaml.end - 1, end: yaml.end - 1, text: `${separator}${entry}`, at }];
296
420
  }
297
- const lineStart = content.lastIndexOf('\n', root.start - 1) + 1;
421
+ const lineStartAt = content.lastIndexOf('\n', yaml.start - 1) + 1;
298
422
  ending ??= lineEnding(content);
299
- const line = `${content.slice(lineStart, root.start)}${entry}${ending}`;
423
+ const line = `${content.slice(lineStartAt, yaml.start)}${entry}${ending}`;
300
424
  return [{ start: internal.yamlEnd, end: internal.yamlEnd, text: line, at, entry: true }];
301
425
  };
302
426
  const section = (name, value, prior, at) => {
303
427
  if (spans === undefined) {
304
428
  spans = new Map();
305
- for (const span of headingSpans(content, internal.bodyStart))
429
+ const found = body === null ? sectionSpans(readingOf(document), content.length) : body.sections;
430
+ for (const span of found)
306
431
  if (!spans.has(span.name))
307
432
  spans.set(span.name, span);
308
433
  }
309
434
  const span = spans.get(name);
310
435
  if (span === undefined)
311
- return refuse('', 'required-section', name);
312
- if (content.slice(span.bodyStart, span.bodyEnd) !== prior)
436
+ return unopenable(name, definition) ?? 'stale';
437
+ const held = content.slice(span.bodyStart, span.bodyEnd);
438
+ if (!samePart(held, prior))
313
439
  return 'stale';
314
- return [{ start: span.bodyStart, end: span.bodyEnd, text: value, at }];
440
+ return [{ start: span.bodyStart, end: span.bodyEnd, text: reframed(held, value), at }];
315
441
  };
316
442
  const link = (href, to, at) => {
317
443
  if (occurrences === undefined) {
318
444
  occurrences = new Map();
319
- for (const occurrence of destinations(inventoryOf(document))) {
445
+ for (const occurrence of linkDestinations(document)) {
320
446
  const same = occurrences.get(occurrence.href);
321
447
  if (same === undefined)
322
448
  occurrences.set(occurrence.href, [occurrence]);
@@ -331,14 +457,23 @@ function planner(document, internal) {
331
457
  if (destinationText(written, false) === null)
332
458
  return refuse('', 'unwritable-link', written);
333
459
  return found.map((occurrence) => ({
334
- start: internal.bodyStart + occurrence.start,
335
- end: internal.bodyStart + occurrence.end,
460
+ start: occurrence.start,
461
+ end: occurrence.end,
336
462
  text: destinationText(written, occurrence.bracketed),
337
463
  at,
338
464
  }));
339
465
  };
340
466
  const reference = (pointer, value, prior, at) => {
341
- const node = internal.root === null ? undefined : nodeAt(internal.root, pointer, entryOf);
467
+ if (body !== null) {
468
+ const destination = body.links[pointer];
469
+ if (destination === undefined || pointed(document.fields, pointer) !== prior)
470
+ return 'stale';
471
+ const text = destinationText(value, destination.bracketed);
472
+ if (text === null)
473
+ return refuse('', 'unwritable-link', value);
474
+ return [{ start: destination.start, end: destination.end, text, at }];
475
+ }
476
+ const node = internal.yaml === null ? undefined : nodeAt(internal.yaml, pointer, entryOf);
342
477
  if (node?.kind !== 'scalar' || node.value !== prior)
343
478
  return 'stale';
344
479
  return [
@@ -354,11 +489,316 @@ function planner(document, internal) {
354
489
  return link(change.href, change.to, at);
355
490
  if (change.kind === 'reference')
356
491
  return reference(change.pointer, change.value, change.prior, at);
357
- if (document.preamble !== change.prior)
492
+ if (change.kind === 'text') {
493
+ if (document.text !== change.prior)
494
+ return 'stale';
495
+ return [{ start: internal.bodyStart, end: content.length, text: change.value, at }];
496
+ }
497
+ const preamble = document.preamble;
498
+ if (!samePart(preamble, change.prior))
358
499
  return 'stale';
500
+ const text = reframed(preamble, change.value);
501
+ if (body !== null) {
502
+ const { start, end } = body.preamble;
503
+ const before = content.slice(internal.bodyStart, start);
504
+ const gap = start < end || text === '' || before === '' || before.endsWith('\n\n')
505
+ ? ''
506
+ : before.endsWith('\n')
507
+ ? '\n'
508
+ : '\n\n';
509
+ return [{ start, end, text: `${gap}${text}`, at }];
510
+ }
359
511
  const start = internal.bodyStart;
360
- return [{ start, end: start + document.preamble.length, text: change.value, at }];
512
+ return [{ start, end: start + preamble.length, text, at }];
513
+ };
514
+ }
515
+ function repointed(value, segments, to) {
516
+ if (segments.length === 0)
517
+ return to;
518
+ const [segment, ...rest] = segments;
519
+ if (Array.isArray(value))
520
+ return value.map((item, index) => String(index) === segment ? repointed(item, rest, to) : item);
521
+ const held = value;
522
+ const copy = Object.create(null);
523
+ for (const key of Object.keys(held))
524
+ copy[key] = key === segment ? repointed(held[key], rest, to) : held[key];
525
+ return copy;
526
+ }
527
+ function framed(part) {
528
+ const lead = /^(?:[ \t]*\r?\n)*/u.exec(part)[0];
529
+ let end = part.length;
530
+ while (end > lead.length && ' \t\r\n'.includes(part[end - 1]))
531
+ end -= 1;
532
+ return { lead, text: part.slice(lead.length, end), tail: part.slice(end) };
533
+ }
534
+ export function partText(part) {
535
+ return framed(part).text;
536
+ }
537
+ function samePart(held, prior) {
538
+ return prior !== undefined && partText(held) === partText(prior);
539
+ }
540
+ function lineEnds(text) {
541
+ return text.split('\n').length - 1;
542
+ }
543
+ function wider(held, written) {
544
+ return lineEnds(held) > lineEnds(written) ? held : written;
545
+ }
546
+ function reframed(held, value) {
547
+ const was = framed(held);
548
+ const next = framed(value);
549
+ if (next.text === '')
550
+ return wider(was.lead, value);
551
+ return `${wider(was.lead, next.lead)}${next.text}${wider(was.tail, next.tail)}`;
552
+ }
553
+ function bulletAt(content, start) {
554
+ let at = start;
555
+ while (content[at] === ' ' || content[at] === '\t')
556
+ at += 1;
557
+ return content[at] === '*' || content[at] === '+' ? content[at] : '-';
558
+ }
559
+ function blankAhead(content, at) {
560
+ return /^[ \t]*(?:\r?\n|$)/u.test(content.slice(at, at + 256));
561
+ }
562
+ function blankBehind(text) {
563
+ return /\n[ \t]*\r?\n$/u.test(text) || text === '';
564
+ }
565
+ function blankBefore(content, at, floor) {
566
+ let start = at;
567
+ while (start > floor) {
568
+ const previous = content.lastIndexOf('\n', start - 2) + 1;
569
+ if (previous < floor || content.slice(previous, start).trim() !== '')
570
+ return start;
571
+ start = previous;
572
+ }
573
+ return start;
574
+ }
575
+ function skipBlank(content, at) {
576
+ let next = at;
577
+ for (;;) {
578
+ const match = /^[ \t]*\r?\n/u.exec(content.slice(next, next + 256));
579
+ if (match === null)
580
+ return next;
581
+ next += match[0].length;
582
+ }
583
+ }
584
+ function bodyPlan(content, internal, body, definition, fields, text, nameOf) {
585
+ const e = lineEnding(content);
586
+ const root = definition.root;
587
+ const order = bodyOrder(root);
588
+ const scale = order.length + 2;
589
+ const rank = (key) => {
590
+ const index = order.indexOf(key);
591
+ return index === -1 ? order.length : index;
592
+ };
593
+ const placed = new Map(body.top.map((one) => [one.key, one]));
594
+ const decided = new Map();
595
+ const changeOf = new Map();
596
+ for (const { key, value, at } of fields) {
597
+ changeOf.set(key, at);
598
+ if (value === undefined) {
599
+ decided.set(key, null);
600
+ continue;
601
+ }
602
+ const written = placedValue(key, root, value, { ending: e, nameOf });
603
+ if ('unwritable' in written)
604
+ return refuse('', 'malformed-value', written.unwritable);
605
+ decided.set(key, written);
606
+ }
607
+ const stays = (one) => {
608
+ const next = decided.get(one.key);
609
+ return next === undefined || (next !== null && next.as === one.as);
610
+ };
611
+ const lines = body.top.filter((one) => one.as === 'line' && stays(one));
612
+ const headings = body.top.filter((one) => one.as === 'heading' && stays(one));
613
+ const added = [...decided].filter(([key, next]) => next !== null && placed.get(key)?.as !== next.as);
614
+ const listed = lines.length > 0 || added.some(([, next]) => next.as === 'line');
615
+ const edits = [];
616
+ const inserts = [];
617
+ const nearest = (among, key) => {
618
+ const own = rank(key);
619
+ let before;
620
+ let after;
621
+ for (const one of among) {
622
+ const held = rank(one.key);
623
+ if (held < own && (before === undefined || held > rank(before.key)))
624
+ before = one;
625
+ if (held > own && (after === undefined || held < rank(after.key)))
626
+ after = one;
627
+ }
628
+ return { before, after };
361
629
  };
630
+ const marker = body.list === null ? '-' : bulletAt(content, body.list.start);
631
+ const item = (text) => (text.startsWith('- ') ? `${marker}${text.slice(1)}` : text);
632
+ let listRemoved = false;
633
+ for (const [key, next] of decided) {
634
+ const before = placed.get(key);
635
+ const at = changeOf.get(key);
636
+ if (before !== undefined && (next === null || next.as !== before.as)) {
637
+ if (before.as === 'line' && !listed && body.list !== null) {
638
+ if (!listRemoved)
639
+ edits.push({
640
+ start: body.list.start,
641
+ end: skipBlank(content, body.list.end),
642
+ text: '',
643
+ at,
644
+ });
645
+ listRemoved = true;
646
+ }
647
+ else {
648
+ const end = before.as === 'line' ? before.end : before.next;
649
+ const start = end === content.length
650
+ ? blankBefore(content, before.start, internal.bodyStart)
651
+ : before.start;
652
+ edits.push({ start, end, text: '', at });
653
+ }
654
+ }
655
+ else if (before !== undefined && next !== null) {
656
+ edits.push({
657
+ start: before.start,
658
+ end: before.end,
659
+ text: next.as === 'line' ? item(next.text) : next.text,
660
+ at,
661
+ });
662
+ }
663
+ if (next === null || (before !== undefined && before.as === next.as))
664
+ continue;
665
+ const own = rank(key);
666
+ if (next.as === 'name') {
667
+ inserts.push({ at: internal.bodyStart, rank: 0, text: next.text, kind: 'block', change: at });
668
+ }
669
+ else if (next.as === 'line') {
670
+ const around = nearest(lines, key);
671
+ if (around.before !== undefined)
672
+ inserts.push({
673
+ at: around.before.end,
674
+ rank: 1 + own / scale,
675
+ text: item(next.text),
676
+ kind: 'item',
677
+ change: at,
678
+ });
679
+ else if (around.after !== undefined)
680
+ inserts.push({
681
+ at: around.after.start,
682
+ rank: 1 + own / scale,
683
+ text: item(next.text),
684
+ kind: 'item',
685
+ change: at,
686
+ });
687
+ else if (body.list !== null && listed)
688
+ inserts.push({
689
+ at: body.list.start,
690
+ rank: 1 + own / scale,
691
+ text: item(next.text),
692
+ kind: 'item',
693
+ change: at,
694
+ });
695
+ else
696
+ inserts.push({
697
+ at: body.listAt,
698
+ rank: 1 + own / scale,
699
+ text: next.text,
700
+ kind: 'line',
701
+ change: at,
702
+ });
703
+ }
704
+ else {
705
+ const around = nearest(headings, key);
706
+ const where = around.before !== undefined
707
+ ? around.before.next
708
+ : around.after !== undefined
709
+ ? around.after.start
710
+ : body.anchor;
711
+ inserts.push({
712
+ at: where,
713
+ rank: 2 + own / scale,
714
+ text: next.text,
715
+ kind: 'block',
716
+ change: at,
717
+ });
718
+ }
719
+ }
720
+ if (text !== undefined) {
721
+ const holds = body.top.some((one) => stays(one)) || added.length > 0;
722
+ if (!holds)
723
+ return [{ start: internal.bodyStart, end: content.length, text: text.value, at: text.at }];
724
+ for (const extent of body.free) {
725
+ if (body.ending !== null && extent.start >= body.ending.end)
726
+ continue;
727
+ if (extent.end > extent.start)
728
+ edits.push({ ...extent, text: '', at: text.at });
729
+ }
730
+ if (body.ending !== null)
731
+ edits.push({
732
+ start: text.value === ''
733
+ ? blankBefore(content, body.ending.start, internal.bodyStart)
734
+ : body.ending.start,
735
+ end: content.length,
736
+ text: '',
737
+ at: text.at,
738
+ });
739
+ if (text.value !== '')
740
+ inserts.push({
741
+ at: body.ending?.start ?? content.length,
742
+ rank: 3,
743
+ text: `---${e}${text.value}`,
744
+ kind: 'block',
745
+ change: text.at,
746
+ });
747
+ }
748
+ const grouped = new Map();
749
+ for (const one of inserts)
750
+ grouped.set(one.at, [...(grouped.get(one.at) ?? []), one]);
751
+ const cuts = edits
752
+ .filter((edit) => edit.text === '' && edit.end > edit.start)
753
+ .sort((left, right) => left.start - right.start);
754
+ const kept = (from, to) => {
755
+ let text = '';
756
+ let cursor = from;
757
+ for (const cut of cuts) {
758
+ if (cut.end <= cursor || cut.start >= to)
759
+ continue;
760
+ text += content.slice(cursor, Math.max(cursor, cut.start));
761
+ cursor = Math.max(cursor, cut.end);
762
+ }
763
+ return text + content.slice(cursor, Math.max(cursor, to));
764
+ };
765
+ let previous;
766
+ for (const [at, group] of [...grouped].sort(([left], [right]) => left - right)) {
767
+ group.sort((left, right) => left.rank - right.rank);
768
+ const between = kept(previous?.at ?? internal.bodyStart, at);
769
+ const opening = previous === undefined && between.trim() === '';
770
+ const base = previous !== undefined && between.trim() === ''
771
+ ? `${previous.out}${between}`
772
+ : between.slice(-8);
773
+ let out = '';
774
+ let last;
775
+ for (const one of group) {
776
+ if (one.kind === 'item' || (one.kind === 'line' && last === 'line')) {
777
+ out += one.text;
778
+ last = one.kind;
779
+ continue;
780
+ }
781
+ const behind = `${base}${out}`;
782
+ if (!(opening && out === '')) {
783
+ if (behind !== '' && !behind.endsWith('\n'))
784
+ out += `${e}${e}`;
785
+ else if (!blankBehind(behind))
786
+ out += e;
787
+ }
788
+ out += one.text;
789
+ last = one.kind;
790
+ }
791
+ if (last !== 'item' && !blankAhead(kept(at, content.length), 0))
792
+ out += e;
793
+ edits.push({
794
+ start: at,
795
+ end: at,
796
+ text: out,
797
+ at: Math.min(...group.map((one) => one.change)),
798
+ });
799
+ previous = { at, out };
800
+ }
801
+ return edits;
362
802
  }
363
803
  function overlapping(edits, stale) {
364
804
  let reach = -1;
@@ -372,24 +812,24 @@ function overlapping(edits, stale) {
372
812
  }
373
813
  }
374
814
  }
375
- function firstHeadings(content, bodyStart) {
815
+ function firstHeadings(reading) {
376
816
  const first = new Map();
377
- for (const span of headingSpans(content, bodyStart)) {
817
+ for (const span of sectionSpans(reading, 0)) {
378
818
  if (!first.has(span.name))
379
819
  first.set(span.name, span.headingStart);
380
820
  }
381
821
  return first;
382
822
  }
383
- function hidden(before, bodyStart, after, shifts) {
384
- const placed = firstHeadings(after, bodyStartOf(after));
823
+ function hidden(before, after, shifts) {
824
+ const placedAfter = firstHeadings(after);
385
825
  let shift = 0;
386
826
  let index = 0;
387
- for (const [name, at] of firstHeadings(before, bodyStart)) {
827
+ for (const [name, at] of firstHeadings(before)) {
388
828
  while (index < shifts.length && shifts[index][0] <= at) {
389
829
  shift += shifts[index][1];
390
830
  index += 1;
391
831
  }
392
- if (placed.get(name) !== at + shift)
832
+ if (placedAfter.get(name) !== at + shift)
393
833
  return name;
394
834
  }
395
835
  return undefined;
@@ -420,15 +860,186 @@ function spliced(source, edits) {
420
860
  pieces.push(source.slice(cursor));
421
861
  return { content: pieces.join(''), shifts };
422
862
  }
423
- export function apply(document, changes) {
863
+ function checkedReading(source, replaced) {
864
+ const checked = spliced(source, replaced);
865
+ return {
866
+ reading: bodyReading(checked.content, bodyStartOf(checked.content)),
867
+ shifts: checked.shifts,
868
+ };
869
+ }
870
+ function opening(change) {
871
+ return change.kind === 'section' && change.prior === undefined;
872
+ }
873
+ function unopenable(name, definition) {
874
+ const root = definition?.root;
875
+ const field = root === undefined
876
+ ? undefined
877
+ : bodyOrder(root).find((key) => labelOf(key, root.properties?.[key]) === name.trim());
878
+ if (field !== undefined)
879
+ return refuse('', 'field-section', field);
880
+ if (read(`## ${name}`, 0).headings[0]?.name !== name)
881
+ return refuse('', 'unwritable-section', name);
882
+ return undefined;
883
+ }
884
+ function withSections(document, changes, definition, nameOf) {
885
+ const opened = [];
886
+ const held = [];
887
+ for (const [at, change] of changes.entries()) {
888
+ if (!opening(change))
889
+ continue;
890
+ const refusal = unopenable(change.name, definition);
891
+ if (refusal !== undefined)
892
+ return { outcome: 'refuse', refusal };
893
+ if (Object.hasOwn(document.sections, change.name) || opened.includes(change.name))
894
+ held.push(at);
895
+ else
896
+ opened.push(change.name);
897
+ }
898
+ const source = document.content;
899
+ const ending = lineEnding(source);
900
+ const lead = source === '' || source.endsWith('\n') ? '' : ending;
901
+ const skeleton = parse(`${source}${lead}${opened.map((name) => `## ${name}${ending}`).join('')}`, '', definition);
902
+ if (!skeleton.ok)
903
+ return { outcome: 'refuse', refusal: skeleton.refusal };
904
+ const kept = changes.flatMap((change, at) => held.includes(at) ? [] : [[opening(change) ? { ...change, prior: '' } : change, at]]);
905
+ const applied = apply(skeleton.document, kept.map(([change]) => change), definition, nameOf);
906
+ if (applied.outcome === 'refuse')
907
+ return applied;
908
+ const stale = [
909
+ ...held,
910
+ ...(applied.outcome === 'stale' ? applied.stale.map((at) => kept[at][1]) : []),
911
+ ];
912
+ if (stale.length > 0)
913
+ return { outcome: 'stale', document, stale: stale.sort((left, right) => left - right) };
914
+ return applied;
915
+ }
916
+ function markdownIn(facts) {
917
+ if (facts === undefined || facts.json === true)
918
+ return false;
919
+ return (facts.markdown === true ||
920
+ Object.values(facts.properties ?? {}).some(markdownIn) ||
921
+ (facts.prefix ?? []).some(markdownIn) ||
922
+ markdownIn(facts.items) ||
923
+ markdownIn(facts.additional) ||
924
+ (facts.patterns ?? []).some((one) => markdownIn(one.facts)));
925
+ }
926
+ function lined(value) {
927
+ if (typeof value === 'string')
928
+ return lineFits(value);
929
+ if (Array.isArray(value))
930
+ return value.every(lined);
931
+ return isRecord(value) ? Object.values(value).every(lined) : true;
932
+ }
933
+ function fromValues(change, root) {
934
+ if (change.kind === 'reference')
935
+ return true;
936
+ return (change.kind === 'field' &&
937
+ (HOUSE_FIELDS.includes(change.name) ||
938
+ (!markdownIn(memberFacts(root, change.name)) && lined(change.value))));
939
+ }
940
+ function written(content, before, fields, changes, definition) {
941
+ const opening = opened(content, '');
942
+ if (!opening.ok)
943
+ throw new Error(`a document the MD model wrote breaks ${opening.refusal.rule}`);
944
+ const values = { ...before.values };
945
+ for (const { key, value } of fields)
946
+ if (value === undefined)
947
+ delete values[key];
948
+ else
949
+ values[key] = writtenValue(value, memberFacts(definition.root, key));
950
+ for (const change of changes)
951
+ if (change.kind === 'reference' && before.links[change.pointer] !== undefined) {
952
+ const [key, ...rest] = pointerSegments(change.pointer);
953
+ values[key] = repointed(values[key], rest, change.value);
954
+ }
955
+ return freezeDocument(content, declaredType(opening.fields), { ...opening.fields, ...values }, {
956
+ yaml: opening.yaml,
957
+ yamlEnd: opening.yamlEnd,
958
+ bodyStart: opening.yaml === null ? 0 : bodyStartAfterYaml(content, opening.yamlEnd),
959
+ }, definition);
960
+ }
961
+ function settled(before, after, changes, root) {
962
+ const set = new Map();
963
+ for (const change of changes)
964
+ if (change.kind === 'field' && !HOUSE_FIELDS.includes(change.name))
965
+ set.set(change.name, change.value);
966
+ for (const [key, value] of set)
967
+ if (!equalValue(ownField(after, key), writtenValue(value, memberFacts(root, key))))
968
+ return refuse('', 'malformed-value', `/${escapePointer(key)}`);
969
+ if (!changes.some((change) => change.kind === 'section' || change.kind === 'preamble' || change.kind === 'text'))
970
+ return undefined;
971
+ for (const key of new Set([...Object.keys(before.fields), ...Object.keys(after.fields)])) {
972
+ if (set.has(key) || HOUSE_FIELDS.includes(key))
973
+ continue;
974
+ if (!equalValue(ownField(before, key), ownField(after, key)))
975
+ return refuse('', 'overlapping-section', `/${escapePointer(key)}`);
976
+ }
977
+ return undefined;
978
+ }
979
+ export function apply(document, changes, definition, nameOf) {
980
+ if (changes.some(opening))
981
+ return withSections(document, changes, definition, nameOf);
424
982
  const internal = internalOf(document);
425
983
  if (!('bodyStart' in internal))
426
984
  return { outcome: 'refuse', refusal: internal };
427
- const plan = planner(document, internal);
985
+ const body = bodyWith(document, definition);
986
+ if (body !== null && !('top' in body))
987
+ return { outcome: 'refuse', refusal: body };
988
+ const plan = planner(document, internal, body, definition);
428
989
  const edits = [];
429
990
  const stale = new Set();
430
991
  const named = new Set();
992
+ const fields = [];
993
+ const moved = new Set();
994
+ let text;
431
995
  for (const [at, change] of changes.entries()) {
996
+ const part = partOf(change);
997
+ if (named.has(part)) {
998
+ stale.add(at);
999
+ continue;
1000
+ }
1001
+ if (change.kind === 'reference' && body !== null && body.links[change.pointer] === undefined) {
1002
+ if (pointed(document.fields, change.pointer) !== change.prior) {
1003
+ stale.add(at);
1004
+ continue;
1005
+ }
1006
+ const [key, ...rest] = pointerSegments(change.pointer);
1007
+ const whole = `field ${key}`;
1008
+ const held = moved.has(key) ? fields.findIndex((field) => field.key === key) : -1;
1009
+ if (named.has(whole) && held === -1) {
1010
+ stale.add(at);
1011
+ continue;
1012
+ }
1013
+ moved.add(key);
1014
+ if (held === -1)
1015
+ fields.push({ key: key, value: repointed(document.fields[key], rest, change.value), at });
1016
+ else
1017
+ fields[held] = {
1018
+ ...fields[held],
1019
+ value: repointed(fields[held].value, rest, change.value),
1020
+ };
1021
+ named.add(part);
1022
+ named.add(whole);
1023
+ continue;
1024
+ }
1025
+ if (inBody(change, body)) {
1026
+ if (change.kind === 'field') {
1027
+ if (!equalValue(ownField(document, change.name), change.prior)) {
1028
+ stale.add(at);
1029
+ continue;
1030
+ }
1031
+ fields.push({ key: change.name, value: change.value, at });
1032
+ }
1033
+ else if (change.kind === 'text') {
1034
+ if (document.text !== change.prior) {
1035
+ stale.add(at);
1036
+ continue;
1037
+ }
1038
+ text = { value: change.value, at };
1039
+ }
1040
+ named.add(part);
1041
+ continue;
1042
+ }
432
1043
  const planned = plan(change, at);
433
1044
  if (planned === 'stale') {
434
1045
  stale.add(at);
@@ -436,41 +1047,67 @@ export function apply(document, changes) {
436
1047
  }
437
1048
  if (!Array.isArray(planned))
438
1049
  return { outcome: 'refuse', refusal: planned };
439
- const part = partOf(change);
440
- if (named.has(part)) {
441
- stale.add(at);
442
- continue;
443
- }
444
1050
  named.add(part);
445
1051
  for (const edit of planned)
446
1052
  edits.push(edit);
447
1053
  }
1054
+ if (body !== null && (fields.length > 0 || text !== undefined)) {
1055
+ const planned = bodyPlan(document.content, internal, body, definition, fields, text, nameOf);
1056
+ if (!Array.isArray(planned))
1057
+ return { outcome: 'refuse', refusal: planned };
1058
+ edits.push(...planned);
1059
+ }
448
1060
  edits.sort((left, right) => left.start - right.start || left.end - right.end || left.at - right.at);
449
1061
  overlapping(edits, stale);
450
1062
  if (stale.size > 0)
451
1063
  return { outcome: 'stale', document, stale: [...stale].sort((left, right) => left - right) };
452
1064
  const source = document.content;
453
- const { content } = spliced(source, edits);
1065
+ const { content, shifts } = spliced(source, edits);
454
1066
  if (content === source)
455
1067
  return { outcome: 'applied', document };
1068
+ if (body !== null && changes.every((change) => fromValues(change, definition.root)))
1069
+ return {
1070
+ outcome: 'applied',
1071
+ document: written(content, body, fields, changes, definition),
1072
+ };
1073
+ const parsed = parse(content, '', definition);
1074
+ if (!parsed.ok)
1075
+ return { outcome: 'refuse', refusal: parsed.refusal };
1076
+ const unsettled = body === null ? undefined : settled(document, parsed.document, changes, definition.root);
1077
+ if (unsettled !== undefined)
1078
+ return { outcome: 'refuse', refusal: unsettled };
456
1079
  const replaced = edits.filter((edit) => {
457
1080
  const kind = changes[edit.at].kind;
458
1081
  return kind === 'section' || kind === 'preamble';
459
1082
  });
460
1083
  if (replaced.length > 0) {
461
- const checked = spliced(source, replaced);
462
- const name = hidden(source, internal.bodyStart, checked.content, checked.shifts);
1084
+ const checked = replaced.length === edits.length
1085
+ ? { reading: readingOf(parsed.document), shifts }
1086
+ : checkedReading(source, replaced);
1087
+ const name = hidden(readingOf(document), checked.reading, checked.shifts);
463
1088
  if (name !== undefined)
464
1089
  return { outcome: 'refuse', refusal: refuse('', 'overlapping-section', name) };
465
1090
  }
466
- const parsed = parse(content);
467
- if (!parsed.ok)
468
- return { outcome: 'refuse', refusal: parsed.refusal };
469
1091
  return { outcome: 'applied', document: parsed.document };
470
1092
  }
1093
+ export function relabel(document, before, after) {
1094
+ const body = bodyWith(document, before);
1095
+ if (body === null || !('top' in body))
1096
+ return document.content;
1097
+ let content = '';
1098
+ let cursor = 0;
1099
+ for (const label of [...body.labels].sort((left, right) => left.start - right.start)) {
1100
+ const next = labelAt(after.root, label.path);
1101
+ if (next === undefined || next === labelAt(before.root, label.path))
1102
+ continue;
1103
+ content += document.content.slice(cursor, label.start) + writtenLabel(next, label.written);
1104
+ cursor = label.end;
1105
+ }
1106
+ return content + document.content.slice(cursor);
1107
+ }
1108
+ export function relabels(before, after) {
1109
+ return JSON.stringify([...labelsIn(before.root)]) !== JSON.stringify([...labelsIn(after.root)]);
1110
+ }
471
1111
  export function serialize(document) {
472
1112
  return document.content;
473
1113
  }
474
- export function frontmatter(document) {
475
- return heldOf(document)?.internal.root ?? null;
476
- }