@agentshouse/mdmodel 0.1.0-cc58fdf3541b7 → 0.1.0-ccb295d7b1fee
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 +229 -235
- package/README.md +27 -25
- 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 +24 -11
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +188 -128
- package/dist/document.d.ts +15 -9
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +769 -149
- 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 +930 -0
- package/dist/house-model.d.ts.map +1 -0
- package/dist/house-model.js +511 -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 +44 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +27 -11
- package/dist/indexed.d.ts +9 -0
- package/dist/indexed.d.ts.map +1 -0
- package/dist/indexed.js +55 -0
- package/dist/kinds.d.ts +21 -0
- package/dist/kinds.d.ts.map +1 -0
- package/dist/kinds.js +210 -0
- package/dist/markdown.d.ts +71 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +232 -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 +16 -1
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +107 -22
- package/dist/pattern.d.ts.map +1 -1
- package/dist/pattern.js +4 -3
- package/dist/query/language.d.ts +255 -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 +158 -0
- package/dist/query/sql.d.ts +43 -0
- package/dist/query/sql.d.ts.map +1 -0
- package/dist/query/sql.js +285 -0
- package/dist/query/walk.d.ts +63 -0
- package/dist/query/walk.d.ts.map +1 -0
- package/dist/query/walk.js +263 -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 +7 -0
- package/dist/reserved.d.ts.map +1 -0
- package/dist/reserved.js +32 -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 -3
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +9 -73
- package/dist/types.d.ts +22 -43
- 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 +217 -179
- package/dist/yaml.d.ts +7 -5
- package/dist/yaml.d.ts.map +1 -1
- package/dist/yaml.js +158 -44
- package/fixtures/README.md +89 -69
- 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/block-scalar-frontmatter/library/notes/literal.md +17 -0
- package/fixtures/cases/block-scalar-frontmatter/library/skills/notes/SKILL.md +10 -0
- 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-section → conditional-long-text}/expected.json +3 -2
- package/fixtures/cases/{conditional-section → conditional-long-text}/library/questions/hosting.md +2 -2
- package/fixtures/cases/{conditional-section → conditional-long-text}/library/questions/payments.md +2 -2
- 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/frontmatter-construct/expected.json +7 -0
- package/fixtures/cases/frontmatter-construct/library/note.md +6 -0
- 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/expected.json +1 -0
- 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/context.json +1 -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-another-type/library/note.md +1 -1
- 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/reference-to-any-file-name/context.json +1 -0
- package/fixtures/cases/reference-to-any-file-name/expected.json +1 -0
- package/fixtures/cases/reference-to-any-file-name/library/people/100%.md +4 -0
- package/fixtures/cases/reference-to-any-file-name/library/people/C#.md +4 -0
- package/fixtures/cases/reference-to-any-file-name/library/people/My notes.md +4 -0
- package/fixtures/cases/reference-to-any-file-name/library/people/[draft].md +4 -0
- package/fixtures/cases/reference-to-any-file-name/library/people/c (d).md +4 -0
- package/fixtures/cases/reference-to-any-file-name/library/people/caf/303/251.md +4 -0
- package/fixtures/cases/reference-to-any-file-name/library/people//320/236/321/202/321/207/321/221/321/202.md +4 -0
- package/fixtures/cases/reference-to-any-file-name/library/tasks/bracket.md +9 -0
- package/fixtures/cases/reference-to-any-file-name/library/tasks/cyrillic.md +9 -0
- package/fixtures/cases/reference-to-any-file-name/library/tasks/decomposed.md +9 -0
- package/fixtures/cases/reference-to-any-file-name/library/tasks/hash.md +9 -0
- package/fixtures/cases/reference-to-any-file-name/library/tasks/parenthesis.md +9 -0
- package/fixtures/cases/reference-to-any-file-name/library/tasks/percent.md +9 -0
- package/fixtures/cases/reference-to-any-file-name/library/tasks/spaced.md +9 -0
- package/fixtures/cases/reference-to-any-file-name/schema.yaml +10 -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/referenced-missing-non-latin-file/context.json +1 -0
- package/fixtures/cases/referenced-missing-non-latin-file/expected.json +6 -0
- package/fixtures/cases/referenced-missing-non-latin-file/library/tasks/a.md +9 -0
- package/fixtures/cases/referenced-missing-non-latin-file/library/tasks/b.md +9 -0
- package/fixtures/cases/referenced-missing-non-latin-file/schema.yaml +10 -0
- 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/forms.json +371 -0
- package/fixtures/preamble.json +29 -8
- package/fixtures/queries.json +1359 -0
- package/package.json +10 -1
- package/dist/blocks.d.ts +0 -11
- package/dist/blocks.d.ts.map +0 -1
- package/dist/blocks.js +0 -91
- package/dist/condition.d.ts +0 -181
- package/dist/condition.d.ts.map +0 -1
- package/dist/condition.js +0 -633
- package/dist/divide.d.ts +0 -3
- package/dist/divide.d.ts.map +0 -1
- package/dist/divide.js +0 -320
- package/dist/links.d.ts +0 -29
- package/dist/links.d.ts.map +0 -1
- package/dist/links.js +0 -831
- package/dist/profile.d.ts +0 -6
- package/dist/profile.d.ts.map +0 -1
- package/dist/profile.js +0 -5
- package/dist/project.d.ts +0 -22
- package/dist/project.d.ts.map +0 -1
- package/dist/project.js +0 -128
- package/dist/sections.d.ts +0 -11
- package/dist/sections.d.ts.map +0 -1
- package/dist/sections.js +0 -57
- package/fixtures/cases/conditional-section/schema.yaml +0 -14
- 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/division.json +0 -409
- package/fixtures/projection.json +0 -255
- /package/fixtures/cases/{conditional-section → block-scalar-frontmatter}/context.json +0 -0
- /package/fixtures/cases/{section-items → block-scalar-frontmatter}/expected.json +0 -0
- /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 → frontmatter-construct}/context.json +0 -0
- /package/fixtures/cases/{section-item-enum → labels-in-the-rooms-language}/context.json +0 -0
- /package/fixtures/cases/{section-item-kind → lazy-list-item}/context.json +0 -0
- /package/fixtures/cases/{section-reference-path → link-into-a-reserved-name}/context.json +0 -0
- /package/fixtures/cases/{section-items → link-into-a-reserved-name-from-an-untyped-document}/context.json +0 -0
- /package/fixtures/cases/{section-reference-type → list-item-enum}/context.json +0 -0
- /package/fixtures/cases/{structured-section → list-item-kind}/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 {
|
|
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,88 +128,69 @@ function declaredType(fields) {
|
|
|
42
128
|
function opened(content, file) {
|
|
43
129
|
const fence = extractFrontmatter(content);
|
|
44
130
|
if (fence.kind === 'none')
|
|
45
|
-
return { ok: true,
|
|
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', error.construct, 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 outline(content, file) {
|
|
76
|
-
const read = parseFrontmatter(content, file);
|
|
77
|
-
const targets = bodyLinks(bodyAfterFrontmatter(content), file).map((link) => link.target);
|
|
78
|
-
return {
|
|
79
|
-
type: read.ok ? (read.frontmatter.type ?? 'any') : null,
|
|
80
|
-
fields: read.ok ? read.frontmatter.fields : null,
|
|
81
|
-
links: [...new Set(targets)],
|
|
82
|
-
};
|
|
148
|
+
function definitionFor(definition, type) {
|
|
149
|
+
return typeof definition === 'function' ? definition(type) : definition;
|
|
83
150
|
}
|
|
84
|
-
export function parse(content, file = '') {
|
|
85
|
-
const
|
|
86
|
-
if (!
|
|
87
|
-
return
|
|
88
|
-
const bodyStart =
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
else if (character === ')') {
|
|
105
|
-
if (depth === 0)
|
|
106
|
-
return false;
|
|
107
|
-
depth -= 1;
|
|
108
|
-
}
|
|
151
|
+
export function parse(content, file = '', definition) {
|
|
152
|
+
const opening = opened(content, file);
|
|
153
|
+
if (!opening.ok)
|
|
154
|
+
return opening;
|
|
155
|
+
const bodyStart = opening.yaml === null ? 0 : bodyStartAfterYaml(content, opening.yamlEnd);
|
|
156
|
+
const type = declaredType(opening.fields);
|
|
157
|
+
const internal = { yaml: opening.yaml, yamlEnd: opening.yamlEnd, bodyStart };
|
|
158
|
+
const typed = definitionFor(definition, type);
|
|
159
|
+
if (typed === undefined)
|
|
160
|
+
return {
|
|
161
|
+
ok: true,
|
|
162
|
+
document: freezeDocument(content, type, opening.fields, internal, undefined, null),
|
|
163
|
+
};
|
|
164
|
+
for (const entry of opening.yaml?.entries ?? []) {
|
|
165
|
+
if (HOUSE_FIELDS.includes(entry.key))
|
|
166
|
+
continue;
|
|
167
|
+
return {
|
|
168
|
+
ok: false,
|
|
169
|
+
refusal: refuse(file, 'malformed-value', `/${escapePointer(entry.key)}`, lineOf(content, entry.value.start)),
|
|
170
|
+
};
|
|
109
171
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
172
|
+
const reading = bodyReading(content, bodyStart);
|
|
173
|
+
const read = readBody(content, bodyStart, reading, typed.root);
|
|
174
|
+
if (!read.ok)
|
|
175
|
+
return {
|
|
176
|
+
ok: false,
|
|
177
|
+
refusal: refuse(file, read.refusal.rule, read.refusal.location, lineOf(content, read.refusal.at)),
|
|
178
|
+
};
|
|
179
|
+
const document = freezeDocument(content, type, { ...opening.fields, ...read.body.values }, internal, typed, read.body);
|
|
180
|
+
heldOf(document).reading = reading;
|
|
181
|
+
return { ok: true, document };
|
|
114
182
|
}
|
|
115
|
-
function
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return null;
|
|
183
|
+
export function create(type, definition) {
|
|
184
|
+
const parsed = parse(type === undefined ? '---\n---\n' : `---\n${TYPE_FIELD}: ${serializeJs(type)}\n---\n`, '', definition);
|
|
185
|
+
if (!parsed.ok)
|
|
186
|
+
throw new Error(`a document of type ${type} does not read`);
|
|
187
|
+
return parsed.document;
|
|
121
188
|
}
|
|
122
189
|
function shellRelinked(body, file, handle) {
|
|
123
190
|
const occurrences = hrefOccurrences(body);
|
|
124
191
|
const written = new Map();
|
|
125
192
|
for (const href of new Set(occurrences.map((occurrence) => occurrence.href))) {
|
|
126
|
-
const path = shellTarget(href, handle);
|
|
193
|
+
const path = keyedLink(href) === null ? shellTarget(href, handle) : null;
|
|
127
194
|
if (path === null)
|
|
128
195
|
continue;
|
|
129
196
|
const to = retarget(href, relative(file, path));
|
|
@@ -142,8 +209,8 @@ function shellRelinked(body, file, handle) {
|
|
|
142
209
|
}
|
|
143
210
|
return rewritten + body.slice(cursor);
|
|
144
211
|
}
|
|
145
|
-
function nodeAt(
|
|
146
|
-
let node =
|
|
212
|
+
function nodeAt(yaml, pointer, entryOf = mapEntry) {
|
|
213
|
+
let node = yaml;
|
|
147
214
|
for (const key of pointerSegments(pointer)) {
|
|
148
215
|
if (node?.kind === 'map')
|
|
149
216
|
node = entryOf(node, key)?.value;
|
|
@@ -163,7 +230,7 @@ function scalarText(source, value) {
|
|
|
163
230
|
}
|
|
164
231
|
export function relativize(document, file, handle, definition, kept = new Set()) {
|
|
165
232
|
const relativized = (href) => {
|
|
166
|
-
const path = shellTarget(href, handle);
|
|
233
|
+
const path = keyedLink(href) === null ? shellTarget(href, handle) : null;
|
|
167
234
|
return path === null || kept.has(`${SHELL_ROOMS}${handle}/${path}`) ? null : path;
|
|
168
235
|
};
|
|
169
236
|
const changes = [];
|
|
@@ -179,12 +246,12 @@ export function relativize(document, file, handle, definition, kept = new Set())
|
|
|
179
246
|
prior: value,
|
|
180
247
|
});
|
|
181
248
|
}
|
|
182
|
-
for (const href of new Set(
|
|
249
|
+
for (const href of new Set(linkDestinations(document).map(({ href }) => href))) {
|
|
183
250
|
const path = relativized(href);
|
|
184
251
|
if (path !== null)
|
|
185
252
|
changes.push({ kind: 'link', href, to: relative(file, path) });
|
|
186
253
|
}
|
|
187
|
-
return apply(document, changes);
|
|
254
|
+
return apply(document, changes, definition);
|
|
188
255
|
}
|
|
189
256
|
export function relativizeBody(body, file, handle) {
|
|
190
257
|
const relinkedBody = shellRelinked(body, file, handle);
|
|
@@ -199,7 +266,7 @@ export function relinkBody(body, file, moves) {
|
|
|
199
266
|
let rewritten = '';
|
|
200
267
|
let cursor = 0;
|
|
201
268
|
for (const match of hrefOccurrences(body)) {
|
|
202
|
-
const from = target(file, match.href) ?? roomPath(match.href);
|
|
269
|
+
const from = target(file, match.href) ?? (keyedLink(match.href) === null ? roomPath(match.href) : null);
|
|
203
270
|
const to = from === null ? undefined : moves.get(from);
|
|
204
271
|
if (to === undefined)
|
|
205
272
|
continue;
|
|
@@ -212,6 +279,30 @@ export function relinkBody(body, file, moves) {
|
|
|
212
279
|
}
|
|
213
280
|
return { outcome: 'applied', body: rewritten + body.slice(cursor) };
|
|
214
281
|
}
|
|
282
|
+
export function unlinkBody(body, hrefs) {
|
|
283
|
+
const { uses, definitions } = read(body);
|
|
284
|
+
const links = new Map();
|
|
285
|
+
for (const use of uses) {
|
|
286
|
+
const { href, bracketed } = use.destination;
|
|
287
|
+
if (!hrefs.has(href) || links.has(href))
|
|
288
|
+
continue;
|
|
289
|
+
const label = body.slice(use.labelStart, use.labelEnd);
|
|
290
|
+
const image = body[use.start] === '!' ? '!' : '';
|
|
291
|
+
links.set(href, `${image}[${label}](${destinationText(href, bracketed) ?? href})`);
|
|
292
|
+
}
|
|
293
|
+
const cut = [...uses, ...definitions]
|
|
294
|
+
.filter((span) => hrefs.has(span.destination.href))
|
|
295
|
+
.sort((left, right) => left.start - right.start);
|
|
296
|
+
let kept = '';
|
|
297
|
+
let cursor = 0;
|
|
298
|
+
for (const span of cut) {
|
|
299
|
+
if (span.start < cursor)
|
|
300
|
+
continue;
|
|
301
|
+
kept += body.slice(cursor, span.start);
|
|
302
|
+
cursor = span.end;
|
|
303
|
+
}
|
|
304
|
+
return { body: kept + body.slice(cursor), links };
|
|
305
|
+
}
|
|
215
306
|
function linksIn(occurrences, file) {
|
|
216
307
|
const found = [];
|
|
217
308
|
const seen = new Set();
|
|
@@ -227,7 +318,7 @@ function linksIn(occurrences, file) {
|
|
|
227
318
|
return found;
|
|
228
319
|
}
|
|
229
320
|
export function links(document, file) {
|
|
230
|
-
return linksIn(
|
|
321
|
+
return linksIn(linkDestinations(document), file);
|
|
231
322
|
}
|
|
232
323
|
export function bodyLinks(body, file) {
|
|
233
324
|
return linksIn(hrefOccurrences(body), file);
|
|
@@ -236,15 +327,27 @@ function internalOf(document) {
|
|
|
236
327
|
const held = heldOf(document);
|
|
237
328
|
if (held !== undefined)
|
|
238
329
|
return held.internal;
|
|
239
|
-
const
|
|
240
|
-
if (!
|
|
241
|
-
return
|
|
330
|
+
const opening = opened(document.content, '');
|
|
331
|
+
if (!opening.ok)
|
|
332
|
+
return opening.refusal;
|
|
242
333
|
return {
|
|
243
|
-
|
|
244
|
-
yamlEnd:
|
|
245
|
-
bodyStart:
|
|
334
|
+
yaml: opening.yaml,
|
|
335
|
+
yamlEnd: opening.yamlEnd,
|
|
336
|
+
bodyStart: opening.yaml === null ? 0 : bodyStartAfterYaml(document.content, opening.yamlEnd),
|
|
246
337
|
};
|
|
247
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
|
+
}
|
|
248
351
|
function partOf(change) {
|
|
249
352
|
if (change.kind === 'field' || change.kind === 'section')
|
|
250
353
|
return `${change.kind} ${change.name}`;
|
|
@@ -252,9 +355,13 @@ function partOf(change) {
|
|
|
252
355
|
return `link ${change.href}`;
|
|
253
356
|
if (change.kind === 'reference')
|
|
254
357
|
return `reference ${change.pointer}`;
|
|
255
|
-
return
|
|
358
|
+
return change.kind;
|
|
256
359
|
}
|
|
257
|
-
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, definition) {
|
|
258
365
|
const content = document.content;
|
|
259
366
|
const keyed = new Map();
|
|
260
367
|
let spans;
|
|
@@ -273,14 +380,14 @@ function planner(document, internal) {
|
|
|
273
380
|
return index.get(key);
|
|
274
381
|
};
|
|
275
382
|
const field = (name, value, prior, at) => {
|
|
276
|
-
const
|
|
277
|
-
if (
|
|
383
|
+
const yaml = internal.yaml;
|
|
384
|
+
if (yaml === null)
|
|
278
385
|
return refuse('', 'frontmatter-fence', name);
|
|
279
|
-
if (!equalValue(document
|
|
386
|
+
if (!equalValue(ownField(document, name), prior))
|
|
280
387
|
return 'stale';
|
|
281
|
-
if (!
|
|
388
|
+
if (!documentValue(value))
|
|
282
389
|
return refuse('', 'frontmatter-fence', name);
|
|
283
|
-
const existing = entryOf(
|
|
390
|
+
const existing = entryOf(yaml, name);
|
|
284
391
|
if (existing !== undefined) {
|
|
285
392
|
const held = existing.value;
|
|
286
393
|
const inline = held.start > existing.after && !/[\r\n]/.test(content.slice(existing.after, held.start));
|
|
@@ -290,33 +397,35 @@ function planner(document, internal) {
|
|
|
290
397
|
}
|
|
291
398
|
const entry = `${serializeKey(name)}: ${serializeJs(value)}`;
|
|
292
399
|
added += 1;
|
|
293
|
-
if (content[
|
|
294
|
-
const separator =
|
|
295
|
-
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 }];
|
|
296
403
|
}
|
|
297
|
-
const
|
|
404
|
+
const lineStartAt = content.lastIndexOf('\n', yaml.start - 1) + 1;
|
|
298
405
|
ending ??= lineEnding(content);
|
|
299
|
-
const line = `${content.slice(
|
|
406
|
+
const line = `${content.slice(lineStartAt, yaml.start)}${entry}${ending}`;
|
|
300
407
|
return [{ start: internal.yamlEnd, end: internal.yamlEnd, text: line, at, entry: true }];
|
|
301
408
|
};
|
|
302
409
|
const section = (name, value, prior, at) => {
|
|
303
410
|
if (spans === undefined) {
|
|
304
411
|
spans = new Map();
|
|
305
|
-
|
|
412
|
+
const found = body === null ? sectionSpans(readingOf(document), content.length) : body.sections;
|
|
413
|
+
for (const span of found)
|
|
306
414
|
if (!spans.has(span.name))
|
|
307
415
|
spans.set(span.name, span);
|
|
308
416
|
}
|
|
309
417
|
const span = spans.get(name);
|
|
310
418
|
if (span === undefined)
|
|
311
|
-
return
|
|
312
|
-
|
|
419
|
+
return unopenable(name, definition) ?? 'stale';
|
|
420
|
+
const held = content.slice(span.bodyStart, span.bodyEnd);
|
|
421
|
+
if (!samePart(held, prior))
|
|
313
422
|
return 'stale';
|
|
314
|
-
return [{ start: span.bodyStart, end: span.bodyEnd, text: value, at }];
|
|
423
|
+
return [{ start: span.bodyStart, end: span.bodyEnd, text: reframed(held, value), at }];
|
|
315
424
|
};
|
|
316
425
|
const link = (href, to, at) => {
|
|
317
426
|
if (occurrences === undefined) {
|
|
318
427
|
occurrences = new Map();
|
|
319
|
-
for (const occurrence of
|
|
428
|
+
for (const occurrence of linkDestinations(document)) {
|
|
320
429
|
const same = occurrences.get(occurrence.href);
|
|
321
430
|
if (same === undefined)
|
|
322
431
|
occurrences.set(occurrence.href, [occurrence]);
|
|
@@ -331,14 +440,23 @@ function planner(document, internal) {
|
|
|
331
440
|
if (destinationText(written, false) === null)
|
|
332
441
|
return refuse('', 'unwritable-link', written);
|
|
333
442
|
return found.map((occurrence) => ({
|
|
334
|
-
start:
|
|
335
|
-
end:
|
|
443
|
+
start: occurrence.start,
|
|
444
|
+
end: occurrence.end,
|
|
336
445
|
text: destinationText(written, occurrence.bracketed),
|
|
337
446
|
at,
|
|
338
447
|
}));
|
|
339
448
|
};
|
|
340
449
|
const reference = (pointer, value, prior, at) => {
|
|
341
|
-
|
|
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);
|
|
342
460
|
if (node?.kind !== 'scalar' || node.value !== prior)
|
|
343
461
|
return 'stale';
|
|
344
462
|
return [
|
|
@@ -354,11 +472,316 @@ function planner(document, internal) {
|
|
|
354
472
|
return link(change.href, change.to, at);
|
|
355
473
|
if (change.kind === 'reference')
|
|
356
474
|
return reference(change.pointer, change.value, change.prior, at);
|
|
357
|
-
if (
|
|
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))
|
|
358
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
|
+
}
|
|
359
494
|
const start = internal.bodyStart;
|
|
360
|
-
return [{ start, end: start +
|
|
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
|
+
export function partText(part) {
|
|
518
|
+
return framed(part).text;
|
|
519
|
+
}
|
|
520
|
+
function samePart(held, prior) {
|
|
521
|
+
return prior !== undefined && partText(held) === partText(prior);
|
|
522
|
+
}
|
|
523
|
+
function lineEnds(text) {
|
|
524
|
+
return text.split('\n').length - 1;
|
|
525
|
+
}
|
|
526
|
+
function wider(held, written) {
|
|
527
|
+
return lineEnds(held) > lineEnds(written) ? held : written;
|
|
528
|
+
}
|
|
529
|
+
function reframed(held, value) {
|
|
530
|
+
const was = framed(held);
|
|
531
|
+
const next = framed(value);
|
|
532
|
+
if (next.text === '')
|
|
533
|
+
return wider(was.lead, value);
|
|
534
|
+
return `${wider(was.lead, next.lead)}${next.text}${wider(was.tail, next.tail)}`;
|
|
535
|
+
}
|
|
536
|
+
function bulletAt(content, start) {
|
|
537
|
+
let at = start;
|
|
538
|
+
while (content[at] === ' ' || content[at] === '\t')
|
|
539
|
+
at += 1;
|
|
540
|
+
return content[at] === '*' || content[at] === '+' ? content[at] : '-';
|
|
541
|
+
}
|
|
542
|
+
function blankAhead(content, at) {
|
|
543
|
+
return /^[ \t]*(?:\r?\n|$)/u.test(content.slice(at, at + 256));
|
|
544
|
+
}
|
|
545
|
+
function blankBehind(text) {
|
|
546
|
+
return /\n[ \t]*\r?\n$/u.test(text) || text === '';
|
|
547
|
+
}
|
|
548
|
+
function blankBefore(content, at, floor) {
|
|
549
|
+
let start = at;
|
|
550
|
+
while (start > floor) {
|
|
551
|
+
const previous = content.lastIndexOf('\n', start - 2) + 1;
|
|
552
|
+
if (previous < floor || content.slice(previous, start).trim() !== '')
|
|
553
|
+
return start;
|
|
554
|
+
start = previous;
|
|
555
|
+
}
|
|
556
|
+
return start;
|
|
557
|
+
}
|
|
558
|
+
function skipBlank(content, at) {
|
|
559
|
+
let next = at;
|
|
560
|
+
for (;;) {
|
|
561
|
+
const match = /^[ \t]*\r?\n/u.exec(content.slice(next, next + 256));
|
|
562
|
+
if (match === null)
|
|
563
|
+
return next;
|
|
564
|
+
next += match[0].length;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
function bodyPlan(content, internal, body, definition, fields, text, nameOf) {
|
|
568
|
+
const e = lineEnding(content);
|
|
569
|
+
const root = definition.root;
|
|
570
|
+
const order = bodyOrder(root);
|
|
571
|
+
const scale = order.length + 2;
|
|
572
|
+
const rank = (key) => {
|
|
573
|
+
const index = order.indexOf(key);
|
|
574
|
+
return index === -1 ? order.length : index;
|
|
575
|
+
};
|
|
576
|
+
const placed = new Map(body.top.map((one) => [one.key, one]));
|
|
577
|
+
const decided = new Map();
|
|
578
|
+
const changeOf = new Map();
|
|
579
|
+
for (const { key, value, at } of fields) {
|
|
580
|
+
changeOf.set(key, at);
|
|
581
|
+
if (value === undefined) {
|
|
582
|
+
decided.set(key, null);
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
const written = placedValue(key, root, value, { ending: e, nameOf });
|
|
586
|
+
if ('unwritable' in written)
|
|
587
|
+
return refuse('', 'malformed-value', written.unwritable);
|
|
588
|
+
decided.set(key, written);
|
|
589
|
+
}
|
|
590
|
+
const stays = (one) => {
|
|
591
|
+
const next = decided.get(one.key);
|
|
592
|
+
return next === undefined || (next !== null && next.as === one.as);
|
|
593
|
+
};
|
|
594
|
+
const lines = body.top.filter((one) => one.as === 'line' && stays(one));
|
|
595
|
+
const headings = body.top.filter((one) => one.as === 'heading' && stays(one));
|
|
596
|
+
const added = [...decided].filter(([key, next]) => next !== null && placed.get(key)?.as !== next.as);
|
|
597
|
+
const listed = lines.length > 0 || added.some(([, next]) => next.as === 'line');
|
|
598
|
+
const edits = [];
|
|
599
|
+
const inserts = [];
|
|
600
|
+
const nearest = (among, key) => {
|
|
601
|
+
const own = rank(key);
|
|
602
|
+
let before;
|
|
603
|
+
let after;
|
|
604
|
+
for (const one of among) {
|
|
605
|
+
const held = rank(one.key);
|
|
606
|
+
if (held < own && (before === undefined || held > rank(before.key)))
|
|
607
|
+
before = one;
|
|
608
|
+
if (held > own && (after === undefined || held < rank(after.key)))
|
|
609
|
+
after = one;
|
|
610
|
+
}
|
|
611
|
+
return { before, after };
|
|
612
|
+
};
|
|
613
|
+
const marker = body.list === null ? '-' : bulletAt(content, body.list.start);
|
|
614
|
+
const item = (text) => (text.startsWith('- ') ? `${marker}${text.slice(1)}` : text);
|
|
615
|
+
let listRemoved = false;
|
|
616
|
+
for (const [key, next] of decided) {
|
|
617
|
+
const before = placed.get(key);
|
|
618
|
+
const at = changeOf.get(key);
|
|
619
|
+
if (before !== undefined && (next === null || next.as !== before.as)) {
|
|
620
|
+
if (before.as === 'line' && !listed && body.list !== null) {
|
|
621
|
+
if (!listRemoved)
|
|
622
|
+
edits.push({
|
|
623
|
+
start: body.list.start,
|
|
624
|
+
end: skipBlank(content, body.list.end),
|
|
625
|
+
text: '',
|
|
626
|
+
at,
|
|
627
|
+
});
|
|
628
|
+
listRemoved = true;
|
|
629
|
+
}
|
|
630
|
+
else {
|
|
631
|
+
const end = before.as === 'line' ? before.end : before.next;
|
|
632
|
+
const start = end === content.length
|
|
633
|
+
? blankBefore(content, before.start, internal.bodyStart)
|
|
634
|
+
: before.start;
|
|
635
|
+
edits.push({ start, end, text: '', at });
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
else if (before !== undefined && next !== null) {
|
|
639
|
+
edits.push({
|
|
640
|
+
start: before.start,
|
|
641
|
+
end: before.end,
|
|
642
|
+
text: next.as === 'line' ? item(next.text) : next.text,
|
|
643
|
+
at,
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
if (next === null || (before !== undefined && before.as === next.as))
|
|
647
|
+
continue;
|
|
648
|
+
const own = rank(key);
|
|
649
|
+
if (next.as === 'name') {
|
|
650
|
+
inserts.push({ at: internal.bodyStart, rank: 0, text: next.text, kind: 'block', change: at });
|
|
651
|
+
}
|
|
652
|
+
else if (next.as === 'line') {
|
|
653
|
+
const around = nearest(lines, key);
|
|
654
|
+
if (around.before !== undefined)
|
|
655
|
+
inserts.push({
|
|
656
|
+
at: around.before.end,
|
|
657
|
+
rank: 1 + own / scale,
|
|
658
|
+
text: item(next.text),
|
|
659
|
+
kind: 'item',
|
|
660
|
+
change: at,
|
|
661
|
+
});
|
|
662
|
+
else if (around.after !== undefined)
|
|
663
|
+
inserts.push({
|
|
664
|
+
at: around.after.start,
|
|
665
|
+
rank: 1 + own / scale,
|
|
666
|
+
text: item(next.text),
|
|
667
|
+
kind: 'item',
|
|
668
|
+
change: at,
|
|
669
|
+
});
|
|
670
|
+
else if (body.list !== null && listed)
|
|
671
|
+
inserts.push({
|
|
672
|
+
at: body.list.start,
|
|
673
|
+
rank: 1 + own / scale,
|
|
674
|
+
text: item(next.text),
|
|
675
|
+
kind: 'item',
|
|
676
|
+
change: at,
|
|
677
|
+
});
|
|
678
|
+
else
|
|
679
|
+
inserts.push({
|
|
680
|
+
at: body.listAt,
|
|
681
|
+
rank: 1 + own / scale,
|
|
682
|
+
text: next.text,
|
|
683
|
+
kind: 'line',
|
|
684
|
+
change: at,
|
|
685
|
+
});
|
|
686
|
+
}
|
|
687
|
+
else {
|
|
688
|
+
const around = nearest(headings, key);
|
|
689
|
+
const where = around.before !== undefined
|
|
690
|
+
? around.before.next
|
|
691
|
+
: around.after !== undefined
|
|
692
|
+
? around.after.start
|
|
693
|
+
: body.anchor;
|
|
694
|
+
inserts.push({
|
|
695
|
+
at: where,
|
|
696
|
+
rank: 2 + own / scale,
|
|
697
|
+
text: next.text,
|
|
698
|
+
kind: 'block',
|
|
699
|
+
change: at,
|
|
700
|
+
});
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
if (text !== undefined) {
|
|
704
|
+
const holds = body.top.some((one) => stays(one)) || added.length > 0;
|
|
705
|
+
if (!holds)
|
|
706
|
+
return [{ start: internal.bodyStart, end: content.length, text: text.value, at: text.at }];
|
|
707
|
+
for (const extent of body.free) {
|
|
708
|
+
if (body.ending !== null && extent.start >= body.ending.end)
|
|
709
|
+
continue;
|
|
710
|
+
if (extent.end > extent.start)
|
|
711
|
+
edits.push({ ...extent, text: '', at: text.at });
|
|
712
|
+
}
|
|
713
|
+
if (body.ending !== null)
|
|
714
|
+
edits.push({
|
|
715
|
+
start: text.value === ''
|
|
716
|
+
? blankBefore(content, body.ending.start, internal.bodyStart)
|
|
717
|
+
: body.ending.start,
|
|
718
|
+
end: content.length,
|
|
719
|
+
text: '',
|
|
720
|
+
at: text.at,
|
|
721
|
+
});
|
|
722
|
+
if (text.value !== '')
|
|
723
|
+
inserts.push({
|
|
724
|
+
at: body.ending?.start ?? content.length,
|
|
725
|
+
rank: 3,
|
|
726
|
+
text: `---${e}${text.value}`,
|
|
727
|
+
kind: 'block',
|
|
728
|
+
change: text.at,
|
|
729
|
+
});
|
|
730
|
+
}
|
|
731
|
+
const grouped = new Map();
|
|
732
|
+
for (const one of inserts)
|
|
733
|
+
grouped.set(one.at, [...(grouped.get(one.at) ?? []), one]);
|
|
734
|
+
const cuts = edits
|
|
735
|
+
.filter((edit) => edit.text === '' && edit.end > edit.start)
|
|
736
|
+
.sort((left, right) => left.start - right.start);
|
|
737
|
+
const kept = (from, to) => {
|
|
738
|
+
let text = '';
|
|
739
|
+
let cursor = from;
|
|
740
|
+
for (const cut of cuts) {
|
|
741
|
+
if (cut.end <= cursor || cut.start >= to)
|
|
742
|
+
continue;
|
|
743
|
+
text += content.slice(cursor, Math.max(cursor, cut.start));
|
|
744
|
+
cursor = Math.max(cursor, cut.end);
|
|
745
|
+
}
|
|
746
|
+
return text + content.slice(cursor, Math.max(cursor, to));
|
|
361
747
|
};
|
|
748
|
+
let previous;
|
|
749
|
+
for (const [at, group] of [...grouped].sort(([left], [right]) => left - right)) {
|
|
750
|
+
group.sort((left, right) => left.rank - right.rank);
|
|
751
|
+
const between = kept(previous?.at ?? internal.bodyStart, at);
|
|
752
|
+
const opening = previous === undefined && between.trim() === '';
|
|
753
|
+
const base = previous !== undefined && between.trim() === ''
|
|
754
|
+
? `${previous.out}${between}`
|
|
755
|
+
: between.slice(-8);
|
|
756
|
+
let out = '';
|
|
757
|
+
let last;
|
|
758
|
+
for (const one of group) {
|
|
759
|
+
if (one.kind === 'item' || (one.kind === 'line' && last === 'line')) {
|
|
760
|
+
out += one.text;
|
|
761
|
+
last = one.kind;
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
const behind = `${base}${out}`;
|
|
765
|
+
if (!(opening && out === '')) {
|
|
766
|
+
if (behind !== '' && !behind.endsWith('\n'))
|
|
767
|
+
out += `${e}${e}`;
|
|
768
|
+
else if (!blankBehind(behind))
|
|
769
|
+
out += e;
|
|
770
|
+
}
|
|
771
|
+
out += one.text;
|
|
772
|
+
last = one.kind;
|
|
773
|
+
}
|
|
774
|
+
if (last !== 'item' && !blankAhead(kept(at, content.length), 0))
|
|
775
|
+
out += e;
|
|
776
|
+
edits.push({
|
|
777
|
+
start: at,
|
|
778
|
+
end: at,
|
|
779
|
+
text: out,
|
|
780
|
+
at: Math.min(...group.map((one) => one.change)),
|
|
781
|
+
});
|
|
782
|
+
previous = { at, out };
|
|
783
|
+
}
|
|
784
|
+
return edits;
|
|
362
785
|
}
|
|
363
786
|
function overlapping(edits, stale) {
|
|
364
787
|
let reach = -1;
|
|
@@ -372,24 +795,24 @@ function overlapping(edits, stale) {
|
|
|
372
795
|
}
|
|
373
796
|
}
|
|
374
797
|
}
|
|
375
|
-
function firstHeadings(
|
|
798
|
+
function firstHeadings(reading) {
|
|
376
799
|
const first = new Map();
|
|
377
|
-
for (const span of
|
|
800
|
+
for (const span of sectionSpans(reading, 0)) {
|
|
378
801
|
if (!first.has(span.name))
|
|
379
802
|
first.set(span.name, span.headingStart);
|
|
380
803
|
}
|
|
381
804
|
return first;
|
|
382
805
|
}
|
|
383
|
-
function hidden(before,
|
|
384
|
-
const
|
|
806
|
+
function hidden(before, after, shifts) {
|
|
807
|
+
const placedAfter = firstHeadings(after);
|
|
385
808
|
let shift = 0;
|
|
386
809
|
let index = 0;
|
|
387
|
-
for (const [name, at] of firstHeadings(before
|
|
810
|
+
for (const [name, at] of firstHeadings(before)) {
|
|
388
811
|
while (index < shifts.length && shifts[index][0] <= at) {
|
|
389
812
|
shift += shifts[index][1];
|
|
390
813
|
index += 1;
|
|
391
814
|
}
|
|
392
|
-
if (
|
|
815
|
+
if (placedAfter.get(name) !== at + shift)
|
|
393
816
|
return name;
|
|
394
817
|
}
|
|
395
818
|
return undefined;
|
|
@@ -420,15 +843,186 @@ function spliced(source, edits) {
|
|
|
420
843
|
pieces.push(source.slice(cursor));
|
|
421
844
|
return { content: pieces.join(''), shifts };
|
|
422
845
|
}
|
|
423
|
-
|
|
846
|
+
function checkedReading(source, replaced) {
|
|
847
|
+
const checked = spliced(source, replaced);
|
|
848
|
+
return {
|
|
849
|
+
reading: bodyReading(checked.content, bodyStartOf(checked.content)),
|
|
850
|
+
shifts: checked.shifts,
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
function opening(change) {
|
|
854
|
+
return change.kind === 'section' && change.prior === undefined;
|
|
855
|
+
}
|
|
856
|
+
function unopenable(name, definition) {
|
|
857
|
+
const root = definition?.root;
|
|
858
|
+
const field = root === undefined
|
|
859
|
+
? undefined
|
|
860
|
+
: bodyOrder(root).find((key) => labelOf(key, root.properties?.[key]) === name.trim());
|
|
861
|
+
if (field !== undefined)
|
|
862
|
+
return refuse('', 'field-section', field);
|
|
863
|
+
if (read(`## ${name}`, 0).headings[0]?.name !== name)
|
|
864
|
+
return refuse('', 'unwritable-section', name);
|
|
865
|
+
return undefined;
|
|
866
|
+
}
|
|
867
|
+
function withSections(document, changes, definition, nameOf) {
|
|
868
|
+
const opened = [];
|
|
869
|
+
const held = [];
|
|
870
|
+
for (const [at, change] of changes.entries()) {
|
|
871
|
+
if (!opening(change))
|
|
872
|
+
continue;
|
|
873
|
+
const refusal = unopenable(change.name, definition);
|
|
874
|
+
if (refusal !== undefined)
|
|
875
|
+
return { outcome: 'refuse', refusal };
|
|
876
|
+
if (Object.hasOwn(document.sections, change.name) || opened.includes(change.name))
|
|
877
|
+
held.push(at);
|
|
878
|
+
else
|
|
879
|
+
opened.push(change.name);
|
|
880
|
+
}
|
|
881
|
+
const source = document.content;
|
|
882
|
+
const ending = lineEnding(source);
|
|
883
|
+
const lead = source === '' || source.endsWith('\n') ? '' : ending;
|
|
884
|
+
const skeleton = parse(`${source}${lead}${opened.map((name) => `## ${name}${ending}`).join('')}`, '', definition);
|
|
885
|
+
if (!skeleton.ok)
|
|
886
|
+
return { outcome: 'refuse', refusal: skeleton.refusal };
|
|
887
|
+
const kept = changes.flatMap((change, at) => held.includes(at) ? [] : [[opening(change) ? { ...change, prior: '' } : change, at]]);
|
|
888
|
+
const applied = apply(skeleton.document, kept.map(([change]) => change), definition, nameOf);
|
|
889
|
+
if (applied.outcome === 'refuse')
|
|
890
|
+
return applied;
|
|
891
|
+
const stale = [
|
|
892
|
+
...held,
|
|
893
|
+
...(applied.outcome === 'stale' ? applied.stale.map((at) => kept[at][1]) : []),
|
|
894
|
+
];
|
|
895
|
+
if (stale.length > 0)
|
|
896
|
+
return { outcome: 'stale', document, stale: stale.sort((left, right) => left - right) };
|
|
897
|
+
return applied;
|
|
898
|
+
}
|
|
899
|
+
function markdownIn(facts) {
|
|
900
|
+
if (facts === undefined || facts.json === true)
|
|
901
|
+
return false;
|
|
902
|
+
return (facts.markdown === true ||
|
|
903
|
+
Object.values(facts.properties ?? {}).some(markdownIn) ||
|
|
904
|
+
(facts.prefix ?? []).some(markdownIn) ||
|
|
905
|
+
markdownIn(facts.items) ||
|
|
906
|
+
markdownIn(facts.additional) ||
|
|
907
|
+
(facts.patterns ?? []).some((one) => markdownIn(one.facts)));
|
|
908
|
+
}
|
|
909
|
+
function lined(value) {
|
|
910
|
+
if (typeof value === 'string')
|
|
911
|
+
return lineFits(value);
|
|
912
|
+
if (Array.isArray(value))
|
|
913
|
+
return value.every(lined);
|
|
914
|
+
return isRecord(value) ? Object.values(value).every(lined) : true;
|
|
915
|
+
}
|
|
916
|
+
function fromValues(change, root) {
|
|
917
|
+
if (change.kind === 'reference')
|
|
918
|
+
return true;
|
|
919
|
+
return (change.kind === 'field' &&
|
|
920
|
+
(HOUSE_FIELDS.includes(change.name) ||
|
|
921
|
+
(!markdownIn(memberFacts(root, change.name)) && lined(change.value))));
|
|
922
|
+
}
|
|
923
|
+
function written(content, before, fields, changes, definition) {
|
|
924
|
+
const opening = opened(content, '');
|
|
925
|
+
if (!opening.ok)
|
|
926
|
+
throw new Error(`a document the MD model wrote breaks ${opening.refusal.rule}`);
|
|
927
|
+
const values = { ...before.values };
|
|
928
|
+
for (const { key, value } of fields)
|
|
929
|
+
if (value === undefined)
|
|
930
|
+
delete values[key];
|
|
931
|
+
else
|
|
932
|
+
values[key] = writtenValue(value, memberFacts(definition.root, key));
|
|
933
|
+
for (const change of changes)
|
|
934
|
+
if (change.kind === 'reference' && before.links[change.pointer] !== undefined) {
|
|
935
|
+
const [key, ...rest] = pointerSegments(change.pointer);
|
|
936
|
+
values[key] = repointed(values[key], rest, change.value);
|
|
937
|
+
}
|
|
938
|
+
return freezeDocument(content, declaredType(opening.fields), { ...opening.fields, ...values }, {
|
|
939
|
+
yaml: opening.yaml,
|
|
940
|
+
yamlEnd: opening.yamlEnd,
|
|
941
|
+
bodyStart: opening.yaml === null ? 0 : bodyStartAfterYaml(content, opening.yamlEnd),
|
|
942
|
+
}, definition);
|
|
943
|
+
}
|
|
944
|
+
function settled(before, after, changes, root) {
|
|
945
|
+
const set = new Map();
|
|
946
|
+
for (const change of changes)
|
|
947
|
+
if (change.kind === 'field' && !HOUSE_FIELDS.includes(change.name))
|
|
948
|
+
set.set(change.name, change.value);
|
|
949
|
+
for (const [key, value] of set)
|
|
950
|
+
if (!equalValue(ownField(after, key), writtenValue(value, memberFacts(root, key))))
|
|
951
|
+
return refuse('', 'malformed-value', `/${escapePointer(key)}`);
|
|
952
|
+
if (!changes.some((change) => change.kind === 'section' || change.kind === 'preamble' || change.kind === 'text'))
|
|
953
|
+
return undefined;
|
|
954
|
+
for (const key of new Set([...Object.keys(before.fields), ...Object.keys(after.fields)])) {
|
|
955
|
+
if (set.has(key) || HOUSE_FIELDS.includes(key))
|
|
956
|
+
continue;
|
|
957
|
+
if (!equalValue(ownField(before, key), ownField(after, key)))
|
|
958
|
+
return refuse('', 'overlapping-section', `/${escapePointer(key)}`);
|
|
959
|
+
}
|
|
960
|
+
return undefined;
|
|
961
|
+
}
|
|
962
|
+
export function apply(document, changes, definition, nameOf) {
|
|
963
|
+
if (changes.some(opening))
|
|
964
|
+
return withSections(document, changes, definition, nameOf);
|
|
424
965
|
const internal = internalOf(document);
|
|
425
966
|
if (!('bodyStart' in internal))
|
|
426
967
|
return { outcome: 'refuse', refusal: internal };
|
|
427
|
-
const
|
|
968
|
+
const body = bodyWith(document, definition);
|
|
969
|
+
if (body !== null && !('top' in body))
|
|
970
|
+
return { outcome: 'refuse', refusal: body };
|
|
971
|
+
const plan = planner(document, internal, body, definition);
|
|
428
972
|
const edits = [];
|
|
429
973
|
const stale = new Set();
|
|
430
974
|
const named = new Set();
|
|
975
|
+
const fields = [];
|
|
976
|
+
const moved = new Set();
|
|
977
|
+
let text;
|
|
431
978
|
for (const [at, change] of changes.entries()) {
|
|
979
|
+
const part = partOf(change);
|
|
980
|
+
if (named.has(part)) {
|
|
981
|
+
stale.add(at);
|
|
982
|
+
continue;
|
|
983
|
+
}
|
|
984
|
+
if (change.kind === 'reference' && body !== null && body.links[change.pointer] === undefined) {
|
|
985
|
+
if (pointed(document.fields, change.pointer) !== change.prior) {
|
|
986
|
+
stale.add(at);
|
|
987
|
+
continue;
|
|
988
|
+
}
|
|
989
|
+
const [key, ...rest] = pointerSegments(change.pointer);
|
|
990
|
+
const whole = `field ${key}`;
|
|
991
|
+
const held = moved.has(key) ? fields.findIndex((field) => field.key === key) : -1;
|
|
992
|
+
if (named.has(whole) && held === -1) {
|
|
993
|
+
stale.add(at);
|
|
994
|
+
continue;
|
|
995
|
+
}
|
|
996
|
+
moved.add(key);
|
|
997
|
+
if (held === -1)
|
|
998
|
+
fields.push({ key: key, value: repointed(document.fields[key], rest, change.value), at });
|
|
999
|
+
else
|
|
1000
|
+
fields[held] = {
|
|
1001
|
+
...fields[held],
|
|
1002
|
+
value: repointed(fields[held].value, rest, change.value),
|
|
1003
|
+
};
|
|
1004
|
+
named.add(part);
|
|
1005
|
+
named.add(whole);
|
|
1006
|
+
continue;
|
|
1007
|
+
}
|
|
1008
|
+
if (inBody(change, body)) {
|
|
1009
|
+
if (change.kind === 'field') {
|
|
1010
|
+
if (!equalValue(ownField(document, change.name), change.prior)) {
|
|
1011
|
+
stale.add(at);
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
fields.push({ key: change.name, value: change.value, at });
|
|
1015
|
+
}
|
|
1016
|
+
else if (change.kind === 'text') {
|
|
1017
|
+
if (document.text !== change.prior) {
|
|
1018
|
+
stale.add(at);
|
|
1019
|
+
continue;
|
|
1020
|
+
}
|
|
1021
|
+
text = { value: change.value, at };
|
|
1022
|
+
}
|
|
1023
|
+
named.add(part);
|
|
1024
|
+
continue;
|
|
1025
|
+
}
|
|
432
1026
|
const planned = plan(change, at);
|
|
433
1027
|
if (planned === 'stale') {
|
|
434
1028
|
stale.add(at);
|
|
@@ -436,41 +1030,67 @@ export function apply(document, changes) {
|
|
|
436
1030
|
}
|
|
437
1031
|
if (!Array.isArray(planned))
|
|
438
1032
|
return { outcome: 'refuse', refusal: planned };
|
|
439
|
-
const part = partOf(change);
|
|
440
|
-
if (named.has(part)) {
|
|
441
|
-
stale.add(at);
|
|
442
|
-
continue;
|
|
443
|
-
}
|
|
444
1033
|
named.add(part);
|
|
445
1034
|
for (const edit of planned)
|
|
446
1035
|
edits.push(edit);
|
|
447
1036
|
}
|
|
1037
|
+
if (body !== null && (fields.length > 0 || text !== undefined)) {
|
|
1038
|
+
const planned = bodyPlan(document.content, internal, body, definition, fields, text, nameOf);
|
|
1039
|
+
if (!Array.isArray(planned))
|
|
1040
|
+
return { outcome: 'refuse', refusal: planned };
|
|
1041
|
+
edits.push(...planned);
|
|
1042
|
+
}
|
|
448
1043
|
edits.sort((left, right) => left.start - right.start || left.end - right.end || left.at - right.at);
|
|
449
1044
|
overlapping(edits, stale);
|
|
450
1045
|
if (stale.size > 0)
|
|
451
1046
|
return { outcome: 'stale', document, stale: [...stale].sort((left, right) => left - right) };
|
|
452
1047
|
const source = document.content;
|
|
453
|
-
const { content } = spliced(source, edits);
|
|
1048
|
+
const { content, shifts } = spliced(source, edits);
|
|
454
1049
|
if (content === source)
|
|
455
1050
|
return { outcome: 'applied', document };
|
|
1051
|
+
if (body !== null && changes.every((change) => fromValues(change, definition.root)))
|
|
1052
|
+
return {
|
|
1053
|
+
outcome: 'applied',
|
|
1054
|
+
document: written(content, body, fields, changes, definition),
|
|
1055
|
+
};
|
|
1056
|
+
const parsed = parse(content, '', definition);
|
|
1057
|
+
if (!parsed.ok)
|
|
1058
|
+
return { outcome: 'refuse', refusal: parsed.refusal };
|
|
1059
|
+
const unsettled = body === null ? undefined : settled(document, parsed.document, changes, definition.root);
|
|
1060
|
+
if (unsettled !== undefined)
|
|
1061
|
+
return { outcome: 'refuse', refusal: unsettled };
|
|
456
1062
|
const replaced = edits.filter((edit) => {
|
|
457
1063
|
const kind = changes[edit.at].kind;
|
|
458
1064
|
return kind === 'section' || kind === 'preamble';
|
|
459
1065
|
});
|
|
460
1066
|
if (replaced.length > 0) {
|
|
461
|
-
const checked =
|
|
462
|
-
|
|
1067
|
+
const checked = replaced.length === edits.length
|
|
1068
|
+
? { reading: readingOf(parsed.document), shifts }
|
|
1069
|
+
: checkedReading(source, replaced);
|
|
1070
|
+
const name = hidden(readingOf(document), checked.reading, checked.shifts);
|
|
463
1071
|
if (name !== undefined)
|
|
464
1072
|
return { outcome: 'refuse', refusal: refuse('', 'overlapping-section', name) };
|
|
465
1073
|
}
|
|
466
|
-
const parsed = parse(content);
|
|
467
|
-
if (!parsed.ok)
|
|
468
|
-
return { outcome: 'refuse', refusal: parsed.refusal };
|
|
469
1074
|
return { outcome: 'applied', document: parsed.document };
|
|
470
1075
|
}
|
|
1076
|
+
export function relabel(document, before, after) {
|
|
1077
|
+
const body = bodyWith(document, before);
|
|
1078
|
+
if (body === null || !('top' in body))
|
|
1079
|
+
return document.content;
|
|
1080
|
+
let content = '';
|
|
1081
|
+
let cursor = 0;
|
|
1082
|
+
for (const label of [...body.labels].sort((left, right) => left.start - right.start)) {
|
|
1083
|
+
const next = labelAt(after.root, label.path);
|
|
1084
|
+
if (next === undefined || next === labelAt(before.root, label.path))
|
|
1085
|
+
continue;
|
|
1086
|
+
content += document.content.slice(cursor, label.start) + writtenLabel(next, label.written);
|
|
1087
|
+
cursor = label.end;
|
|
1088
|
+
}
|
|
1089
|
+
return content + document.content.slice(cursor);
|
|
1090
|
+
}
|
|
1091
|
+
export function relabels(before, after) {
|
|
1092
|
+
return JSON.stringify([...labelsIn(before.root)]) !== JSON.stringify([...labelsIn(after.root)]);
|
|
1093
|
+
}
|
|
471
1094
|
export function serialize(document) {
|
|
472
1095
|
return document.content;
|
|
473
1096
|
}
|
|
474
|
-
export function frontmatter(document) {
|
|
475
|
-
return heldOf(document)?.internal.root ?? null;
|
|
476
|
-
}
|