@agentshouse/mdmodel 0.1.0-ccf1367190878 → 0.1.0-cd6a8e5bc7efc

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