@agentshouse/mdmodel 0.1.0-ccf1367190878 → 0.1.0-cd5ace6e0b27a
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 +232 -182
- package/README.md +21 -22
- package/dist/definition.d.ts +18 -1
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +176 -48
- package/dist/divide.d.ts.map +1 -1
- package/dist/divide.js +32 -21
- package/dist/document.d.ts +10 -6
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +424 -207
- package/dist/event.d.ts +78 -0
- package/dist/event.d.ts.map +1 -0
- package/dist/event.js +201 -0
- package/dist/facts.d.ts +15 -0
- package/dist/facts.d.ts.map +1 -0
- package/dist/facts.js +41 -0
- package/dist/form.d.ts +4 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/form.js +139 -0
- package/dist/index.d.ts +25 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -8
- 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 +51 -0
- package/dist/markdown.d.ts.map +1 -0
- package/dist/markdown.js +271 -0
- package/dist/paths.d.ts +18 -2
- package/dist/paths.d.ts.map +1 -1
- package/dist/paths.js +69 -15
- 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 +1 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +44 -26
- 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.map +1 -1
- package/dist/references.js +6 -1
- package/dist/reserved.d.ts +6 -0
- package/dist/reserved.d.ts.map +1 -0
- package/dist/reserved.js +21 -0
- package/dist/schema.d.ts +1 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +55 -4
- package/dist/types.d.ts +46 -5
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +31 -4
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +387 -129
- package/dist/yaml.d.ts.map +1 -1
- package/dist/yaml.js +40 -14
- package/fixtures/README.md +34 -7
- package/fixtures/cases/conditional-section/context.json +1 -0
- package/fixtures/cases/conditional-section/expected.json +6 -0
- package/fixtures/cases/conditional-section/library/questions/hosting.md +9 -0
- package/fixtures/cases/conditional-section/library/questions/payments.md +12 -0
- package/fixtures/cases/conditional-section/schema.yaml +14 -0
- package/fixtures/cases/cross-room-link/context.json +1 -0
- package/fixtures/cases/cross-room-link/expected.json +1 -0
- package/fixtures/cases/cross-room-link/library/clients/acme.md +6 -0
- package/fixtures/cases/cross-room-link/schema.yaml +3 -0
- package/fixtures/cases/cross-room-reference/context.json +1 -0
- package/fixtures/cases/cross-room-reference/expected.json +1 -0
- package/fixtures/cases/cross-room-reference/library/clients/acme.md +9 -0
- package/fixtures/cases/cross-room-reference/schema.yaml +9 -0
- package/fixtures/cases/equal-labels/context.json +1 -0
- package/fixtures/cases/equal-labels/expected.json +5 -0
- package/fixtures/cases/equal-labels/schema.yaml +4 -0
- package/fixtures/cases/labels-in-the-rooms-language/context.json +1 -0
- package/fixtures/cases/labels-in-the-rooms-language/expected.json +1 -0
- package/fixtures/cases/labels-in-the-rooms-language/library/deals/atlas.md +6 -0
- package/fixtures/cases/labels-in-the-rooms-language/schema.yaml +9 -0
- package/fixtures/cases/lazy-list-item/context.json +1 -0
- package/fixtures/cases/lazy-list-item/expected.json +3 -0
- package/fixtures/cases/lazy-list-item/library/item.md +8 -0
- package/fixtures/cases/lazy-list-item/schema.yaml +3 -0
- package/fixtures/cases/link-into-a-reserved-name/context.json +1 -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/context.json +1 -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/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/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/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/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/reference-into-a-reserved-name/context.json +1 -0
- package/fixtures/cases/reference-into-a-reserved-name/expected.json +6 -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/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/thematic-break-in-a-list-section/context.json +1 -0
- package/fixtures/cases/thematic-break-in-a-list-section/expected.json +6 -0
- package/fixtures/cases/thematic-break-in-a-list-section/library/item.md +7 -0
- package/fixtures/cases/thematic-break-in-a-list-section/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/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 +4 -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 +6 -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 +5 -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/division.json +73 -28
- package/fixtures/preamble.json +24 -3
- package/fixtures/projection.json +105 -2
- 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 -76
- package/dist/condition.d.ts +0 -149
- package/dist/condition.d.ts.map +0 -1
- package/dist/condition.js +0 -500
- package/dist/links.d.ts +0 -27
- package/dist/links.d.ts.map +0 -1
- package/dist/links.js +0 -774
- 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/dist/document.js
CHANGED
|
@@ -1,29 +1,62 @@
|
|
|
1
1
|
import { ARRAY_INDEX, pointerSegments } from './definition.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { TYPE_FIELD } from './fields.js';
|
|
3
|
+
import { destinations, hrefOccurrences, read, sectionSpans, sectionsRecord, } from './markdown.js';
|
|
4
|
+
import { destinationPath, relative, retarget, roomPath, roomTarget, SHELL_ROOMS, shellTarget, target, } from './paths.js';
|
|
4
5
|
import { references } from './references.js';
|
|
5
|
-
import { headingSpans, sectionsRecord } from './sections.js';
|
|
6
6
|
import { refusal } from './refuse.js';
|
|
7
7
|
import { bodyStartAfterYaml, bodyStartOf, equalValue, extractFrontmatter, frontmatterValue, jsValue, lineEnding, mapEntry, parseYamlMapping, serializeJs, serializeKey, YamlError, } from './yaml.js';
|
|
8
|
-
const
|
|
8
|
+
const HELD = Symbol('held');
|
|
9
|
+
function heldOf(document) {
|
|
10
|
+
return document[HELD];
|
|
11
|
+
}
|
|
12
|
+
function bodyReading(content, bodyStart) {
|
|
13
|
+
return read(content.slice(bodyStart), bodyStart);
|
|
14
|
+
}
|
|
15
|
+
function heldReading(content, held) {
|
|
16
|
+
held.reading ??= bodyReading(content, held.internal.bodyStart);
|
|
17
|
+
return held.reading;
|
|
18
|
+
}
|
|
19
|
+
export function readingOf(document) {
|
|
20
|
+
const held = heldOf(document);
|
|
21
|
+
return held === undefined
|
|
22
|
+
? bodyReading(document.content, bodyStartOf(document.content))
|
|
23
|
+
: heldReading(document.content, held);
|
|
24
|
+
}
|
|
25
|
+
export function linkDestinations(document) {
|
|
26
|
+
const bodyStart = heldOf(document)?.internal.bodyStart ?? bodyStartOf(document.content);
|
|
27
|
+
return document.content.includes('[', bodyStart) ? destinations(readingOf(document)) : [];
|
|
28
|
+
}
|
|
29
|
+
function heldParts(content, held) {
|
|
30
|
+
if (held.parts !== undefined)
|
|
31
|
+
return held.parts;
|
|
32
|
+
const spans = sectionSpans(heldReading(content, held), content.length);
|
|
33
|
+
held.parts = {
|
|
34
|
+
preamble: content.slice(held.internal.bodyStart, spans[0]?.headingStart),
|
|
35
|
+
sections: Object.freeze(sectionsRecord(spans, content)),
|
|
36
|
+
};
|
|
37
|
+
return held.parts;
|
|
38
|
+
}
|
|
39
|
+
function bodyStartIn(document) {
|
|
40
|
+
return heldOf(document)?.internal.bodyStart ?? bodyStartOf(document.content);
|
|
41
|
+
}
|
|
42
|
+
export function textSpans(document) {
|
|
43
|
+
const start = bodyStartIn(document);
|
|
44
|
+
return [{ start, end: document.content.length, text: document.content.slice(start) }];
|
|
45
|
+
}
|
|
9
46
|
function refuse(file, rule, location) {
|
|
10
47
|
return location === undefined ? refusal(file, rule) : refusal(file, rule, { location });
|
|
11
48
|
}
|
|
12
|
-
function freezeDocument(content, type, fields,
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
});
|
|
20
|
-
internals.set(document, internal);
|
|
21
|
-
return document;
|
|
49
|
+
function freezeDocument(content, type, fields, internal) {
|
|
50
|
+
const held = { internal };
|
|
51
|
+
return Object.freeze(Object.defineProperties({ content, type, fields: Object.freeze(fields) }, {
|
|
52
|
+
preamble: { enumerable: true, get: () => heldParts(content, held).preamble },
|
|
53
|
+
sections: { enumerable: true, get: () => heldParts(content, held).sections },
|
|
54
|
+
text: { enumerable: true, get: () => content.slice(internal.bodyStart) },
|
|
55
|
+
[HELD]: { value: held },
|
|
56
|
+
}));
|
|
22
57
|
}
|
|
23
58
|
function declaredType(fields) {
|
|
24
59
|
const value = fields.type;
|
|
25
|
-
if (value === undefined || value === null)
|
|
26
|
-
return undefined;
|
|
27
60
|
if (typeof value !== 'string' || value === '')
|
|
28
61
|
return undefined;
|
|
29
62
|
return value;
|
|
@@ -37,9 +70,6 @@ function opened(content, file) {
|
|
|
37
70
|
try {
|
|
38
71
|
const root = parseYamlMapping(content, fence.yamlStart, fence.yamlEnd);
|
|
39
72
|
const fields = jsValue(root);
|
|
40
|
-
if (fields === null || typeof fields !== 'object' || Array.isArray(fields)) {
|
|
41
|
-
return { ok: false, refusal: refuse(file, 'frontmatter-fence') };
|
|
42
|
-
}
|
|
43
73
|
return { ok: true, root, fields: fields, yamlEnd: fence.yamlEnd };
|
|
44
74
|
}
|
|
45
75
|
catch (error) {
|
|
@@ -49,108 +79,37 @@ function opened(content, file) {
|
|
|
49
79
|
}
|
|
50
80
|
}
|
|
51
81
|
export function parseFrontmatter(content, file = '') {
|
|
52
|
-
const
|
|
53
|
-
if (!
|
|
54
|
-
return
|
|
82
|
+
const opening = opened(content, file);
|
|
83
|
+
if (!opening.ok)
|
|
84
|
+
return opening;
|
|
55
85
|
return {
|
|
56
86
|
ok: true,
|
|
57
87
|
frontmatter: Object.freeze({
|
|
58
88
|
content,
|
|
59
|
-
type: declaredType(
|
|
60
|
-
fields: Object.freeze(
|
|
89
|
+
type: declaredType(opening.fields),
|
|
90
|
+
fields: Object.freeze(opening.fields),
|
|
61
91
|
}),
|
|
62
92
|
};
|
|
63
93
|
}
|
|
64
|
-
export function parse(content, file = '') {
|
|
65
|
-
const
|
|
66
|
-
if (!
|
|
67
|
-
return
|
|
68
|
-
const bodyStart =
|
|
69
|
-
const spans = headingSpans(content, bodyStart);
|
|
94
|
+
export function parse(content, file = '', definition) {
|
|
95
|
+
const opening = opened(content, file);
|
|
96
|
+
if (!opening.ok)
|
|
97
|
+
return opening;
|
|
98
|
+
const bodyStart = opening.root === null ? 0 : bodyStartAfterYaml(content, opening.yamlEnd);
|
|
70
99
|
return {
|
|
71
100
|
ok: true,
|
|
72
|
-
document: freezeDocument(content, declaredType(
|
|
101
|
+
document: freezeDocument(content, declaredType(opening.fields), opening.fields, {
|
|
102
|
+
root: opening.root,
|
|
103
|
+
yamlEnd: opening.yamlEnd,
|
|
104
|
+
bodyStart,
|
|
105
|
+
}),
|
|
73
106
|
};
|
|
74
107
|
}
|
|
75
|
-
function
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
function applyField(document, name, value, prior) {
|
|
79
|
-
const internal = internals.get(document);
|
|
80
|
-
if (internal === undefined || internal.root === null) {
|
|
81
|
-
return { outcome: 'refuse', refusal: refuse('', 'frontmatter-fence', name) };
|
|
82
|
-
}
|
|
83
|
-
if (!equalValue(document.fields[name], prior)) {
|
|
84
|
-
return { outcome: 'stale', document };
|
|
85
|
-
}
|
|
86
|
-
if (!frontmatterValue(value)) {
|
|
87
|
-
return { outcome: 'refuse', refusal: refuse('', 'frontmatter-fence', name) };
|
|
88
|
-
}
|
|
89
|
-
let nextContent;
|
|
90
|
-
const existing = mapEntry(internal.root, name);
|
|
91
|
-
if (existing !== undefined) {
|
|
92
|
-
const held = existing.value;
|
|
93
|
-
const inline = held.start > existing.after &&
|
|
94
|
-
!/[\r\n]/.test(document.content.slice(existing.after, held.start));
|
|
95
|
-
nextContent = inline
|
|
96
|
-
? splice(document.content, held.start, held.end, serializeJs(value))
|
|
97
|
-
: splice(document.content, existing.after, held.end, ` ${serializeJs(value)}`);
|
|
98
|
-
}
|
|
99
|
-
else if (document.content[internal.root.start] === '{') {
|
|
100
|
-
nextContent = splice(document.content, internal.root.end - 1, internal.root.end - 1, `${internal.root.entries.length === 0 ? '' : ', '}${serializeKey(name)}: ${serializeJs(value)}`);
|
|
101
|
-
}
|
|
102
|
-
else {
|
|
103
|
-
const nl = lineEnding(document.content);
|
|
104
|
-
const lineStart = document.content.lastIndexOf('\n', internal.root.start - 1) + 1;
|
|
105
|
-
const indent = document.content.slice(lineStart, internal.root.start);
|
|
106
|
-
const line = `${indent}${serializeKey(name)}: ${serializeJs(value)}${nl}`;
|
|
107
|
-
const before = document.content.slice(0, internal.yamlEnd);
|
|
108
|
-
const prefix = before.endsWith('\n') || before.endsWith('\r\n') ? '' : nl;
|
|
109
|
-
nextContent = splice(document.content, internal.yamlEnd, internal.yamlEnd, `${prefix}${line}`);
|
|
110
|
-
}
|
|
111
|
-
const parsed = parse(nextContent);
|
|
108
|
+
export function create(type, definition) {
|
|
109
|
+
const parsed = parse(type === undefined ? '---\n---\n' : `---\n${TYPE_FIELD}: ${serializeJs(type)}\n---\n`, '', definition);
|
|
112
110
|
if (!parsed.ok)
|
|
113
|
-
|
|
114
|
-
return
|
|
115
|
-
}
|
|
116
|
-
function applySection(document, name, value, prior) {
|
|
117
|
-
const internal = internals.get(document);
|
|
118
|
-
if (internal === undefined)
|
|
119
|
-
return { outcome: 'refuse', refusal: refuse('', 'required-section', name) };
|
|
120
|
-
const spans = headingSpans(document.content, internal.bodyStart);
|
|
121
|
-
const span = spans.find((item) => item.name === name);
|
|
122
|
-
if (span === undefined)
|
|
123
|
-
return { outcome: 'refuse', refusal: refuse('', 'required-section', name) };
|
|
124
|
-
const current = document.content.slice(span.bodyStart, span.bodyEnd);
|
|
125
|
-
if (current !== prior)
|
|
126
|
-
return { outcome: 'stale', document };
|
|
127
|
-
return replaced(document, internal.bodyStart, span.bodyStart, span.bodyEnd, value);
|
|
128
|
-
}
|
|
129
|
-
function applyPreamble(document, value, prior) {
|
|
130
|
-
if (document.preamble !== prior)
|
|
131
|
-
return { outcome: 'stale', document };
|
|
132
|
-
const start = bodyStartOf(document.content);
|
|
133
|
-
return replaced(document, start, start, start + document.preamble.length, value);
|
|
134
|
-
}
|
|
135
|
-
function firstHeadings(content, bodyStart) {
|
|
136
|
-
const first = new Map();
|
|
137
|
-
for (const span of headingSpans(content, bodyStart)) {
|
|
138
|
-
if (!first.has(span.name))
|
|
139
|
-
first.set(span.name, span.headingStart);
|
|
140
|
-
}
|
|
141
|
-
return first;
|
|
142
|
-
}
|
|
143
|
-
function replaced(document, bodyStart, start, end, value) {
|
|
144
|
-
const content = splice(document.content, start, end, value);
|
|
145
|
-
const after = firstHeadings(content, bodyStartOf(content));
|
|
146
|
-
for (const [name, at] of firstHeadings(document.content, bodyStart)) {
|
|
147
|
-
if (after.get(name) !== (at < start ? at : at + value.length - (end - start)))
|
|
148
|
-
return { outcome: 'refuse', refusal: refuse('', 'overlapping-section', name) };
|
|
149
|
-
}
|
|
150
|
-
const parsed = parse(content);
|
|
151
|
-
if (!parsed.ok)
|
|
152
|
-
return { outcome: 'refuse', refusal: parsed.refusal };
|
|
153
|
-
return { outcome: 'applied', document: parsed.document };
|
|
111
|
+
throw new Error(`a document of type ${type} does not read`);
|
|
112
|
+
return parsed.document;
|
|
154
113
|
}
|
|
155
114
|
function fitsBare(to) {
|
|
156
115
|
if (to === '' || to.startsWith('<'))
|
|
@@ -179,58 +138,34 @@ function destinationText(to, bracketed) {
|
|
|
179
138
|
return `<${to}>`;
|
|
180
139
|
return null;
|
|
181
140
|
}
|
|
182
|
-
function
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
for (const match of hrefOccurrences(content.slice(bodyStart))) {
|
|
187
|
-
if (match.href !== href)
|
|
188
|
-
continue;
|
|
189
|
-
const text = destinationText(written, match.bracketed);
|
|
190
|
-
if (text === null)
|
|
191
|
-
return refuse('', 'unwritable-link', written);
|
|
192
|
-
rewritten += content.slice(cursor, bodyStart + match.start);
|
|
193
|
-
rewritten += text;
|
|
194
|
-
cursor = bodyStart + match.end;
|
|
195
|
-
}
|
|
196
|
-
return rewritten + content.slice(cursor);
|
|
197
|
-
}
|
|
198
|
-
function shellRelinked(content, bodyStart, file, handle) {
|
|
199
|
-
const hrefs = new Set(hrefOccurrences(content.slice(bodyStart)).map(({ href }) => href));
|
|
200
|
-
let held = content;
|
|
201
|
-
for (const href of hrefs) {
|
|
141
|
+
function shellRelinked(body, file, handle) {
|
|
142
|
+
const occurrences = hrefOccurrences(body);
|
|
143
|
+
const written = new Map();
|
|
144
|
+
for (const href of new Set(occurrences.map((occurrence) => occurrence.href))) {
|
|
202
145
|
const path = shellTarget(href, handle);
|
|
203
146
|
if (path === null)
|
|
204
147
|
continue;
|
|
205
|
-
const
|
|
206
|
-
if (
|
|
207
|
-
return
|
|
208
|
-
|
|
148
|
+
const to = retarget(href, relative(file, path));
|
|
149
|
+
if (destinationText(to, false) === null)
|
|
150
|
+
return refuse('', 'unwritable-link', to);
|
|
151
|
+
written.set(href, to);
|
|
209
152
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
return { outcome: 'applied', document: parsed.document };
|
|
221
|
-
}
|
|
222
|
-
function applyLink(document, href, to) {
|
|
223
|
-
const content = document.content;
|
|
224
|
-
const bodyStart = bodyStartOf(content);
|
|
225
|
-
if (!hrefOccurrences(content.slice(bodyStart)).some((occurrence) => occurrence.href === href))
|
|
226
|
-
return { outcome: 'stale', document };
|
|
227
|
-
return reparsed(document, relinked(content, bodyStart, href, to));
|
|
153
|
+
let rewritten = '';
|
|
154
|
+
let cursor = 0;
|
|
155
|
+
for (const match of occurrences) {
|
|
156
|
+
const to = written.get(match.href);
|
|
157
|
+
if (to === undefined)
|
|
158
|
+
continue;
|
|
159
|
+
rewritten += body.slice(cursor, match.start) + destinationText(to, match.bracketed);
|
|
160
|
+
cursor = match.end;
|
|
161
|
+
}
|
|
162
|
+
return rewritten + body.slice(cursor);
|
|
228
163
|
}
|
|
229
|
-
function nodeAt(root, pointer) {
|
|
164
|
+
function nodeAt(root, pointer, entryOf = mapEntry) {
|
|
230
165
|
let node = root;
|
|
231
166
|
for (const key of pointerSegments(pointer)) {
|
|
232
167
|
if (node?.kind === 'map')
|
|
233
|
-
node =
|
|
168
|
+
node = entryOf(node, key)?.value;
|
|
234
169
|
else if (node?.kind === 'seq' && ARRAY_INDEX.test(key))
|
|
235
170
|
node = node.items[Number(key)];
|
|
236
171
|
else
|
|
@@ -245,37 +180,36 @@ function scalarText(source, value) {
|
|
|
245
180
|
return JSON.stringify(value);
|
|
246
181
|
return serializeJs(value);
|
|
247
182
|
}
|
|
248
|
-
function
|
|
249
|
-
const
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
const parsed = parse(splice(document.content, node.start, node.end, text));
|
|
255
|
-
if (!parsed.ok)
|
|
256
|
-
return { outcome: 'refuse', refusal: parsed.refusal };
|
|
257
|
-
return { outcome: 'applied', document: parsed.document };
|
|
258
|
-
}
|
|
259
|
-
export function relativize(document, file, handle, definition) {
|
|
260
|
-
let held = document;
|
|
183
|
+
export function relativize(document, file, handle, definition, kept = new Set()) {
|
|
184
|
+
const relativized = (href) => {
|
|
185
|
+
const path = shellTarget(href, handle);
|
|
186
|
+
return path === null || kept.has(`${SHELL_ROOMS}${handle}/${path}`) ? null : path;
|
|
187
|
+
};
|
|
188
|
+
const changes = [];
|
|
261
189
|
for (const { pointer, value } of definition === undefined
|
|
262
190
|
? []
|
|
263
191
|
: references(document, definition)) {
|
|
264
|
-
const path =
|
|
265
|
-
if (path
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
192
|
+
const path = relativized(value);
|
|
193
|
+
if (path !== null)
|
|
194
|
+
changes.push({
|
|
195
|
+
kind: 'reference',
|
|
196
|
+
pointer,
|
|
197
|
+
value: retarget(value, relative(file, path)),
|
|
198
|
+
prior: value,
|
|
199
|
+
});
|
|
271
200
|
}
|
|
272
|
-
|
|
201
|
+
for (const href of new Set(linkDestinations(document).map(({ href }) => href))) {
|
|
202
|
+
const path = relativized(href);
|
|
203
|
+
if (path !== null)
|
|
204
|
+
changes.push({ kind: 'link', href, to: relative(file, path) });
|
|
205
|
+
}
|
|
206
|
+
return apply(document, changes, definition);
|
|
273
207
|
}
|
|
274
208
|
export function relativizeBody(body, file, handle) {
|
|
275
|
-
const relinkedBody = shellRelinked(body,
|
|
209
|
+
const relinkedBody = shellRelinked(body, file, handle);
|
|
276
210
|
if (typeof relinkedBody !== 'string')
|
|
277
211
|
return { outcome: 'refuse', refusal: relinkedBody };
|
|
278
|
-
const elsewhere = hrefOccurrences(relinkedBody).find(({ href }) => destinationPath(href)?.startsWith(SHELL_ROOMS) === true);
|
|
212
|
+
const elsewhere = hrefOccurrences(relinkedBody).find(({ href }) => destinationPath(href)?.startsWith(SHELL_ROOMS) === true && roomTarget(href) === null);
|
|
279
213
|
if (elsewhere !== undefined)
|
|
280
214
|
return { outcome: 'refuse', refusal: refuse(file, 'unresolved-link', elsewhere.href) };
|
|
281
215
|
return { outcome: 'applied', body: relinkedBody };
|
|
@@ -284,11 +218,11 @@ export function relinkBody(body, file, moves) {
|
|
|
284
218
|
let rewritten = '';
|
|
285
219
|
let cursor = 0;
|
|
286
220
|
for (const match of hrefOccurrences(body)) {
|
|
287
|
-
const from = target(file, match.href);
|
|
221
|
+
const from = target(file, match.href) ?? roomPath(match.href);
|
|
288
222
|
const to = from === null ? undefined : moves.get(from);
|
|
289
223
|
if (to === undefined)
|
|
290
224
|
continue;
|
|
291
|
-
const written = retarget(match.href, relative(file, to));
|
|
225
|
+
const written = retarget(match.href, from.startsWith(SHELL_ROOMS) || to.startsWith(SHELL_ROOMS) ? to : relative(file, to));
|
|
292
226
|
const text = destinationText(written, match.bracketed);
|
|
293
227
|
if (text === null)
|
|
294
228
|
return { outcome: 'refuse', refusal: refuse(file, 'unwritable-link', written) };
|
|
@@ -297,54 +231,337 @@ export function relinkBody(body, file, moves) {
|
|
|
297
231
|
}
|
|
298
232
|
return { outcome: 'applied', body: rewritten + body.slice(cursor) };
|
|
299
233
|
}
|
|
300
|
-
function
|
|
301
|
-
const
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
234
|
+
export function unlinkBody(body, hrefs) {
|
|
235
|
+
const { uses, definitions } = read(body);
|
|
236
|
+
const links = new Map();
|
|
237
|
+
for (const use of uses) {
|
|
238
|
+
const { href, bracketed } = use.destination;
|
|
239
|
+
if (!hrefs.has(href) || links.has(href))
|
|
240
|
+
continue;
|
|
241
|
+
const label = body.slice(use.labelStart, use.labelEnd);
|
|
242
|
+
const image = body[use.start] === '!' ? '!' : '';
|
|
243
|
+
links.set(href, `${image}[${label}](${destinationText(href, bracketed) ?? href})`);
|
|
306
244
|
}
|
|
307
|
-
|
|
245
|
+
const cut = [...uses, ...definitions]
|
|
246
|
+
.filter((span) => hrefs.has(span.destination.href))
|
|
247
|
+
.sort((left, right) => left.start - right.start);
|
|
248
|
+
let kept = '';
|
|
249
|
+
let cursor = 0;
|
|
250
|
+
for (const span of cut) {
|
|
251
|
+
if (span.start < cursor)
|
|
252
|
+
continue;
|
|
253
|
+
kept += body.slice(cursor, span.start);
|
|
254
|
+
cursor = span.end;
|
|
255
|
+
}
|
|
256
|
+
return { body: kept + body.slice(cursor), links };
|
|
308
257
|
}
|
|
309
|
-
function linksIn(
|
|
258
|
+
function linksIn(occurrences, file) {
|
|
310
259
|
const found = [];
|
|
311
260
|
const seen = new Set();
|
|
312
|
-
for (const occurrence of
|
|
261
|
+
for (const occurrence of occurrences) {
|
|
313
262
|
if (seen.has(occurrence.href))
|
|
314
263
|
continue;
|
|
264
|
+
const resolved = target(file, occurrence.href);
|
|
265
|
+
if (resolved === null)
|
|
266
|
+
continue;
|
|
315
267
|
seen.add(occurrence.href);
|
|
316
|
-
found.push({ href: occurrence.href, target:
|
|
268
|
+
found.push({ href: occurrence.href, target: resolved });
|
|
317
269
|
}
|
|
318
270
|
return found;
|
|
319
271
|
}
|
|
320
272
|
export function links(document, file) {
|
|
321
|
-
return linksIn(
|
|
322
|
-
}
|
|
323
|
-
export function linkOccurrences(document, file) {
|
|
324
|
-
const bodyStart = bodyStartOf(document.content);
|
|
325
|
-
return occurrencesIn(document.content.slice(bodyStart), file, bodyStart);
|
|
273
|
+
return linksIn(linkDestinations(document), file);
|
|
326
274
|
}
|
|
327
275
|
export function bodyLinks(body, file) {
|
|
328
|
-
return linksIn(body, file);
|
|
276
|
+
return linksIn(hrefOccurrences(body), file);
|
|
277
|
+
}
|
|
278
|
+
function internalOf(document) {
|
|
279
|
+
const held = heldOf(document);
|
|
280
|
+
if (held !== undefined)
|
|
281
|
+
return held.internal;
|
|
282
|
+
const opening = opened(document.content, '');
|
|
283
|
+
if (!opening.ok)
|
|
284
|
+
return opening.refusal;
|
|
285
|
+
return {
|
|
286
|
+
root: opening.root,
|
|
287
|
+
yamlEnd: opening.yamlEnd,
|
|
288
|
+
bodyStart: opening.root === null ? 0 : bodyStartAfterYaml(document.content, opening.yamlEnd),
|
|
289
|
+
};
|
|
329
290
|
}
|
|
330
|
-
|
|
291
|
+
function partOf(change) {
|
|
292
|
+
if (change.kind === 'field' || change.kind === 'section')
|
|
293
|
+
return `${change.kind} ${change.name}`;
|
|
331
294
|
if (change.kind === 'link')
|
|
332
|
-
return
|
|
295
|
+
return `link ${change.href}`;
|
|
333
296
|
if (change.kind === 'reference')
|
|
334
|
-
return
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
297
|
+
return `reference ${change.pointer}`;
|
|
298
|
+
return change.kind;
|
|
299
|
+
}
|
|
300
|
+
function planner(document, internal) {
|
|
301
|
+
const content = document.content;
|
|
302
|
+
const keyed = new Map();
|
|
303
|
+
let spans;
|
|
304
|
+
let occurrences;
|
|
305
|
+
let ending;
|
|
306
|
+
let added = 0;
|
|
307
|
+
const entryOf = (map, key) => {
|
|
308
|
+
let index = keyed.get(map);
|
|
309
|
+
if (index === undefined) {
|
|
310
|
+
index = new Map();
|
|
311
|
+
for (const entry of map.entries)
|
|
312
|
+
if (!index.has(entry.key))
|
|
313
|
+
index.set(entry.key, entry);
|
|
314
|
+
keyed.set(map, index);
|
|
315
|
+
}
|
|
316
|
+
return index.get(key);
|
|
317
|
+
};
|
|
318
|
+
const field = (name, value, prior, at) => {
|
|
319
|
+
const root = internal.root;
|
|
320
|
+
if (root === null)
|
|
321
|
+
return refuse('', 'frontmatter-fence', name);
|
|
322
|
+
if (!equalValue(document.fields[name], prior))
|
|
323
|
+
return 'stale';
|
|
324
|
+
if (!frontmatterValue(value))
|
|
325
|
+
return refuse('', 'frontmatter-fence', name);
|
|
326
|
+
const existing = entryOf(root, name);
|
|
327
|
+
if (existing !== undefined) {
|
|
328
|
+
const held = existing.value;
|
|
329
|
+
const inline = held.start > existing.after && !/[\r\n]/.test(content.slice(existing.after, held.start));
|
|
330
|
+
return inline
|
|
331
|
+
? [{ start: held.start, end: held.end, text: serializeJs(value), at }]
|
|
332
|
+
: [{ start: existing.after, end: held.end, text: ` ${serializeJs(value)}`, at }];
|
|
333
|
+
}
|
|
334
|
+
const entry = `${serializeKey(name)}: ${serializeJs(value)}`;
|
|
335
|
+
added += 1;
|
|
336
|
+
if (content[root.start] === '{') {
|
|
337
|
+
const separator = root.entries.length === 0 && added === 1 ? '' : ', ';
|
|
338
|
+
return [{ start: root.end - 1, end: root.end - 1, text: `${separator}${entry}`, at }];
|
|
339
|
+
}
|
|
340
|
+
const lineStart = content.lastIndexOf('\n', root.start - 1) + 1;
|
|
341
|
+
ending ??= lineEnding(content);
|
|
342
|
+
const line = `${content.slice(lineStart, root.start)}${entry}${ending}`;
|
|
343
|
+
return [{ start: internal.yamlEnd, end: internal.yamlEnd, text: line, at, entry: true }];
|
|
344
|
+
};
|
|
345
|
+
const section = (name, value, prior, at) => {
|
|
346
|
+
if (spans === undefined) {
|
|
347
|
+
spans = new Map();
|
|
348
|
+
for (const span of sectionSpans(readingOf(document), content.length))
|
|
349
|
+
if (!spans.has(span.name))
|
|
350
|
+
spans.set(span.name, span);
|
|
351
|
+
}
|
|
352
|
+
const span = spans.get(name);
|
|
353
|
+
if (span === undefined)
|
|
354
|
+
return refuse('', 'required-section', name);
|
|
355
|
+
if (content.slice(span.bodyStart, span.bodyEnd) !== prior)
|
|
356
|
+
return 'stale';
|
|
357
|
+
return [{ start: span.bodyStart, end: span.bodyEnd, text: value, at }];
|
|
358
|
+
};
|
|
359
|
+
const link = (href, to, at) => {
|
|
360
|
+
if (occurrences === undefined) {
|
|
361
|
+
occurrences = new Map();
|
|
362
|
+
for (const occurrence of linkDestinations(document)) {
|
|
363
|
+
const same = occurrences.get(occurrence.href);
|
|
364
|
+
if (same === undefined)
|
|
365
|
+
occurrences.set(occurrence.href, [occurrence]);
|
|
366
|
+
else
|
|
367
|
+
same.push(occurrence);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
const found = occurrences.get(href);
|
|
371
|
+
if (found === undefined)
|
|
372
|
+
return 'stale';
|
|
373
|
+
const written = retarget(href, to);
|
|
374
|
+
if (destinationText(written, false) === null)
|
|
375
|
+
return refuse('', 'unwritable-link', written);
|
|
376
|
+
return found.map((occurrence) => ({
|
|
377
|
+
start: occurrence.start,
|
|
378
|
+
end: occurrence.end,
|
|
379
|
+
text: destinationText(written, occurrence.bracketed),
|
|
380
|
+
at,
|
|
381
|
+
}));
|
|
382
|
+
};
|
|
383
|
+
const reference = (pointer, value, prior, at) => {
|
|
384
|
+
const node = internal.root === null ? undefined : nodeAt(internal.root, pointer, entryOf);
|
|
385
|
+
if (node?.kind !== 'scalar' || node.value !== prior)
|
|
386
|
+
return 'stale';
|
|
387
|
+
return [
|
|
388
|
+
{ start: node.start, end: node.end, text: scalarText(content[node.start], value), at },
|
|
389
|
+
];
|
|
390
|
+
};
|
|
391
|
+
return (change, at) => {
|
|
392
|
+
if (change.kind === 'field')
|
|
393
|
+
return field(change.name, change.value, change.prior, at);
|
|
394
|
+
if (change.kind === 'section')
|
|
395
|
+
return section(change.name, change.value, change.prior, at);
|
|
396
|
+
if (change.kind === 'link')
|
|
397
|
+
return link(change.href, change.to, at);
|
|
398
|
+
if (change.kind === 'reference')
|
|
399
|
+
return reference(change.pointer, change.value, change.prior, at);
|
|
400
|
+
if (change.kind === 'text') {
|
|
401
|
+
if (document.text !== change.prior)
|
|
402
|
+
return 'stale';
|
|
403
|
+
return [{ start: internal.bodyStart, end: content.length, text: change.value, at }];
|
|
404
|
+
}
|
|
405
|
+
if (document.preamble !== change.prior)
|
|
406
|
+
return 'stale';
|
|
407
|
+
const start = internal.bodyStart;
|
|
408
|
+
return [{ start, end: start + document.preamble.length, text: change.value, at }];
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
function overlapping(edits, stale) {
|
|
412
|
+
let reach = -1;
|
|
413
|
+
let owner = -1;
|
|
414
|
+
for (const edit of edits) {
|
|
415
|
+
if (edit.start < reach)
|
|
416
|
+
stale.add(Math.max(edit.at, owner));
|
|
417
|
+
if (edit.end > reach) {
|
|
418
|
+
reach = edit.end;
|
|
419
|
+
owner = edit.at;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
function firstHeadings(reading) {
|
|
424
|
+
const first = new Map();
|
|
425
|
+
for (const span of sectionSpans(reading, 0)) {
|
|
426
|
+
if (!first.has(span.name))
|
|
427
|
+
first.set(span.name, span.headingStart);
|
|
428
|
+
}
|
|
429
|
+
return first;
|
|
430
|
+
}
|
|
431
|
+
function hidden(before, after, shifts) {
|
|
432
|
+
const placed = firstHeadings(after);
|
|
433
|
+
let shift = 0;
|
|
434
|
+
let index = 0;
|
|
435
|
+
for (const [name, at] of firstHeadings(before)) {
|
|
436
|
+
while (index < shifts.length && shifts[index][0] <= at) {
|
|
437
|
+
shift += shifts[index][1];
|
|
438
|
+
index += 1;
|
|
439
|
+
}
|
|
440
|
+
if (placed.get(name) !== at + shift)
|
|
441
|
+
return name;
|
|
442
|
+
}
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
function spliced(source, edits) {
|
|
446
|
+
const shifts = [];
|
|
447
|
+
const pieces = [];
|
|
448
|
+
let cursor = 0;
|
|
449
|
+
let entered = false;
|
|
450
|
+
let ended = false;
|
|
451
|
+
for (const edit of edits) {
|
|
452
|
+
const kept = source.slice(cursor, edit.start);
|
|
453
|
+
pieces.push(kept);
|
|
454
|
+
if (kept !== '')
|
|
455
|
+
ended = kept.endsWith('\n');
|
|
456
|
+
let text = edit.text;
|
|
457
|
+
if (edit.entry === true && !entered) {
|
|
458
|
+
entered = true;
|
|
459
|
+
if (!ended)
|
|
460
|
+
text = `${lineEnding(source)}${text}`;
|
|
461
|
+
}
|
|
462
|
+
pieces.push(text);
|
|
463
|
+
if (text !== '')
|
|
464
|
+
ended = text.endsWith('\n');
|
|
465
|
+
shifts.push([edit.start, text.length - (edit.end - edit.start)]);
|
|
466
|
+
cursor = edit.end;
|
|
467
|
+
}
|
|
468
|
+
pieces.push(source.slice(cursor));
|
|
469
|
+
return { content: pieces.join(''), shifts };
|
|
470
|
+
}
|
|
471
|
+
function checkedReading(source, replaced) {
|
|
472
|
+
const checked = spliced(source, replaced);
|
|
473
|
+
return {
|
|
474
|
+
reading: bodyReading(checked.content, bodyStartOf(checked.content)),
|
|
475
|
+
shifts: checked.shifts,
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
function opening(change) {
|
|
479
|
+
return change.kind === 'section' && change.prior === undefined;
|
|
480
|
+
}
|
|
481
|
+
function withSections(document, changes, definition) {
|
|
482
|
+
const opened = [];
|
|
483
|
+
const held = [];
|
|
484
|
+
for (const [at, change] of changes.entries()) {
|
|
485
|
+
if (!opening(change))
|
|
486
|
+
continue;
|
|
487
|
+
if (Object.hasOwn(document.sections, change.name) || opened.includes(change.name))
|
|
488
|
+
held.push(at);
|
|
489
|
+
else
|
|
490
|
+
opened.push(change.name);
|
|
491
|
+
}
|
|
492
|
+
const source = document.content;
|
|
493
|
+
const ending = lineEnding(source);
|
|
494
|
+
const lead = source === '' || source.endsWith('\n') ? '' : ending;
|
|
495
|
+
const skeleton = parse(`${source}${lead}${opened.map((name) => `## ${name}${ending}`).join('')}`, '', definition);
|
|
496
|
+
if (!skeleton.ok)
|
|
497
|
+
return { outcome: 'refuse', refusal: skeleton.refusal };
|
|
498
|
+
const kept = changes.flatMap((change, at) => held.includes(at) ? [] : [[opening(change) ? { ...change, prior: '' } : change, at]]);
|
|
499
|
+
const applied = apply(skeleton.document, kept.map(([change]) => change), definition);
|
|
500
|
+
if (applied.outcome === 'refuse')
|
|
501
|
+
return applied;
|
|
502
|
+
const stale = [
|
|
503
|
+
...held,
|
|
504
|
+
...(applied.outcome === 'stale' ? applied.stale.map((at) => kept[at][1]) : []),
|
|
505
|
+
];
|
|
506
|
+
if (stale.length > 0)
|
|
507
|
+
return { outcome: 'stale', document, stale: stale.sort((left, right) => left - right) };
|
|
508
|
+
return applied;
|
|
509
|
+
}
|
|
510
|
+
export function apply(document, changes, definition) {
|
|
511
|
+
if (changes.some(opening))
|
|
512
|
+
return withSections(document, changes, definition);
|
|
513
|
+
const internal = internalOf(document);
|
|
514
|
+
if (!('bodyStart' in internal))
|
|
515
|
+
return { outcome: 'refuse', refusal: internal };
|
|
516
|
+
const plan = planner(document, internal);
|
|
517
|
+
const edits = [];
|
|
518
|
+
const stale = new Set();
|
|
519
|
+
const named = new Set();
|
|
520
|
+
for (const [at, change] of changes.entries()) {
|
|
521
|
+
const planned = plan(change, at);
|
|
522
|
+
if (planned === 'stale') {
|
|
523
|
+
stale.add(at);
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
if (!Array.isArray(planned))
|
|
527
|
+
return { outcome: 'refuse', refusal: planned };
|
|
528
|
+
const part = partOf(change);
|
|
529
|
+
if (named.has(part)) {
|
|
530
|
+
stale.add(at);
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
named.add(part);
|
|
534
|
+
for (const edit of planned)
|
|
535
|
+
edits.push(edit);
|
|
536
|
+
}
|
|
537
|
+
edits.sort((left, right) => left.start - right.start || left.end - right.end || left.at - right.at);
|
|
538
|
+
overlapping(edits, stale);
|
|
539
|
+
if (stale.size > 0)
|
|
540
|
+
return { outcome: 'stale', document, stale: [...stale].sort((left, right) => left - right) };
|
|
541
|
+
const source = document.content;
|
|
542
|
+
const { content, shifts } = spliced(source, edits);
|
|
543
|
+
if (content === source)
|
|
544
|
+
return { outcome: 'applied', document };
|
|
545
|
+
const parsed = parse(content, '', definition);
|
|
546
|
+
if (!parsed.ok)
|
|
547
|
+
return { outcome: 'refuse', refusal: parsed.refusal };
|
|
548
|
+
const replaced = edits.filter((edit) => {
|
|
549
|
+
const kind = changes[edit.at].kind;
|
|
550
|
+
return kind === 'section' || kind === 'preamble';
|
|
551
|
+
});
|
|
552
|
+
if (replaced.length > 0) {
|
|
553
|
+
const checked = replaced.length === edits.length
|
|
554
|
+
? { reading: readingOf(parsed.document), shifts }
|
|
555
|
+
: checkedReading(source, replaced);
|
|
556
|
+
const name = hidden(readingOf(document), checked.reading, checked.shifts);
|
|
557
|
+
if (name !== undefined)
|
|
558
|
+
return { outcome: 'refuse', refusal: refuse('', 'overlapping-section', name) };
|
|
559
|
+
}
|
|
560
|
+
return { outcome: 'applied', document: parsed.document };
|
|
340
561
|
}
|
|
341
562
|
export function serialize(document) {
|
|
342
563
|
return document.content;
|
|
343
564
|
}
|
|
344
|
-
export function fieldStart(document, pointer) {
|
|
345
|
-
const root = internals.get(document)?.root;
|
|
346
|
-
return root === null || root === undefined ? undefined : nodeAt(root, pointer)?.start;
|
|
347
|
-
}
|
|
348
565
|
export function frontmatter(document) {
|
|
349
|
-
return
|
|
566
|
+
return heldOf(document)?.internal.root ?? null;
|
|
350
567
|
}
|