@agentshouse/mdmodel 0.1.0-c04c0d681d2d4 → 0.1.0-c08b2adadd2fe
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 +150 -110
- package/README.md +8 -6
- package/dist/definition.d.ts +3 -1
- package/dist/definition.d.ts.map +1 -1
- package/dist/definition.js +138 -26
- package/dist/divide.d.ts.map +1 -1
- package/dist/divide.js +5 -4
- package/dist/document.d.ts +9 -5
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +169 -67
- 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/index.d.ts +23 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -7
- 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.map +1 -1
- package/dist/paths.js +9 -2
- package/dist/pattern.d.ts +10 -0
- package/dist/pattern.d.ts.map +1 -0
- package/dist/pattern.js +356 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +16 -21
- 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/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 +19 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/validate.d.ts +29 -5
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +327 -161
- package/dist/yaml.d.ts.map +1 -1
- package/dist/yaml.js +40 -14
- package/fixtures/README.md +25 -3
- 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/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/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/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 +47 -2
- 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 -69
- package/dist/condition.d.ts +0 -162
- package/dist/condition.d.ts.map +0 -1
- package/dist/condition.js +0 -616
- package/dist/links.d.ts +0 -29
- package/dist/links.d.ts.map +0 -1
- package/dist/links.js +0 -788
- 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/links.js
DELETED
|
@@ -1,788 +0,0 @@
|
|
|
1
|
-
const TAB_STOP = 4;
|
|
2
|
-
const CLOSING_FENCE_INDENT = 3;
|
|
3
|
-
const CODE_INDENT = 4;
|
|
4
|
-
const HEADING = /^#{1,6}(?:[ \t]|$)/;
|
|
5
|
-
const BREAK = /^(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/;
|
|
6
|
-
const SETEXT = /^(?:=+|-+)[ \t]*$/;
|
|
7
|
-
const BLANK = /^[ \t\v\f]*\r?$/;
|
|
8
|
-
const HTML_CLOSED = [
|
|
9
|
-
[/^<(?:script|pre|style)(?:[ \t\v\f>]|$)/i, /<\/(?:script|pre|style)>/i],
|
|
10
|
-
[/^<!--/, /-->/],
|
|
11
|
-
[/^<\?/, /\?>/],
|
|
12
|
-
[/^<![A-Z]/, />/],
|
|
13
|
-
[/^<!\[CDATA\[/, /\]\]>/],
|
|
14
|
-
];
|
|
15
|
-
const HTML_BLOCK_TAG = /^<\/?(?:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[1-6]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)(?:[ \t\v\f>]|\/>|$)/i;
|
|
16
|
-
const HTML_TAG_LINE = /^(?:<[A-Za-z][A-Za-z0-9-]*(?:[ \t\v\f]+[A-Za-z_:][A-Za-z0-9_.:-]*(?:[ \t\v\f]*=[ \t\v\f]*(?:[^ \t\v\f\r\n"'=<>`]+|'[^']*'|"[^"]*"))?)*[ \t\v\f]*\/?>|<\/[A-Za-z][A-Za-z0-9-]*[ \t\v\f]*>)[ \t\v\f]*$/;
|
|
17
|
-
const ORDERED = /^(\d{1,9})[.)]/;
|
|
18
|
-
const LABEL_LENGTH = 999;
|
|
19
|
-
const PUNCTUATION = /^[!-/:-@[-`{-~]$/;
|
|
20
|
-
const INLINE_RAW = new RegExp([
|
|
21
|
-
'<[A-Za-z][A-Za-z0-9-]*(?:[ \\t\\n\\v\\f\\r]+[A-Za-z_:][A-Za-z0-9_.:-]*(?:[ \\t\\n\\v\\f\\r]*=[ \\t\\n\\v\\f\\r]*(?:[^ \\t\\n\\v\\f\\r"\'=<>`]+|\'[^\']*\'|"[^"]*"))?)*[ \\t\\n\\v\\f\\r]*\\/?>',
|
|
22
|
-
'<\\/[A-Za-z][A-Za-z0-9-]*[ \\t\\n\\v\\f\\r]*>',
|
|
23
|
-
'<!--(?!-?>)(?:-?[^-])*-->',
|
|
24
|
-
'<\\?[\\s\\S]*?\\?>',
|
|
25
|
-
'<![A-Z]+[ \\t\\n\\v\\f\\r]+[^>]*>',
|
|
26
|
-
'<!\\[CDATA\\[[\\s\\S]*?\\]\\]>',
|
|
27
|
-
'<[A-Za-z][A-Za-z0-9+.-]{1,31}:[^\\x00-\\x20<>]*>',
|
|
28
|
-
"<[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?(?:\\.[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?)*>",
|
|
29
|
-
].join('|'), 'y');
|
|
30
|
-
const DELIMITER_ROW = /^\|?[ \t]*:?-+:?[ \t]*(?:\|[ \t]*:?-+:?[ \t]*)*\|?[ \t]*$/;
|
|
31
|
-
const GAP = /^[ \t\v\f]+/;
|
|
32
|
-
function endOfLine(text, from) {
|
|
33
|
-
const nl = text.indexOf('\n', from);
|
|
34
|
-
return nl === -1 ? text.length : nl;
|
|
35
|
-
}
|
|
36
|
-
function afterLine(text, end) {
|
|
37
|
-
return end < text.length ? end + 1 : text.length;
|
|
38
|
-
}
|
|
39
|
-
function itemMarker(text, index, end) {
|
|
40
|
-
let i = index;
|
|
41
|
-
const bullet = text[i];
|
|
42
|
-
if (bullet === '-' || bullet === '*' || bullet === '+')
|
|
43
|
-
i += 1;
|
|
44
|
-
else {
|
|
45
|
-
let digits = 0;
|
|
46
|
-
while (digits < 9 && i + digits < end && text[i + digits] >= '0' && text[i + digits] <= '9')
|
|
47
|
-
digits += 1;
|
|
48
|
-
const delimiter = text[i + digits];
|
|
49
|
-
if (digits === 0 || (delimiter !== '.' && delimiter !== ')'))
|
|
50
|
-
return null;
|
|
51
|
-
i += digits + 1;
|
|
52
|
-
}
|
|
53
|
-
if (i >= end)
|
|
54
|
-
return i;
|
|
55
|
-
if (text[i] !== ' ' && text[i] !== '\t')
|
|
56
|
-
return null;
|
|
57
|
-
return i + 1;
|
|
58
|
-
}
|
|
59
|
-
function readLine(text, start, end) {
|
|
60
|
-
let i = start;
|
|
61
|
-
let depth = 0;
|
|
62
|
-
let item = false;
|
|
63
|
-
let inner = start;
|
|
64
|
-
for (;;) {
|
|
65
|
-
let spaces = 0;
|
|
66
|
-
while (spaces < 3 && i < end && text[i] === ' ') {
|
|
67
|
-
i += 1;
|
|
68
|
-
spaces += 1;
|
|
69
|
-
}
|
|
70
|
-
if (i < end && text[i] === '>') {
|
|
71
|
-
depth += 1;
|
|
72
|
-
i += 1;
|
|
73
|
-
if (i < end && text[i] === ' ')
|
|
74
|
-
i += 1;
|
|
75
|
-
inner = i;
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
const after = i < end ? itemMarker(text, i, end) : null;
|
|
79
|
-
if (after === null)
|
|
80
|
-
break;
|
|
81
|
-
item = true;
|
|
82
|
-
i = after;
|
|
83
|
-
}
|
|
84
|
-
return { start, end, content: i, depth, item, inner };
|
|
85
|
-
}
|
|
86
|
-
function widthOf(text, from, to) {
|
|
87
|
-
let width = 0;
|
|
88
|
-
for (let i = from; i < to; i += 1)
|
|
89
|
-
width += text[i] === '\t' ? TAB_STOP - (width % TAB_STOP) : 1;
|
|
90
|
-
return width;
|
|
91
|
-
}
|
|
92
|
-
function afterIndent(text, line) {
|
|
93
|
-
let i = line.inner;
|
|
94
|
-
while (i < line.end && (text[i] === ' ' || text[i] === '\t'))
|
|
95
|
-
i += 1;
|
|
96
|
-
return i;
|
|
97
|
-
}
|
|
98
|
-
function indentWidth(text, line) {
|
|
99
|
-
return widthOf(text, line.inner, afterIndent(text, line));
|
|
100
|
-
}
|
|
101
|
-
function lineText(text, line) {
|
|
102
|
-
const raw = text.slice(line.content, line.end);
|
|
103
|
-
return raw.endsWith('\r') ? raw.slice(0, -1) : raw;
|
|
104
|
-
}
|
|
105
|
-
function fenceRun(text, at, end) {
|
|
106
|
-
const char = text[at];
|
|
107
|
-
if (char !== '`' && char !== '~')
|
|
108
|
-
return null;
|
|
109
|
-
let length = 0;
|
|
110
|
-
while (at + length < end && text[at + length] === char)
|
|
111
|
-
length += 1;
|
|
112
|
-
return length < 3 ? null : { char, length };
|
|
113
|
-
}
|
|
114
|
-
function fenceAt(text, line) {
|
|
115
|
-
const run = fenceRun(text, line.content, line.end);
|
|
116
|
-
if (run === null)
|
|
117
|
-
return null;
|
|
118
|
-
return {
|
|
119
|
-
char: run.char,
|
|
120
|
-
length: run.length,
|
|
121
|
-
depth: line.depth,
|
|
122
|
-
column: line.item ? widthOf(text, line.inner, line.content) : 0,
|
|
123
|
-
item: line.item,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
function closesFence(text, line, open) {
|
|
127
|
-
if (line.depth !== open.depth || line.item)
|
|
128
|
-
return false;
|
|
129
|
-
const indent = indentWidth(text, line);
|
|
130
|
-
if (indent < open.column || indent > open.column + CLOSING_FENCE_INDENT)
|
|
131
|
-
return false;
|
|
132
|
-
const at = afterIndent(text, line);
|
|
133
|
-
const run = fenceRun(text, at, line.end);
|
|
134
|
-
if (run === null || run.char !== open.char || run.length < open.length)
|
|
135
|
-
return false;
|
|
136
|
-
return text.slice(at + run.length, line.end).trim() === '';
|
|
137
|
-
}
|
|
138
|
-
function leavesContainer(text, line, open) {
|
|
139
|
-
if (line.depth < open.depth)
|
|
140
|
-
return true;
|
|
141
|
-
if (!open.item || lineText(text, line).trim() === '')
|
|
142
|
-
return false;
|
|
143
|
-
return indentWidth(text, line) < open.column;
|
|
144
|
-
}
|
|
145
|
-
function fenceSpan(text, line, open) {
|
|
146
|
-
let cursor = afterLine(text, line.end);
|
|
147
|
-
while (cursor < text.length) {
|
|
148
|
-
const end = endOfLine(text, cursor);
|
|
149
|
-
const next = readLine(text, cursor, end);
|
|
150
|
-
if (leavesContainer(text, next, open))
|
|
151
|
-
return cursor;
|
|
152
|
-
if (closesFence(text, next, open))
|
|
153
|
-
return afterLine(text, end);
|
|
154
|
-
cursor = afterLine(text, end);
|
|
155
|
-
}
|
|
156
|
-
return text.length;
|
|
157
|
-
}
|
|
158
|
-
function skipInlineCode(text, index, joins, unclosed) {
|
|
159
|
-
let ticks = 0;
|
|
160
|
-
while (text[index + ticks] === '`')
|
|
161
|
-
ticks += 1;
|
|
162
|
-
if (index < unclosed.stop && (unclosed.runs.get(ticks) ?? -1) <= index)
|
|
163
|
-
return index + ticks;
|
|
164
|
-
const runs = new Map();
|
|
165
|
-
let i = index + ticks;
|
|
166
|
-
while (i < text.length) {
|
|
167
|
-
const ch = text[i];
|
|
168
|
-
if (ch === '`') {
|
|
169
|
-
let run = 0;
|
|
170
|
-
while (text[i + run] === '`')
|
|
171
|
-
run += 1;
|
|
172
|
-
if (run === ticks)
|
|
173
|
-
return i + run;
|
|
174
|
-
runs.set(run, i);
|
|
175
|
-
i += run;
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
if (ch === '\r' && text[i + 1] !== '\n')
|
|
179
|
-
break;
|
|
180
|
-
if (ch === '\n' && !joins.has(i))
|
|
181
|
-
break;
|
|
182
|
-
i += 1;
|
|
183
|
-
}
|
|
184
|
-
unclosed.stop = i;
|
|
185
|
-
unclosed.runs = runs;
|
|
186
|
-
return index + ticks;
|
|
187
|
-
}
|
|
188
|
-
function skipLinkSpace(text, start) {
|
|
189
|
-
let i = start;
|
|
190
|
-
while (text[i] === ' ' || text[i] === '\t')
|
|
191
|
-
i += 1;
|
|
192
|
-
return i;
|
|
193
|
-
}
|
|
194
|
-
function skipInlineSpace(text, start, joins) {
|
|
195
|
-
let i = skipLinkSpace(text, start);
|
|
196
|
-
if (text[i] === '\r' && text[i + 1] === '\n')
|
|
197
|
-
i += 1;
|
|
198
|
-
if (text[i] !== '\n' || !joins.has(i))
|
|
199
|
-
return i;
|
|
200
|
-
return afterIndent(text, readLine(text, i + 1, endOfLine(text, i + 1)));
|
|
201
|
-
}
|
|
202
|
-
function closeEnd(text, start, open, joins) {
|
|
203
|
-
const close = open === '(' ? ')' : open;
|
|
204
|
-
let i = start;
|
|
205
|
-
while (i < text.length) {
|
|
206
|
-
const ch = text[i];
|
|
207
|
-
if (escapes(text, i)) {
|
|
208
|
-
i += 2;
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
if (ch === close)
|
|
212
|
-
return i + 1;
|
|
213
|
-
if (open === '(' && ch === '(')
|
|
214
|
-
return null;
|
|
215
|
-
if (ch === '\r' && text[i + 1] !== '\n')
|
|
216
|
-
return null;
|
|
217
|
-
if (ch === '\n' && !joins.has(i))
|
|
218
|
-
return null;
|
|
219
|
-
i += 1;
|
|
220
|
-
}
|
|
221
|
-
return null;
|
|
222
|
-
}
|
|
223
|
-
function skipTitle(text, start, joins) {
|
|
224
|
-
const open = text[start];
|
|
225
|
-
if (open !== '"' && open !== "'" && open !== '(')
|
|
226
|
-
return start;
|
|
227
|
-
return closeEnd(text, start + 1, open, joins);
|
|
228
|
-
}
|
|
229
|
-
function destinationAt(text, at, end) {
|
|
230
|
-
if (text[at] === '<') {
|
|
231
|
-
let close = at + 1;
|
|
232
|
-
while (close < end && text[close] !== '>') {
|
|
233
|
-
if (text[close] === '<')
|
|
234
|
-
return null;
|
|
235
|
-
close += escapes(text, close) ? 2 : 1;
|
|
236
|
-
}
|
|
237
|
-
if (close >= end)
|
|
238
|
-
return null;
|
|
239
|
-
return { href: text.slice(at + 1, close), start: at, end: close + 1, bracketed: true };
|
|
240
|
-
}
|
|
241
|
-
let i = at;
|
|
242
|
-
let depth = 0;
|
|
243
|
-
while (i < end) {
|
|
244
|
-
if (escapes(text, i)) {
|
|
245
|
-
i += 2;
|
|
246
|
-
continue;
|
|
247
|
-
}
|
|
248
|
-
const ch = text[i];
|
|
249
|
-
if (ch <= ' ' || ch === '\x7f')
|
|
250
|
-
break;
|
|
251
|
-
if (ch === '(')
|
|
252
|
-
depth += 1;
|
|
253
|
-
else if (ch === ')') {
|
|
254
|
-
if (depth === 0)
|
|
255
|
-
break;
|
|
256
|
-
depth -= 1;
|
|
257
|
-
}
|
|
258
|
-
i += 1;
|
|
259
|
-
}
|
|
260
|
-
if (depth !== 0)
|
|
261
|
-
return null;
|
|
262
|
-
return { href: text.slice(at, i), start: at, end: i, bracketed: false };
|
|
263
|
-
}
|
|
264
|
-
function destination(text, start, joins) {
|
|
265
|
-
let i = skipInlineSpace(text, start + 1, joins);
|
|
266
|
-
const occurrence = destinationAt(text, i, endOfLine(text, i));
|
|
267
|
-
if (occurrence === null)
|
|
268
|
-
return null;
|
|
269
|
-
i = occurrence.end;
|
|
270
|
-
const spaced = skipInlineSpace(text, i, joins);
|
|
271
|
-
if (spaced === i && text[i] !== ')')
|
|
272
|
-
return null;
|
|
273
|
-
i = spaced;
|
|
274
|
-
const afterTitle = skipTitle(text, i, joins);
|
|
275
|
-
if (afterTitle === null)
|
|
276
|
-
return null;
|
|
277
|
-
i = skipInlineSpace(text, afterTitle, joins);
|
|
278
|
-
if (text[i] !== ')')
|
|
279
|
-
return null;
|
|
280
|
-
return { occurrence, end: i + 1 };
|
|
281
|
-
}
|
|
282
|
-
function cellCount(row) {
|
|
283
|
-
return row
|
|
284
|
-
.trim()
|
|
285
|
-
.replace(/^\|/, '')
|
|
286
|
-
.replace(/(?<!\\)\|$/, '')
|
|
287
|
-
.split(/(?<!\\)\|/).length;
|
|
288
|
-
}
|
|
289
|
-
function headsTable(text, header, end) {
|
|
290
|
-
if (end >= text.length)
|
|
291
|
-
return false;
|
|
292
|
-
const row = lineText(text, readLine(text, end + 1, endOfLine(text, end + 1)));
|
|
293
|
-
return row.includes('|') && DELIMITER_ROW.test(row) && cellCount(row) === cellCount(header);
|
|
294
|
-
}
|
|
295
|
-
function blockText(text, line) {
|
|
296
|
-
if (!line.item)
|
|
297
|
-
return lineText(text, line);
|
|
298
|
-
const raw = text.slice(afterIndent(text, line), line.end);
|
|
299
|
-
return raw.endsWith('\r') ? raw.slice(0, -1) : raw;
|
|
300
|
-
}
|
|
301
|
-
function interruptsWithHtml(head) {
|
|
302
|
-
return HTML_CLOSED.some(([open]) => open.test(head)) || HTML_BLOCK_TAG.test(head);
|
|
303
|
-
}
|
|
304
|
-
function markerColumn(text, line) {
|
|
305
|
-
return widthOf(text, line.inner, afterIndent(text, line));
|
|
306
|
-
}
|
|
307
|
-
function markerEnd(text, at, end) {
|
|
308
|
-
const after = itemMarker(text, at, end);
|
|
309
|
-
if (after === null)
|
|
310
|
-
return null;
|
|
311
|
-
return after > at && (text[after - 1] === ' ' || text[after - 1] === '\t') ? after - 1 : after;
|
|
312
|
-
}
|
|
313
|
-
function itemContent(text, line) {
|
|
314
|
-
let marker = markerEnd(text, afterIndent(text, line), line.end);
|
|
315
|
-
for (;;) {
|
|
316
|
-
let next = marker + 1;
|
|
317
|
-
let spaces = 0;
|
|
318
|
-
while (spaces < 3 && text[next] === ' ') {
|
|
319
|
-
next += 1;
|
|
320
|
-
spaces += 1;
|
|
321
|
-
}
|
|
322
|
-
const nested = next < line.end ? markerEnd(text, next, line.end) : null;
|
|
323
|
-
if (nested === null)
|
|
324
|
-
break;
|
|
325
|
-
marker = nested;
|
|
326
|
-
}
|
|
327
|
-
const at = skipLinkSpace(text, marker);
|
|
328
|
-
const rest = text.slice(at, line.end).trim();
|
|
329
|
-
const column = widthOf(text, line.inner, at);
|
|
330
|
-
const start = widthOf(text, line.inner, marker);
|
|
331
|
-
if (rest !== '' && column - start <= CODE_INDENT)
|
|
332
|
-
return { column, code: null };
|
|
333
|
-
return { column: start + 1, code: rest === '' ? null : marker + 1 };
|
|
334
|
-
}
|
|
335
|
-
function joinsParagraph(text, line, depth, item) {
|
|
336
|
-
if (line.depth > depth)
|
|
337
|
-
return false;
|
|
338
|
-
const head = blockText(text, line);
|
|
339
|
-
if (head.trim() === '' || endsBlock(head) || interruptsWithHtml(head))
|
|
340
|
-
return false;
|
|
341
|
-
if (fenceAt(text, line) !== null)
|
|
342
|
-
return false;
|
|
343
|
-
if (!line.item)
|
|
344
|
-
return true;
|
|
345
|
-
if (item !== undefined && markerColumn(text, line) < item)
|
|
346
|
-
return false;
|
|
347
|
-
if (lineText(text, line).trim() === '')
|
|
348
|
-
return true;
|
|
349
|
-
const ordered = ORDERED.exec(head);
|
|
350
|
-
return ordered !== null && Number(ordered[1]) !== 1;
|
|
351
|
-
}
|
|
352
|
-
function startsParagraph(text, line) {
|
|
353
|
-
return lineText(text, line).trim() !== '' && !endsBlock(blockText(text, line));
|
|
354
|
-
}
|
|
355
|
-
function lineColumn(text, line) {
|
|
356
|
-
return line.item ? markerColumn(text, line) : indentWidth(text, line);
|
|
357
|
-
}
|
|
358
|
-
function endsTable(text, line, depth) {
|
|
359
|
-
const head = blockText(text, line);
|
|
360
|
-
return (head.trim() === '' ||
|
|
361
|
-
line.depth !== depth ||
|
|
362
|
-
line.item ||
|
|
363
|
-
endsBlock(head) ||
|
|
364
|
-
interruptsWithHtml(head) ||
|
|
365
|
-
fenceAt(text, line) !== null);
|
|
366
|
-
}
|
|
367
|
-
function quoteInner(text, start, end, depth) {
|
|
368
|
-
let i = start;
|
|
369
|
-
for (let level = 0; level < depth; level += 1) {
|
|
370
|
-
let spaces = 0;
|
|
371
|
-
while (spaces < 3 && i < end && text[i] === ' ') {
|
|
372
|
-
i += 1;
|
|
373
|
-
spaces += 1;
|
|
374
|
-
}
|
|
375
|
-
if (text[i] !== '>')
|
|
376
|
-
return null;
|
|
377
|
-
i += 1;
|
|
378
|
-
if (text[i] === ' ')
|
|
379
|
-
i += 1;
|
|
380
|
-
}
|
|
381
|
-
return i;
|
|
382
|
-
}
|
|
383
|
-
function htmlEnd(text, line, item) {
|
|
384
|
-
const head = lineText(text, line);
|
|
385
|
-
const closed = HTML_CLOSED.find(([open]) => open.test(head));
|
|
386
|
-
if (closed === undefined && !HTML_BLOCK_TAG.test(head) && !HTML_TAG_LINE.test(head))
|
|
387
|
-
return null;
|
|
388
|
-
if (closed !== undefined && closed[1].test(head))
|
|
389
|
-
return afterLine(text, line.end);
|
|
390
|
-
let cursor = afterLine(text, line.end);
|
|
391
|
-
while (cursor < text.length) {
|
|
392
|
-
const end = endOfLine(text, cursor);
|
|
393
|
-
const inner = quoteInner(text, cursor, end, line.depth);
|
|
394
|
-
if (inner === null)
|
|
395
|
-
return cursor;
|
|
396
|
-
const at = skipLinkSpace(text, inner);
|
|
397
|
-
const body = text.slice(inner, end);
|
|
398
|
-
const blank = BLANK.test(text.slice(at, end));
|
|
399
|
-
if (item !== undefined && !blank && widthOf(text, inner, at) < item)
|
|
400
|
-
return cursor;
|
|
401
|
-
if (closed === undefined && blank)
|
|
402
|
-
return cursor;
|
|
403
|
-
if (closed !== undefined && closed[1].test(body))
|
|
404
|
-
return afterLine(text, end);
|
|
405
|
-
cursor = afterLine(text, end);
|
|
406
|
-
if (cursor === end)
|
|
407
|
-
break;
|
|
408
|
-
}
|
|
409
|
-
return text.length;
|
|
410
|
-
}
|
|
411
|
-
function codeEnd(text, line, base) {
|
|
412
|
-
let stop = afterLine(text, line.end);
|
|
413
|
-
let cursor = stop;
|
|
414
|
-
while (cursor < text.length) {
|
|
415
|
-
const end = endOfLine(text, cursor);
|
|
416
|
-
const current = readLine(text, cursor, end);
|
|
417
|
-
if (current.depth !== line.depth)
|
|
418
|
-
break;
|
|
419
|
-
const blank = lineText(text, current).trim() === '' && !current.item;
|
|
420
|
-
if (!blank && (current.item || indentWidth(text, current) < base + CODE_INDENT))
|
|
421
|
-
break;
|
|
422
|
-
cursor = afterLine(text, end);
|
|
423
|
-
if (!blank)
|
|
424
|
-
stop = cursor;
|
|
425
|
-
if (cursor === end)
|
|
426
|
-
break;
|
|
427
|
-
}
|
|
428
|
-
return stop;
|
|
429
|
-
}
|
|
430
|
-
function structure(text) {
|
|
431
|
-
const joins = new Set();
|
|
432
|
-
const raw = [];
|
|
433
|
-
const items = [];
|
|
434
|
-
let paragraph = null;
|
|
435
|
-
let table = null;
|
|
436
|
-
let depth = 0;
|
|
437
|
-
let cursor = 0;
|
|
438
|
-
while (cursor < text.length) {
|
|
439
|
-
const end = endOfLine(text, cursor);
|
|
440
|
-
const line = readLine(text, cursor, end);
|
|
441
|
-
const content = lineText(text, line);
|
|
442
|
-
const blank = content.trim() === '' && !line.item;
|
|
443
|
-
if (table !== null && endsTable(text, line, table))
|
|
444
|
-
table = null;
|
|
445
|
-
if (table !== null) {
|
|
446
|
-
cursor = afterLine(text, end);
|
|
447
|
-
if (cursor === end)
|
|
448
|
-
break;
|
|
449
|
-
continue;
|
|
450
|
-
}
|
|
451
|
-
if (paragraph !== null && joinsParagraph(text, line, paragraph, items.at(-1))) {
|
|
452
|
-
if (headsTable(text, content, end)) {
|
|
453
|
-
table = line.depth;
|
|
454
|
-
paragraph = null;
|
|
455
|
-
}
|
|
456
|
-
else
|
|
457
|
-
joins.add(cursor - 1);
|
|
458
|
-
cursor = afterLine(text, end);
|
|
459
|
-
if (cursor === end)
|
|
460
|
-
break;
|
|
461
|
-
continue;
|
|
462
|
-
}
|
|
463
|
-
paragraph = null;
|
|
464
|
-
if (line.depth !== depth)
|
|
465
|
-
items.length = 0;
|
|
466
|
-
depth = line.depth;
|
|
467
|
-
if (!blank) {
|
|
468
|
-
while (items.length > 0 && items.at(-1) > lineColumn(text, line))
|
|
469
|
-
items.pop();
|
|
470
|
-
}
|
|
471
|
-
if (line.item && !BREAK.test(blockText(text, line))) {
|
|
472
|
-
const opened = itemContent(text, line);
|
|
473
|
-
items.push(opened.column);
|
|
474
|
-
if (opened.code !== null) {
|
|
475
|
-
const stop = codeEnd(text, line, opened.column);
|
|
476
|
-
raw.push([opened.code, stop]);
|
|
477
|
-
cursor = stop;
|
|
478
|
-
continue;
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
const open = fenceAt(text, line);
|
|
482
|
-
if (open !== null) {
|
|
483
|
-
const stop = fenceSpan(text, line, open);
|
|
484
|
-
raw.push([cursor, stop]);
|
|
485
|
-
cursor = stop;
|
|
486
|
-
continue;
|
|
487
|
-
}
|
|
488
|
-
const base = items.at(-1) ?? 0;
|
|
489
|
-
if (!blank && !line.item && indentWidth(text, line) >= base + CODE_INDENT) {
|
|
490
|
-
const stop = codeEnd(text, line, base);
|
|
491
|
-
raw.push([cursor, stop]);
|
|
492
|
-
cursor = stop;
|
|
493
|
-
continue;
|
|
494
|
-
}
|
|
495
|
-
const html = htmlEnd(text, line, items.at(-1));
|
|
496
|
-
if (html !== null) {
|
|
497
|
-
raw.push([cursor, html]);
|
|
498
|
-
cursor = html;
|
|
499
|
-
continue;
|
|
500
|
-
}
|
|
501
|
-
if (startsParagraph(text, line)) {
|
|
502
|
-
if (headsTable(text, content, end))
|
|
503
|
-
table = line.depth;
|
|
504
|
-
else
|
|
505
|
-
paragraph = line.depth;
|
|
506
|
-
}
|
|
507
|
-
cursor = afterLine(text, end);
|
|
508
|
-
if (cursor === end)
|
|
509
|
-
break;
|
|
510
|
-
}
|
|
511
|
-
return { joins, raw };
|
|
512
|
-
}
|
|
513
|
-
export function escapes(text, index) {
|
|
514
|
-
return text[index] === '\\' && PUNCTUATION.test(text[index + 1] ?? '');
|
|
515
|
-
}
|
|
516
|
-
function referenceEnd(text, start, joins) {
|
|
517
|
-
let i = start;
|
|
518
|
-
while (i < text.length) {
|
|
519
|
-
const ch = text[i];
|
|
520
|
-
if (escapes(text, i)) {
|
|
521
|
-
i += 2;
|
|
522
|
-
continue;
|
|
523
|
-
}
|
|
524
|
-
if (ch === ']')
|
|
525
|
-
return i + 1;
|
|
526
|
-
if (ch === '[')
|
|
527
|
-
return -1;
|
|
528
|
-
if (ch === '\r' && text[i + 1] !== '\n')
|
|
529
|
-
return -1;
|
|
530
|
-
if (ch === '\n' && !joins.has(i))
|
|
531
|
-
return -1;
|
|
532
|
-
i += 1;
|
|
533
|
-
}
|
|
534
|
-
return -1;
|
|
535
|
-
}
|
|
536
|
-
function labelText(text, from, to) {
|
|
537
|
-
let label = '';
|
|
538
|
-
let cursor = from;
|
|
539
|
-
let newline = text.indexOf('\n', cursor);
|
|
540
|
-
while (newline !== -1 && newline < to) {
|
|
541
|
-
label += `${text.slice(cursor, newline)}\n`;
|
|
542
|
-
cursor = afterIndent(text, readLine(text, newline + 1, endOfLine(text, newline + 1)));
|
|
543
|
-
newline = text.indexOf('\n', cursor);
|
|
544
|
-
}
|
|
545
|
-
return label + text.slice(cursor, to);
|
|
546
|
-
}
|
|
547
|
-
function labelKey(label) {
|
|
548
|
-
return label
|
|
549
|
-
.replace(/[ \t\n\v\f\r]+/g, ' ')
|
|
550
|
-
.replace(/^ | $/g, '')
|
|
551
|
-
.toLowerCase()
|
|
552
|
-
.split('\u0131')
|
|
553
|
-
.map((part) => part.toUpperCase())
|
|
554
|
-
.join('\u0131');
|
|
555
|
-
}
|
|
556
|
-
function labelKeyAt(text, from, to) {
|
|
557
|
-
const label = labelText(text, from, to);
|
|
558
|
-
if (label.length > LABEL_LENGTH && [...label].length > LABEL_LENGTH)
|
|
559
|
-
return '';
|
|
560
|
-
return labelKey(label);
|
|
561
|
-
}
|
|
562
|
-
function closeLink(text, opener, close, defs, joins) {
|
|
563
|
-
const end = close + 1;
|
|
564
|
-
const use = (destination, to) => ({
|
|
565
|
-
start: opener.start,
|
|
566
|
-
end: to,
|
|
567
|
-
labelStart: opener.at + 1,
|
|
568
|
-
labelEnd: close,
|
|
569
|
-
destination,
|
|
570
|
-
});
|
|
571
|
-
if (text[end] === '(') {
|
|
572
|
-
const dest = destination(text, end, joins);
|
|
573
|
-
if (dest !== null)
|
|
574
|
-
return use(dest.occurrence, dest.end);
|
|
575
|
-
}
|
|
576
|
-
if (text[end] === '[') {
|
|
577
|
-
const second = referenceEnd(text, end + 1, joins);
|
|
578
|
-
const collapsed = second === end + 2;
|
|
579
|
-
const named = collapsed
|
|
580
|
-
? opener.bracketAfter
|
|
581
|
-
? ''
|
|
582
|
-
: labelKeyAt(text, opener.at + 1, close)
|
|
583
|
-
: labelKeyAt(text, end + 1, second - 1);
|
|
584
|
-
if (second !== -1 && (named !== '' || collapsed)) {
|
|
585
|
-
const defined = defs.get(named);
|
|
586
|
-
return defined === undefined ? null : use(defined, second);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
if (opener.bracketAfter)
|
|
590
|
-
return null;
|
|
591
|
-
const shortcut = defs.get(labelKeyAt(text, opener.at + 1, close));
|
|
592
|
-
return shortcut === undefined ? null : use(shortcut, end);
|
|
593
|
-
}
|
|
594
|
-
function definitionDestination(text, from, to) {
|
|
595
|
-
const occurrence = destinationAt(text, skipLinkSpace(text, from), to);
|
|
596
|
-
return occurrence === null || (occurrence.href === '' && !occurrence.bracketed)
|
|
597
|
-
? null
|
|
598
|
-
: occurrence;
|
|
599
|
-
}
|
|
600
|
-
function titleEnd(text, at, joins) {
|
|
601
|
-
const open = text[at];
|
|
602
|
-
if (open !== '"' && open !== "'" && open !== '(')
|
|
603
|
-
return null;
|
|
604
|
-
const closed = closeEnd(text, at + 1, open, joins);
|
|
605
|
-
if (closed === null)
|
|
606
|
-
return null;
|
|
607
|
-
const end = endOfLine(text, closed);
|
|
608
|
-
return BLANK.test(text.slice(closed, end)) ? afterLine(text, end) : null;
|
|
609
|
-
}
|
|
610
|
-
function definitionAt(text, line, joins) {
|
|
611
|
-
if (text[line.content] !== '[')
|
|
612
|
-
return null;
|
|
613
|
-
const close = referenceEnd(text, line.content + 1, joins);
|
|
614
|
-
if (close === -1 || text[close] !== ':')
|
|
615
|
-
return null;
|
|
616
|
-
const id = labelKeyAt(text, line.content + 1, close - 1);
|
|
617
|
-
if (id === '')
|
|
618
|
-
return null;
|
|
619
|
-
const from = close + 1;
|
|
620
|
-
let end = endOfLine(text, from);
|
|
621
|
-
let occurrence = definitionDestination(text, from, end);
|
|
622
|
-
if (occurrence === null) {
|
|
623
|
-
if (text.slice(from, end).trim() !== '' || !joins.has(end))
|
|
624
|
-
return null;
|
|
625
|
-
const start = end + 1;
|
|
626
|
-
end = endOfLine(text, start);
|
|
627
|
-
occurrence = definitionDestination(text, afterIndent(text, readLine(text, start, end)), end);
|
|
628
|
-
if (occurrence === null)
|
|
629
|
-
return null;
|
|
630
|
-
}
|
|
631
|
-
const rest = text.slice(occurrence.end, end);
|
|
632
|
-
if (!BLANK.test(rest)) {
|
|
633
|
-
const gap = GAP.exec(rest);
|
|
634
|
-
const titled = gap === null ? null : titleEnd(text, occurrence.end + gap[0].length, joins);
|
|
635
|
-
return titled === null ? null : { id, occurrence, next: titled };
|
|
636
|
-
}
|
|
637
|
-
const next = afterLine(text, end);
|
|
638
|
-
if (joins.has(end)) {
|
|
639
|
-
const titled = titleEnd(text, afterIndent(text, readLine(text, next, endOfLine(text, next))), joins);
|
|
640
|
-
if (titled !== null)
|
|
641
|
-
return { id, occurrence, next: titled };
|
|
642
|
-
}
|
|
643
|
-
return { id, occurrence, next };
|
|
644
|
-
}
|
|
645
|
-
function endsBlock(content) {
|
|
646
|
-
return HEADING.test(content) || BREAK.test(content) || SETEXT.test(content);
|
|
647
|
-
}
|
|
648
|
-
function scanBlocks(text) {
|
|
649
|
-
const { joins, raw } = structure(text);
|
|
650
|
-
const rawEnd = spanCursor(raw);
|
|
651
|
-
const definitions = new Map();
|
|
652
|
-
const blocks = [];
|
|
653
|
-
let fresh = 0;
|
|
654
|
-
let i = 0;
|
|
655
|
-
while (i < text.length) {
|
|
656
|
-
const skip = rawEnd(i);
|
|
657
|
-
if (skip !== undefined) {
|
|
658
|
-
i = skip;
|
|
659
|
-
continue;
|
|
660
|
-
}
|
|
661
|
-
const line = readLine(text, i, endOfLine(text, i));
|
|
662
|
-
const definition = lineText(text, line).trim() === '' || (i !== fresh && joins.has(i - 1))
|
|
663
|
-
? null
|
|
664
|
-
: definitionAt(text, line, joins);
|
|
665
|
-
if (definition !== null) {
|
|
666
|
-
if (!definitions.has(definition.id))
|
|
667
|
-
definitions.set(definition.id, definition.occurrence);
|
|
668
|
-
blocks.push({ start: line.start, end: definition.next, destination: definition.occurrence });
|
|
669
|
-
i = definition.next;
|
|
670
|
-
fresh = i;
|
|
671
|
-
continue;
|
|
672
|
-
}
|
|
673
|
-
i = afterLine(text, line.end);
|
|
674
|
-
}
|
|
675
|
-
return { raw, definitions, blocks, joins };
|
|
676
|
-
}
|
|
677
|
-
export function spanCursor(spans) {
|
|
678
|
-
let next = 0;
|
|
679
|
-
return (index) => {
|
|
680
|
-
while (next < spans.length && spans[next][1] <= index)
|
|
681
|
-
next += 1;
|
|
682
|
-
const span = spans[next];
|
|
683
|
-
return span !== undefined && span[0] <= index ? span[1] : undefined;
|
|
684
|
-
};
|
|
685
|
-
}
|
|
686
|
-
export function fenceSpans(text) {
|
|
687
|
-
const fences = [];
|
|
688
|
-
let i = 0;
|
|
689
|
-
while (i < text.length) {
|
|
690
|
-
const line = readLine(text, i, endOfLine(text, i));
|
|
691
|
-
const open = fenceAt(text, line);
|
|
692
|
-
if (open === null) {
|
|
693
|
-
i = afterLine(text, line.end);
|
|
694
|
-
continue;
|
|
695
|
-
}
|
|
696
|
-
const stop = fenceSpan(text, line, open);
|
|
697
|
-
fences.push([i, stop]);
|
|
698
|
-
i = stop;
|
|
699
|
-
}
|
|
700
|
-
return fences;
|
|
701
|
-
}
|
|
702
|
-
function paragraphEnd(text, from, joins) {
|
|
703
|
-
let i = from;
|
|
704
|
-
while (i < text.length) {
|
|
705
|
-
if (text[i] === '\n' && !joins.has(i))
|
|
706
|
-
return i;
|
|
707
|
-
if (text[i] === '\r' && text[i + 1] !== '\n')
|
|
708
|
-
return i;
|
|
709
|
-
i += 1;
|
|
710
|
-
}
|
|
711
|
-
return i;
|
|
712
|
-
}
|
|
713
|
-
export function linkInventory(body) {
|
|
714
|
-
const { raw, definitions, blocks, joins } = scanBlocks(body);
|
|
715
|
-
const rawEnd = spanCursor(raw);
|
|
716
|
-
const blockEnd = spanCursor(blocks.map((block) => [block.start, block.end]));
|
|
717
|
-
const uses = [];
|
|
718
|
-
const openers = [];
|
|
719
|
-
const unclosed = { stop: 0, runs: new Map() };
|
|
720
|
-
let paragraph = { start: 0, end: -1, text: '' };
|
|
721
|
-
let i = 0;
|
|
722
|
-
while (i < body.length) {
|
|
723
|
-
const skipEnd = rawEnd(i) ?? blockEnd(i);
|
|
724
|
-
if (skipEnd !== undefined) {
|
|
725
|
-
openers.length = 0;
|
|
726
|
-
i = skipEnd;
|
|
727
|
-
continue;
|
|
728
|
-
}
|
|
729
|
-
const ch = body[i];
|
|
730
|
-
if (escapes(body, i)) {
|
|
731
|
-
i += 2;
|
|
732
|
-
continue;
|
|
733
|
-
}
|
|
734
|
-
if (ch === '`') {
|
|
735
|
-
i = skipInlineCode(body, i, joins, unclosed);
|
|
736
|
-
continue;
|
|
737
|
-
}
|
|
738
|
-
if (ch === '<') {
|
|
739
|
-
if (i >= paragraph.end) {
|
|
740
|
-
const end = paragraphEnd(body, i, joins);
|
|
741
|
-
paragraph = { start: i, end, text: body.slice(i, end) };
|
|
742
|
-
}
|
|
743
|
-
INLINE_RAW.lastIndex = i - paragraph.start;
|
|
744
|
-
const raw = INLINE_RAW.exec(paragraph.text);
|
|
745
|
-
if (raw !== null) {
|
|
746
|
-
i += raw[0].length;
|
|
747
|
-
continue;
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
if ((ch === '\n' && !joins.has(i)) || (ch === '\r' && body[i + 1] !== '\n'))
|
|
751
|
-
openers.length = 0;
|
|
752
|
-
if (ch === '[' || (ch === '!' && body[i + 1] === '[')) {
|
|
753
|
-
const previous = openers.at(-1);
|
|
754
|
-
if (previous !== undefined)
|
|
755
|
-
previous.bracketAfter = true;
|
|
756
|
-
const image = ch === '!';
|
|
757
|
-
openers.push({ at: image ? i + 1 : i, start: i, image, active: true, bracketAfter: false });
|
|
758
|
-
i += image ? 2 : 1;
|
|
759
|
-
continue;
|
|
760
|
-
}
|
|
761
|
-
if (ch === ']') {
|
|
762
|
-
const opener = openers.pop();
|
|
763
|
-
const use = opener?.active ? closeLink(body, opener, i, definitions, joins) : null;
|
|
764
|
-
if (opener === undefined || use === null) {
|
|
765
|
-
i += 1;
|
|
766
|
-
continue;
|
|
767
|
-
}
|
|
768
|
-
uses.push(use);
|
|
769
|
-
if (!opener.image)
|
|
770
|
-
for (const earlier of openers)
|
|
771
|
-
if (!earlier.image)
|
|
772
|
-
earlier.active = false;
|
|
773
|
-
i = use.end;
|
|
774
|
-
continue;
|
|
775
|
-
}
|
|
776
|
-
i += 1;
|
|
777
|
-
}
|
|
778
|
-
return { uses, blocks };
|
|
779
|
-
}
|
|
780
|
-
export function destinations(inventory) {
|
|
781
|
-
const byStart = new Map();
|
|
782
|
-
for (const use of inventory.uses)
|
|
783
|
-
byStart.set(use.destination.start, use.destination);
|
|
784
|
-
return [...byStart.values()].sort((left, right) => left.start - right.start);
|
|
785
|
-
}
|
|
786
|
-
export function hrefOccurrences(body) {
|
|
787
|
-
return destinations(linkInventory(body));
|
|
788
|
-
}
|