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