@agentshouse/mdmodel 0.1.0-alpha.8 → 0.1.0-c001b4068d4dc

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