@pyreon/code 0.5.0
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/LICENSE +21 -0
- package/lib/analysis/index.js.html +5406 -0
- package/lib/dist-B5vB-rif.js +3904 -0
- package/lib/dist-B5vB-rif.js.map +1 -0
- package/lib/dist-BAfzu5eu.js +1428 -0
- package/lib/dist-BAfzu5eu.js.map +1 -0
- package/lib/dist-BLlV_D16.js +1166 -0
- package/lib/dist-BLlV_D16.js.map +1 -0
- package/lib/dist-BNmKLTu8.js +373 -0
- package/lib/dist-BNmKLTu8.js.map +1 -0
- package/lib/dist-BZtTlC1J.js +692 -0
- package/lib/dist-BZtTlC1J.js.map +1 -0
- package/lib/dist-CTDqGIAf.js +856 -0
- package/lib/dist-CTDqGIAf.js.map +1 -0
- package/lib/dist-CTPisNZp.js +83 -0
- package/lib/dist-CTPisNZp.js.map +1 -0
- package/lib/dist-Ce2tvOxv.js +379 -0
- package/lib/dist-Ce2tvOxv.js.map +1 -0
- package/lib/dist-CttF0OTv.js +465 -0
- package/lib/dist-CttF0OTv.js.map +1 -0
- package/lib/dist-DS2tluW9.js +818 -0
- package/lib/dist-DS2tluW9.js.map +1 -0
- package/lib/dist-DUNx9ldu.js +460 -0
- package/lib/dist-DUNx9ldu.js.map +1 -0
- package/lib/dist-Dej_yf3k.js +473 -0
- package/lib/dist-Dej_yf3k.js.map +1 -0
- package/lib/dist-DshStUxU.js +283 -0
- package/lib/dist-DshStUxU.js.map +1 -0
- package/lib/dist-qTrOe7xY.js +461 -0
- package/lib/dist-qTrOe7xY.js.map +1 -0
- package/lib/dist-v09vikKr.js +2421 -0
- package/lib/dist-v09vikKr.js.map +1 -0
- package/lib/index.js +915 -0
- package/lib/index.js.map +1 -0
- package/lib/types/dist.d.ts +798 -0
- package/lib/types/dist.d.ts.map +1 -0
- package/lib/types/dist10.d.ts +67 -0
- package/lib/types/dist10.d.ts.map +1 -0
- package/lib/types/dist11.d.ts +126 -0
- package/lib/types/dist11.d.ts.map +1 -0
- package/lib/types/dist12.d.ts +21 -0
- package/lib/types/dist12.d.ts.map +1 -0
- package/lib/types/dist13.d.ts +404 -0
- package/lib/types/dist13.d.ts.map +1 -0
- package/lib/types/dist14.d.ts +292 -0
- package/lib/types/dist14.d.ts.map +1 -0
- package/lib/types/dist15.d.ts +132 -0
- package/lib/types/dist15.d.ts.map +1 -0
- package/lib/types/dist2.d.ts +15 -0
- package/lib/types/dist2.d.ts.map +1 -0
- package/lib/types/dist3.d.ts +106 -0
- package/lib/types/dist3.d.ts.map +1 -0
- package/lib/types/dist4.d.ts +67 -0
- package/lib/types/dist4.d.ts.map +1 -0
- package/lib/types/dist5.d.ts +95 -0
- package/lib/types/dist5.d.ts.map +1 -0
- package/lib/types/dist6.d.ts +330 -0
- package/lib/types/dist6.d.ts.map +1 -0
- package/lib/types/dist7.d.ts +15 -0
- package/lib/types/dist7.d.ts.map +1 -0
- package/lib/types/dist8.d.ts +15 -0
- package/lib/types/dist8.d.ts.map +1 -0
- package/lib/types/dist9.d.ts +635 -0
- package/lib/types/dist9.d.ts.map +1 -0
- package/lib/types/index.d.ts +852 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index2.d.ts +347 -0
- package/lib/types/index2.d.ts.map +1 -0
- package/package.json +79 -0
- package/src/components/code-editor.tsx +42 -0
- package/src/components/diff-editor.tsx +97 -0
- package/src/components/tabbed-editor.tsx +86 -0
- package/src/editor.ts +652 -0
- package/src/index.ts +52 -0
- package/src/languages.ts +77 -0
- package/src/minimap.ts +160 -0
- package/src/tabbed-editor.ts +231 -0
- package/src/tests/code.test.ts +505 -0
- package/src/themes.ts +87 -0
- package/src/types.ts +253 -0
|
@@ -0,0 +1,2421 @@
|
|
|
1
|
+
import { d as NodeType, h as parseMixed, l as NodeProp, m as Tree, n as styleTags, p as Parser, r as tags, t as Tag, u as NodeSet } from "./dist-B5vB-rif.js";
|
|
2
|
+
import { html, htmlCompletionSource } from "./dist-BAfzu5eu.js";
|
|
3
|
+
import { Language, LanguageDescription, LanguageSupport, ParseContext, defineLanguageFacet, foldNodeProp, foldService, indentNodeProp, indentUnit, languageDataProp, syntaxTree } from "@codemirror/language";
|
|
4
|
+
import { EditorSelection, EditorState, Prec, countColumn } from "@codemirror/state";
|
|
5
|
+
import { EditorView, keymap } from "@codemirror/view";
|
|
6
|
+
import { CompletionContext } from "@codemirror/autocomplete";
|
|
7
|
+
|
|
8
|
+
//#region ../../node_modules/.bun/@lezer+markdown@1.6.3/node_modules/@lezer/markdown/dist/index.js
|
|
9
|
+
var CompositeBlock = class CompositeBlock {
|
|
10
|
+
static create(type, value, from, parentHash, end) {
|
|
11
|
+
return new CompositeBlock(type, value, from, parentHash + (parentHash << 8) + type + (value << 4) | 0, end, [], []);
|
|
12
|
+
}
|
|
13
|
+
constructor(type, value, from, hash, end, children, positions) {
|
|
14
|
+
this.type = type;
|
|
15
|
+
this.value = value;
|
|
16
|
+
this.from = from;
|
|
17
|
+
this.hash = hash;
|
|
18
|
+
this.end = end;
|
|
19
|
+
this.children = children;
|
|
20
|
+
this.positions = positions;
|
|
21
|
+
this.hashProp = [[NodeProp.contextHash, hash]];
|
|
22
|
+
}
|
|
23
|
+
addChild(child, pos) {
|
|
24
|
+
if (child.prop(NodeProp.contextHash) != this.hash) child = new Tree(child.type, child.children, child.positions, child.length, this.hashProp);
|
|
25
|
+
this.children.push(child);
|
|
26
|
+
this.positions.push(pos);
|
|
27
|
+
}
|
|
28
|
+
toTree(nodeSet, end = this.end) {
|
|
29
|
+
let last = this.children.length - 1;
|
|
30
|
+
if (last >= 0) end = Math.max(end, this.positions[last] + this.children[last].length + this.from);
|
|
31
|
+
return new Tree(nodeSet.types[this.type], this.children, this.positions, end - this.from).balance({ makeTree: (children, positions, length) => new Tree(NodeType.none, children, positions, length, this.hashProp) });
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var Type;
|
|
35
|
+
(function(Type) {
|
|
36
|
+
Type[Type["Document"] = 1] = "Document";
|
|
37
|
+
Type[Type["CodeBlock"] = 2] = "CodeBlock";
|
|
38
|
+
Type[Type["FencedCode"] = 3] = "FencedCode";
|
|
39
|
+
Type[Type["Blockquote"] = 4] = "Blockquote";
|
|
40
|
+
Type[Type["HorizontalRule"] = 5] = "HorizontalRule";
|
|
41
|
+
Type[Type["BulletList"] = 6] = "BulletList";
|
|
42
|
+
Type[Type["OrderedList"] = 7] = "OrderedList";
|
|
43
|
+
Type[Type["ListItem"] = 8] = "ListItem";
|
|
44
|
+
Type[Type["ATXHeading1"] = 9] = "ATXHeading1";
|
|
45
|
+
Type[Type["ATXHeading2"] = 10] = "ATXHeading2";
|
|
46
|
+
Type[Type["ATXHeading3"] = 11] = "ATXHeading3";
|
|
47
|
+
Type[Type["ATXHeading4"] = 12] = "ATXHeading4";
|
|
48
|
+
Type[Type["ATXHeading5"] = 13] = "ATXHeading5";
|
|
49
|
+
Type[Type["ATXHeading6"] = 14] = "ATXHeading6";
|
|
50
|
+
Type[Type["SetextHeading1"] = 15] = "SetextHeading1";
|
|
51
|
+
Type[Type["SetextHeading2"] = 16] = "SetextHeading2";
|
|
52
|
+
Type[Type["HTMLBlock"] = 17] = "HTMLBlock";
|
|
53
|
+
Type[Type["LinkReference"] = 18] = "LinkReference";
|
|
54
|
+
Type[Type["Paragraph"] = 19] = "Paragraph";
|
|
55
|
+
Type[Type["CommentBlock"] = 20] = "CommentBlock";
|
|
56
|
+
Type[Type["ProcessingInstructionBlock"] = 21] = "ProcessingInstructionBlock";
|
|
57
|
+
Type[Type["Escape"] = 22] = "Escape";
|
|
58
|
+
Type[Type["Entity"] = 23] = "Entity";
|
|
59
|
+
Type[Type["HardBreak"] = 24] = "HardBreak";
|
|
60
|
+
Type[Type["Emphasis"] = 25] = "Emphasis";
|
|
61
|
+
Type[Type["StrongEmphasis"] = 26] = "StrongEmphasis";
|
|
62
|
+
Type[Type["Link"] = 27] = "Link";
|
|
63
|
+
Type[Type["Image"] = 28] = "Image";
|
|
64
|
+
Type[Type["InlineCode"] = 29] = "InlineCode";
|
|
65
|
+
Type[Type["HTMLTag"] = 30] = "HTMLTag";
|
|
66
|
+
Type[Type["Comment"] = 31] = "Comment";
|
|
67
|
+
Type[Type["ProcessingInstruction"] = 32] = "ProcessingInstruction";
|
|
68
|
+
Type[Type["Autolink"] = 33] = "Autolink";
|
|
69
|
+
Type[Type["HeaderMark"] = 34] = "HeaderMark";
|
|
70
|
+
Type[Type["QuoteMark"] = 35] = "QuoteMark";
|
|
71
|
+
Type[Type["ListMark"] = 36] = "ListMark";
|
|
72
|
+
Type[Type["LinkMark"] = 37] = "LinkMark";
|
|
73
|
+
Type[Type["EmphasisMark"] = 38] = "EmphasisMark";
|
|
74
|
+
Type[Type["CodeMark"] = 39] = "CodeMark";
|
|
75
|
+
Type[Type["CodeText"] = 40] = "CodeText";
|
|
76
|
+
Type[Type["CodeInfo"] = 41] = "CodeInfo";
|
|
77
|
+
Type[Type["LinkTitle"] = 42] = "LinkTitle";
|
|
78
|
+
Type[Type["LinkLabel"] = 43] = "LinkLabel";
|
|
79
|
+
Type[Type["URL"] = 44] = "URL";
|
|
80
|
+
})(Type || (Type = {}));
|
|
81
|
+
/**
|
|
82
|
+
Data structure used to accumulate a block's content during [leaf
|
|
83
|
+
block parsing](#BlockParser.leaf).
|
|
84
|
+
*/
|
|
85
|
+
var LeafBlock = class {
|
|
86
|
+
/**
|
|
87
|
+
@internal
|
|
88
|
+
*/
|
|
89
|
+
constructor(start, content) {
|
|
90
|
+
this.start = start;
|
|
91
|
+
this.content = content;
|
|
92
|
+
/**
|
|
93
|
+
@internal
|
|
94
|
+
*/
|
|
95
|
+
this.marks = [];
|
|
96
|
+
/**
|
|
97
|
+
The block parsers active for this block.
|
|
98
|
+
*/
|
|
99
|
+
this.parsers = [];
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
Data structure used during block-level per-line parsing.
|
|
104
|
+
*/
|
|
105
|
+
var Line = class {
|
|
106
|
+
constructor() {
|
|
107
|
+
/**
|
|
108
|
+
The line's full text.
|
|
109
|
+
*/
|
|
110
|
+
this.text = "";
|
|
111
|
+
/**
|
|
112
|
+
The base indent provided by the composite contexts (that have
|
|
113
|
+
been handled so far).
|
|
114
|
+
*/
|
|
115
|
+
this.baseIndent = 0;
|
|
116
|
+
/**
|
|
117
|
+
The string position corresponding to the base indent.
|
|
118
|
+
*/
|
|
119
|
+
this.basePos = 0;
|
|
120
|
+
/**
|
|
121
|
+
The number of contexts handled @internal
|
|
122
|
+
*/
|
|
123
|
+
this.depth = 0;
|
|
124
|
+
/**
|
|
125
|
+
Any markers (i.e. block quote markers) parsed for the contexts. @internal
|
|
126
|
+
*/
|
|
127
|
+
this.markers = [];
|
|
128
|
+
/**
|
|
129
|
+
The position of the next non-whitespace character beyond any
|
|
130
|
+
list, blockquote, or other composite block markers.
|
|
131
|
+
*/
|
|
132
|
+
this.pos = 0;
|
|
133
|
+
/**
|
|
134
|
+
The column of the next non-whitespace character.
|
|
135
|
+
*/
|
|
136
|
+
this.indent = 0;
|
|
137
|
+
/**
|
|
138
|
+
The character code of the character after `pos`.
|
|
139
|
+
*/
|
|
140
|
+
this.next = -1;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
@internal
|
|
144
|
+
*/
|
|
145
|
+
forward() {
|
|
146
|
+
if (this.basePos > this.pos) this.forwardInner();
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
@internal
|
|
150
|
+
*/
|
|
151
|
+
forwardInner() {
|
|
152
|
+
let newPos = this.skipSpace(this.basePos);
|
|
153
|
+
this.indent = this.countIndent(newPos, this.pos, this.indent);
|
|
154
|
+
this.pos = newPos;
|
|
155
|
+
this.next = newPos == this.text.length ? -1 : this.text.charCodeAt(newPos);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
Skip whitespace after the given position, return the position of
|
|
159
|
+
the next non-space character or the end of the line if there's
|
|
160
|
+
only space after `from`.
|
|
161
|
+
*/
|
|
162
|
+
skipSpace(from) {
|
|
163
|
+
return skipSpace(this.text, from);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
@internal
|
|
167
|
+
*/
|
|
168
|
+
reset(text) {
|
|
169
|
+
this.text = text;
|
|
170
|
+
this.baseIndent = this.basePos = this.pos = this.indent = 0;
|
|
171
|
+
this.forwardInner();
|
|
172
|
+
this.depth = 1;
|
|
173
|
+
while (this.markers.length) this.markers.pop();
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
Move the line's base position forward to the given position.
|
|
177
|
+
This should only be called by composite [block
|
|
178
|
+
parsers](#BlockParser.parse) or [markup skipping
|
|
179
|
+
functions](#NodeSpec.composite).
|
|
180
|
+
*/
|
|
181
|
+
moveBase(to) {
|
|
182
|
+
this.basePos = to;
|
|
183
|
+
this.baseIndent = this.countIndent(to, this.pos, this.indent);
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
Move the line's base position forward to the given _column_.
|
|
187
|
+
*/
|
|
188
|
+
moveBaseColumn(indent) {
|
|
189
|
+
this.baseIndent = indent;
|
|
190
|
+
this.basePos = this.findColumn(indent);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
Store a composite-block-level marker. Should be called from
|
|
194
|
+
[markup skipping functions](#NodeSpec.composite) when they
|
|
195
|
+
consume any non-whitespace characters.
|
|
196
|
+
*/
|
|
197
|
+
addMarker(elt) {
|
|
198
|
+
this.markers.push(elt);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
Find the column position at `to`, optionally starting at a given
|
|
202
|
+
position and column.
|
|
203
|
+
*/
|
|
204
|
+
countIndent(to, from = 0, indent = 0) {
|
|
205
|
+
for (let i = from; i < to; i++) indent += this.text.charCodeAt(i) == 9 ? 4 - indent % 4 : 1;
|
|
206
|
+
return indent;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
Find the position corresponding to the given column.
|
|
210
|
+
*/
|
|
211
|
+
findColumn(goal) {
|
|
212
|
+
let i = 0;
|
|
213
|
+
for (let indent = 0; i < this.text.length && indent < goal; i++) indent += this.text.charCodeAt(i) == 9 ? 4 - indent % 4 : 1;
|
|
214
|
+
return i;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
@internal
|
|
218
|
+
*/
|
|
219
|
+
scrub() {
|
|
220
|
+
if (!this.baseIndent) return this.text;
|
|
221
|
+
let result = "";
|
|
222
|
+
for (let i = 0; i < this.basePos; i++) result += " ";
|
|
223
|
+
return result + this.text.slice(this.basePos);
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
function skipForList(bl, cx, line) {
|
|
227
|
+
if (line.pos == line.text.length || bl != cx.block && line.indent >= cx.stack[line.depth + 1].value + line.baseIndent) return true;
|
|
228
|
+
if (line.indent >= line.baseIndent + 4) return false;
|
|
229
|
+
let size = (bl.type == Type.OrderedList ? isOrderedList : isBulletList)(line, cx, false);
|
|
230
|
+
return size > 0 && (bl.type != Type.BulletList || isHorizontalRule(line, cx, false) < 0) && line.text.charCodeAt(line.pos + size - 1) == bl.value;
|
|
231
|
+
}
|
|
232
|
+
const DefaultSkipMarkup = {
|
|
233
|
+
[Type.Blockquote](bl, cx, line) {
|
|
234
|
+
if (line.next != 62) return false;
|
|
235
|
+
line.markers.push(elt(Type.QuoteMark, cx.lineStart + line.pos, cx.lineStart + line.pos + 1));
|
|
236
|
+
line.moveBase(line.pos + (space(line.text.charCodeAt(line.pos + 1)) ? 2 : 1));
|
|
237
|
+
bl.end = cx.lineStart + line.text.length;
|
|
238
|
+
return true;
|
|
239
|
+
},
|
|
240
|
+
[Type.ListItem](bl, _cx, line) {
|
|
241
|
+
if (line.indent < line.baseIndent + bl.value && line.next > -1) return false;
|
|
242
|
+
line.moveBaseColumn(line.baseIndent + bl.value);
|
|
243
|
+
return true;
|
|
244
|
+
},
|
|
245
|
+
[Type.OrderedList]: skipForList,
|
|
246
|
+
[Type.BulletList]: skipForList,
|
|
247
|
+
[Type.Document]() {
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
function space(ch) {
|
|
252
|
+
return ch == 32 || ch == 9 || ch == 10 || ch == 13;
|
|
253
|
+
}
|
|
254
|
+
function skipSpace(line, i = 0) {
|
|
255
|
+
while (i < line.length && space(line.charCodeAt(i))) i++;
|
|
256
|
+
return i;
|
|
257
|
+
}
|
|
258
|
+
function skipSpaceBack(line, i, to) {
|
|
259
|
+
while (i > to && space(line.charCodeAt(i - 1))) i--;
|
|
260
|
+
return i;
|
|
261
|
+
}
|
|
262
|
+
function isFencedCode(line) {
|
|
263
|
+
if (line.next != 96 && line.next != 126) return -1;
|
|
264
|
+
let pos = line.pos + 1;
|
|
265
|
+
while (pos < line.text.length && line.text.charCodeAt(pos) == line.next) pos++;
|
|
266
|
+
if (pos < line.pos + 3) return -1;
|
|
267
|
+
if (line.next == 96) {
|
|
268
|
+
for (let i = pos; i < line.text.length; i++) if (line.text.charCodeAt(i) == 96) return -1;
|
|
269
|
+
}
|
|
270
|
+
return pos;
|
|
271
|
+
}
|
|
272
|
+
function isBlockquote(line) {
|
|
273
|
+
return line.next != 62 ? -1 : line.text.charCodeAt(line.pos + 1) == 32 ? 2 : 1;
|
|
274
|
+
}
|
|
275
|
+
function isHorizontalRule(line, cx, breaking) {
|
|
276
|
+
if (line.next != 42 && line.next != 45 && line.next != 95) return -1;
|
|
277
|
+
let count = 1;
|
|
278
|
+
for (let pos = line.pos + 1; pos < line.text.length; pos++) {
|
|
279
|
+
let ch = line.text.charCodeAt(pos);
|
|
280
|
+
if (ch == line.next) count++;
|
|
281
|
+
else if (!space(ch)) return -1;
|
|
282
|
+
}
|
|
283
|
+
if (breaking && line.next == 45 && isSetextUnderline(line) > -1 && line.depth == cx.stack.length && cx.parser.leafBlockParsers.indexOf(DefaultLeafBlocks.SetextHeading) > -1) return -1;
|
|
284
|
+
return count < 3 ? -1 : 1;
|
|
285
|
+
}
|
|
286
|
+
function inList(cx, type) {
|
|
287
|
+
for (let i = cx.stack.length - 1; i >= 0; i--) if (cx.stack[i].type == type) return true;
|
|
288
|
+
return false;
|
|
289
|
+
}
|
|
290
|
+
function isBulletList(line, cx, breaking) {
|
|
291
|
+
return (line.next == 45 || line.next == 43 || line.next == 42) && (line.pos == line.text.length - 1 || space(line.text.charCodeAt(line.pos + 1))) && (!breaking || inList(cx, Type.BulletList) || line.skipSpace(line.pos + 2) < line.text.length) ? 1 : -1;
|
|
292
|
+
}
|
|
293
|
+
function isOrderedList(line, cx, breaking) {
|
|
294
|
+
let pos = line.pos, next = line.next;
|
|
295
|
+
for (;;) {
|
|
296
|
+
if (next >= 48 && next <= 57) pos++;
|
|
297
|
+
else break;
|
|
298
|
+
if (pos == line.text.length) return -1;
|
|
299
|
+
next = line.text.charCodeAt(pos);
|
|
300
|
+
}
|
|
301
|
+
if (pos == line.pos || pos > line.pos + 9 || next != 46 && next != 41 || pos < line.text.length - 1 && !space(line.text.charCodeAt(pos + 1)) || breaking && !inList(cx, Type.OrderedList) && (line.skipSpace(pos + 1) == line.text.length || pos > line.pos + 1 || line.next != 49)) return -1;
|
|
302
|
+
return pos + 1 - line.pos;
|
|
303
|
+
}
|
|
304
|
+
function isAtxHeading(line) {
|
|
305
|
+
if (line.next != 35) return -1;
|
|
306
|
+
let pos = line.pos + 1;
|
|
307
|
+
while (pos < line.text.length && line.text.charCodeAt(pos) == 35) pos++;
|
|
308
|
+
if (pos < line.text.length && line.text.charCodeAt(pos) != 32) return -1;
|
|
309
|
+
let size = pos - line.pos;
|
|
310
|
+
return size > 6 ? -1 : size;
|
|
311
|
+
}
|
|
312
|
+
function isSetextUnderline(line) {
|
|
313
|
+
if (line.next != 45 && line.next != 61 || line.indent >= line.baseIndent + 4) return -1;
|
|
314
|
+
let pos = line.pos + 1;
|
|
315
|
+
while (pos < line.text.length && line.text.charCodeAt(pos) == line.next) pos++;
|
|
316
|
+
let end = pos;
|
|
317
|
+
while (pos < line.text.length && space(line.text.charCodeAt(pos))) pos++;
|
|
318
|
+
return pos == line.text.length ? end : -1;
|
|
319
|
+
}
|
|
320
|
+
const EmptyLine = /^[ \t]*$/, CommentEnd = /-->/, ProcessingEnd = /\?>/;
|
|
321
|
+
const HTMLBlockStyle = [
|
|
322
|
+
[/^<(?:script|pre|style)(?:\s|>|$)/i, /<\/(?:script|pre|style)>/i],
|
|
323
|
+
[/^\s*<!--/, CommentEnd],
|
|
324
|
+
[/^\s*<\?/, ProcessingEnd],
|
|
325
|
+
[/^\s*<![A-Z]/, />/],
|
|
326
|
+
[/^\s*<!\[CDATA\[/, /\]\]>/],
|
|
327
|
+
[/^\s*<\/?(?: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|h1|h2|h3|h4|h5|h6|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)(?:\s|\/?>|$)/i, EmptyLine],
|
|
328
|
+
[/^\s*(?:<\/[a-z][\w-]*\s*>|<[a-z][\w-]*(\s+[a-z:_][\w-.]*(?:\s*=\s*(?:[^\s"'=<>`]+|'[^']*'|"[^"]*"))?)*\s*>)\s*$/i, EmptyLine]
|
|
329
|
+
];
|
|
330
|
+
function isHTMLBlock(line, _cx, breaking) {
|
|
331
|
+
if (line.next != 60) return -1;
|
|
332
|
+
let rest = line.text.slice(line.pos);
|
|
333
|
+
for (let i = 0, e = HTMLBlockStyle.length - (breaking ? 1 : 0); i < e; i++) if (HTMLBlockStyle[i][0].test(rest)) return i;
|
|
334
|
+
return -1;
|
|
335
|
+
}
|
|
336
|
+
function getListIndent(line, pos) {
|
|
337
|
+
let indentAfter = line.countIndent(pos, line.pos, line.indent);
|
|
338
|
+
let indented = line.countIndent(line.skipSpace(pos), pos, indentAfter);
|
|
339
|
+
return indented >= indentAfter + 5 ? indentAfter + 1 : indented;
|
|
340
|
+
}
|
|
341
|
+
function addCodeText(marks, from, to) {
|
|
342
|
+
let last = marks.length - 1;
|
|
343
|
+
if (last >= 0 && marks[last].to == from && marks[last].type == Type.CodeText) marks[last].to = to;
|
|
344
|
+
else marks.push(elt(Type.CodeText, from, to));
|
|
345
|
+
}
|
|
346
|
+
const DefaultBlockParsers = {
|
|
347
|
+
LinkReference: void 0,
|
|
348
|
+
IndentedCode(cx, line) {
|
|
349
|
+
let base = line.baseIndent + 4;
|
|
350
|
+
if (line.indent < base) return false;
|
|
351
|
+
let start = line.findColumn(base);
|
|
352
|
+
let from = cx.lineStart + start, to = cx.lineStart + line.text.length;
|
|
353
|
+
let marks = [], pendingMarks = [];
|
|
354
|
+
addCodeText(marks, from, to);
|
|
355
|
+
while (cx.nextLine() && line.depth >= cx.stack.length) if (line.pos == line.text.length) {
|
|
356
|
+
addCodeText(pendingMarks, cx.lineStart - 1, cx.lineStart);
|
|
357
|
+
for (let m of line.markers) pendingMarks.push(m);
|
|
358
|
+
} else if (line.indent < base) break;
|
|
359
|
+
else {
|
|
360
|
+
if (pendingMarks.length) {
|
|
361
|
+
for (let m of pendingMarks) if (m.type == Type.CodeText) addCodeText(marks, m.from, m.to);
|
|
362
|
+
else marks.push(m);
|
|
363
|
+
pendingMarks = [];
|
|
364
|
+
}
|
|
365
|
+
addCodeText(marks, cx.lineStart - 1, cx.lineStart);
|
|
366
|
+
for (let m of line.markers) marks.push(m);
|
|
367
|
+
to = cx.lineStart + line.text.length;
|
|
368
|
+
let codeStart = cx.lineStart + line.findColumn(line.baseIndent + 4);
|
|
369
|
+
if (codeStart < to) addCodeText(marks, codeStart, to);
|
|
370
|
+
}
|
|
371
|
+
if (pendingMarks.length) {
|
|
372
|
+
pendingMarks = pendingMarks.filter((m) => m.type != Type.CodeText);
|
|
373
|
+
if (pendingMarks.length) line.markers = pendingMarks.concat(line.markers);
|
|
374
|
+
}
|
|
375
|
+
cx.addNode(cx.buffer.writeElements(marks, -from).finish(Type.CodeBlock, to - from), from);
|
|
376
|
+
return true;
|
|
377
|
+
},
|
|
378
|
+
FencedCode(cx, line) {
|
|
379
|
+
let fenceEnd = isFencedCode(line);
|
|
380
|
+
if (fenceEnd < 0) return false;
|
|
381
|
+
let from = cx.lineStart + line.pos, ch = line.next, len = fenceEnd - line.pos;
|
|
382
|
+
let infoFrom = line.skipSpace(fenceEnd), infoTo = skipSpaceBack(line.text, line.text.length, infoFrom);
|
|
383
|
+
let marks = [elt(Type.CodeMark, from, from + len)];
|
|
384
|
+
if (infoFrom < infoTo) marks.push(elt(Type.CodeInfo, cx.lineStart + infoFrom, cx.lineStart + infoTo));
|
|
385
|
+
for (let first = true, empty = true, hasLine = false; cx.nextLine() && line.depth >= cx.stack.length; first = false) {
|
|
386
|
+
let i = line.pos;
|
|
387
|
+
if (line.indent - line.baseIndent < 4) while (i < line.text.length && line.text.charCodeAt(i) == ch) i++;
|
|
388
|
+
if (i - line.pos >= len && line.skipSpace(i) == line.text.length) {
|
|
389
|
+
for (let m of line.markers) marks.push(m);
|
|
390
|
+
if (empty && hasLine) addCodeText(marks, cx.lineStart - 1, cx.lineStart);
|
|
391
|
+
marks.push(elt(Type.CodeMark, cx.lineStart + line.pos, cx.lineStart + i));
|
|
392
|
+
cx.nextLine();
|
|
393
|
+
break;
|
|
394
|
+
} else {
|
|
395
|
+
hasLine = true;
|
|
396
|
+
if (!first) {
|
|
397
|
+
addCodeText(marks, cx.lineStart - 1, cx.lineStart);
|
|
398
|
+
empty = false;
|
|
399
|
+
}
|
|
400
|
+
for (let m of line.markers) marks.push(m);
|
|
401
|
+
let textStart = cx.lineStart + line.basePos, textEnd = cx.lineStart + line.text.length;
|
|
402
|
+
if (textStart < textEnd) {
|
|
403
|
+
addCodeText(marks, textStart, textEnd);
|
|
404
|
+
empty = false;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
cx.addNode(cx.buffer.writeElements(marks, -from).finish(Type.FencedCode, cx.prevLineEnd() - from), from);
|
|
409
|
+
return true;
|
|
410
|
+
},
|
|
411
|
+
Blockquote(cx, line) {
|
|
412
|
+
let size = isBlockquote(line);
|
|
413
|
+
if (size < 0) return false;
|
|
414
|
+
cx.startContext(Type.Blockquote, line.pos);
|
|
415
|
+
cx.addNode(Type.QuoteMark, cx.lineStart + line.pos, cx.lineStart + line.pos + 1);
|
|
416
|
+
line.moveBase(line.pos + size);
|
|
417
|
+
return null;
|
|
418
|
+
},
|
|
419
|
+
HorizontalRule(cx, line) {
|
|
420
|
+
if (isHorizontalRule(line, cx, false) < 0) return false;
|
|
421
|
+
let from = cx.lineStart + line.pos;
|
|
422
|
+
cx.nextLine();
|
|
423
|
+
cx.addNode(Type.HorizontalRule, from);
|
|
424
|
+
return true;
|
|
425
|
+
},
|
|
426
|
+
BulletList(cx, line) {
|
|
427
|
+
let size = isBulletList(line, cx, false);
|
|
428
|
+
if (size < 0) return false;
|
|
429
|
+
if (cx.block.type != Type.BulletList) cx.startContext(Type.BulletList, line.basePos, line.next);
|
|
430
|
+
let newBase = getListIndent(line, line.pos + 1);
|
|
431
|
+
cx.startContext(Type.ListItem, line.basePos, newBase - line.baseIndent);
|
|
432
|
+
cx.addNode(Type.ListMark, cx.lineStart + line.pos, cx.lineStart + line.pos + size);
|
|
433
|
+
line.moveBaseColumn(newBase);
|
|
434
|
+
return null;
|
|
435
|
+
},
|
|
436
|
+
OrderedList(cx, line) {
|
|
437
|
+
let size = isOrderedList(line, cx, false);
|
|
438
|
+
if (size < 0) return false;
|
|
439
|
+
if (cx.block.type != Type.OrderedList) cx.startContext(Type.OrderedList, line.basePos, line.text.charCodeAt(line.pos + size - 1));
|
|
440
|
+
let newBase = getListIndent(line, line.pos + size);
|
|
441
|
+
cx.startContext(Type.ListItem, line.basePos, newBase - line.baseIndent);
|
|
442
|
+
cx.addNode(Type.ListMark, cx.lineStart + line.pos, cx.lineStart + line.pos + size);
|
|
443
|
+
line.moveBaseColumn(newBase);
|
|
444
|
+
return null;
|
|
445
|
+
},
|
|
446
|
+
ATXHeading(cx, line) {
|
|
447
|
+
let size = isAtxHeading(line);
|
|
448
|
+
if (size < 0) return false;
|
|
449
|
+
let off = line.pos, from = cx.lineStart + off;
|
|
450
|
+
let endOfSpace = skipSpaceBack(line.text, line.text.length, off), after = endOfSpace;
|
|
451
|
+
while (after > off && line.text.charCodeAt(after - 1) == line.next) after--;
|
|
452
|
+
if (after == endOfSpace || after == off || !space(line.text.charCodeAt(after - 1))) after = line.text.length;
|
|
453
|
+
let buf = cx.buffer.write(Type.HeaderMark, 0, size).writeElements(cx.parser.parseInline(line.text.slice(off + size + 1, after), from + size + 1), -from);
|
|
454
|
+
if (after < line.text.length) buf.write(Type.HeaderMark, after - off, endOfSpace - off);
|
|
455
|
+
let node = buf.finish(Type.ATXHeading1 - 1 + size, line.text.length - off);
|
|
456
|
+
cx.nextLine();
|
|
457
|
+
cx.addNode(node, from);
|
|
458
|
+
return true;
|
|
459
|
+
},
|
|
460
|
+
HTMLBlock(cx, line) {
|
|
461
|
+
let type = isHTMLBlock(line, cx, false);
|
|
462
|
+
if (type < 0) return false;
|
|
463
|
+
let from = cx.lineStart + line.pos, end = HTMLBlockStyle[type][1];
|
|
464
|
+
let marks = [], trailing = end != EmptyLine;
|
|
465
|
+
while (!end.test(line.text) && cx.nextLine()) {
|
|
466
|
+
if (line.depth < cx.stack.length) {
|
|
467
|
+
trailing = false;
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
for (let m of line.markers) marks.push(m);
|
|
471
|
+
}
|
|
472
|
+
if (trailing) cx.nextLine();
|
|
473
|
+
let nodeType = end == CommentEnd ? Type.CommentBlock : end == ProcessingEnd ? Type.ProcessingInstructionBlock : Type.HTMLBlock;
|
|
474
|
+
let to = cx.prevLineEnd();
|
|
475
|
+
cx.addNode(cx.buffer.writeElements(marks, -from).finish(nodeType, to - from), from);
|
|
476
|
+
return true;
|
|
477
|
+
},
|
|
478
|
+
SetextHeading: void 0
|
|
479
|
+
};
|
|
480
|
+
var LinkReferenceParser = class {
|
|
481
|
+
constructor(leaf) {
|
|
482
|
+
this.stage = 0;
|
|
483
|
+
this.elts = [];
|
|
484
|
+
this.pos = 0;
|
|
485
|
+
this.start = leaf.start;
|
|
486
|
+
this.advance(leaf.content);
|
|
487
|
+
}
|
|
488
|
+
nextLine(cx, line, leaf) {
|
|
489
|
+
if (this.stage == -1) return false;
|
|
490
|
+
let content = leaf.content + "\n" + line.scrub();
|
|
491
|
+
let finish = this.advance(content);
|
|
492
|
+
if (finish > -1 && finish < content.length) return this.complete(cx, leaf, finish);
|
|
493
|
+
return false;
|
|
494
|
+
}
|
|
495
|
+
finish(cx, leaf) {
|
|
496
|
+
if ((this.stage == 2 || this.stage == 3) && skipSpace(leaf.content, this.pos) == leaf.content.length) return this.complete(cx, leaf, leaf.content.length);
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
complete(cx, leaf, len) {
|
|
500
|
+
cx.addLeafElement(leaf, elt(Type.LinkReference, this.start, this.start + len, this.elts));
|
|
501
|
+
return true;
|
|
502
|
+
}
|
|
503
|
+
nextStage(elt) {
|
|
504
|
+
if (elt) {
|
|
505
|
+
this.pos = elt.to - this.start;
|
|
506
|
+
this.elts.push(elt);
|
|
507
|
+
this.stage++;
|
|
508
|
+
return true;
|
|
509
|
+
}
|
|
510
|
+
if (elt === false) this.stage = -1;
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
advance(content) {
|
|
514
|
+
for (;;) if (this.stage == -1) return -1;
|
|
515
|
+
else if (this.stage == 0) {
|
|
516
|
+
if (!this.nextStage(parseLinkLabel(content, this.pos, this.start, true))) return -1;
|
|
517
|
+
if (content.charCodeAt(this.pos) != 58) return this.stage = -1;
|
|
518
|
+
this.elts.push(elt(Type.LinkMark, this.pos + this.start, this.pos + this.start + 1));
|
|
519
|
+
this.pos++;
|
|
520
|
+
} else if (this.stage == 1) {
|
|
521
|
+
if (!this.nextStage(parseURL(content, skipSpace(content, this.pos), this.start))) return -1;
|
|
522
|
+
} else if (this.stage == 2) {
|
|
523
|
+
let skip = skipSpace(content, this.pos), end = 0;
|
|
524
|
+
if (skip > this.pos) {
|
|
525
|
+
let title = parseLinkTitle(content, skip, this.start);
|
|
526
|
+
if (title) {
|
|
527
|
+
let titleEnd = lineEnd(content, title.to - this.start);
|
|
528
|
+
if (titleEnd > 0) {
|
|
529
|
+
this.nextStage(title);
|
|
530
|
+
end = titleEnd;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (!end) end = lineEnd(content, this.pos);
|
|
535
|
+
return end > 0 && end < content.length ? end : -1;
|
|
536
|
+
} else return lineEnd(content, this.pos);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
function lineEnd(text, pos) {
|
|
540
|
+
for (; pos < text.length; pos++) {
|
|
541
|
+
let next = text.charCodeAt(pos);
|
|
542
|
+
if (next == 10) break;
|
|
543
|
+
if (!space(next)) return -1;
|
|
544
|
+
}
|
|
545
|
+
return pos;
|
|
546
|
+
}
|
|
547
|
+
var SetextHeadingParser = class {
|
|
548
|
+
nextLine(cx, line, leaf) {
|
|
549
|
+
let underline = line.depth < cx.stack.length ? -1 : isSetextUnderline(line);
|
|
550
|
+
let next = line.next;
|
|
551
|
+
if (underline < 0) return false;
|
|
552
|
+
let underlineMark = elt(Type.HeaderMark, cx.lineStart + line.pos, cx.lineStart + underline);
|
|
553
|
+
cx.nextLine();
|
|
554
|
+
cx.addLeafElement(leaf, elt(next == 61 ? Type.SetextHeading1 : Type.SetextHeading2, leaf.start, cx.prevLineEnd(), [...cx.parser.parseInline(leaf.content, leaf.start), underlineMark]));
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
finish() {
|
|
558
|
+
return false;
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
const DefaultLeafBlocks = {
|
|
562
|
+
LinkReference(_, leaf) {
|
|
563
|
+
return leaf.content.charCodeAt(0) == 91 ? new LinkReferenceParser(leaf) : null;
|
|
564
|
+
},
|
|
565
|
+
SetextHeading() {
|
|
566
|
+
return new SetextHeadingParser();
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
const DefaultEndLeaf = [
|
|
570
|
+
(_, line) => isAtxHeading(line) >= 0,
|
|
571
|
+
(_, line) => isFencedCode(line) >= 0,
|
|
572
|
+
(_, line) => isBlockquote(line) >= 0,
|
|
573
|
+
(p, line) => isBulletList(line, p, true) >= 0,
|
|
574
|
+
(p, line) => isOrderedList(line, p, true) >= 0,
|
|
575
|
+
(p, line) => isHorizontalRule(line, p, true) >= 0,
|
|
576
|
+
(p, line) => isHTMLBlock(line, p, true) >= 0
|
|
577
|
+
];
|
|
578
|
+
const scanLineResult = {
|
|
579
|
+
text: "",
|
|
580
|
+
end: 0
|
|
581
|
+
};
|
|
582
|
+
/**
|
|
583
|
+
Block-level parsing functions get access to this context object.
|
|
584
|
+
*/
|
|
585
|
+
var BlockContext = class {
|
|
586
|
+
/**
|
|
587
|
+
@internal
|
|
588
|
+
*/
|
|
589
|
+
constructor(parser, input, fragments, ranges) {
|
|
590
|
+
this.parser = parser;
|
|
591
|
+
this.input = input;
|
|
592
|
+
this.ranges = ranges;
|
|
593
|
+
this.line = new Line();
|
|
594
|
+
this.atEnd = false;
|
|
595
|
+
/**
|
|
596
|
+
For reused nodes on gaps, we can't directly put the original
|
|
597
|
+
node into the tree, since that may be bigger than its parent.
|
|
598
|
+
When this happens, we create a dummy tree that is replaced by
|
|
599
|
+
the proper node in `injectGaps` @internal
|
|
600
|
+
*/
|
|
601
|
+
this.reusePlaceholders = /* @__PURE__ */ new Map();
|
|
602
|
+
this.stoppedAt = null;
|
|
603
|
+
/**
|
|
604
|
+
The range index that absoluteLineStart points into @internal
|
|
605
|
+
*/
|
|
606
|
+
this.rangeI = 0;
|
|
607
|
+
this.to = ranges[ranges.length - 1].to;
|
|
608
|
+
this.lineStart = this.absoluteLineStart = this.absoluteLineEnd = ranges[0].from;
|
|
609
|
+
this.block = CompositeBlock.create(Type.Document, 0, this.lineStart, 0, 0);
|
|
610
|
+
this.stack = [this.block];
|
|
611
|
+
this.fragments = fragments.length ? new FragmentCursor(fragments, input) : null;
|
|
612
|
+
this.readLine();
|
|
613
|
+
}
|
|
614
|
+
get parsedPos() {
|
|
615
|
+
return this.absoluteLineStart;
|
|
616
|
+
}
|
|
617
|
+
advance() {
|
|
618
|
+
if (this.stoppedAt != null && this.absoluteLineStart > this.stoppedAt) return this.finish();
|
|
619
|
+
let { line } = this;
|
|
620
|
+
for (;;) {
|
|
621
|
+
for (let markI = 0;;) {
|
|
622
|
+
let next = line.depth < this.stack.length ? this.stack[this.stack.length - 1] : null;
|
|
623
|
+
while (markI < line.markers.length && (!next || line.markers[markI].from < next.end)) {
|
|
624
|
+
let mark = line.markers[markI++];
|
|
625
|
+
this.addNode(mark.type, mark.from, mark.to);
|
|
626
|
+
}
|
|
627
|
+
if (!next) break;
|
|
628
|
+
this.finishContext();
|
|
629
|
+
}
|
|
630
|
+
if (line.pos < line.text.length) break;
|
|
631
|
+
if (!this.nextLine()) return this.finish();
|
|
632
|
+
}
|
|
633
|
+
if (this.fragments && this.reuseFragment(line.basePos)) return null;
|
|
634
|
+
start: for (;;) {
|
|
635
|
+
for (let type of this.parser.blockParsers) if (type) {
|
|
636
|
+
let result = type(this, line);
|
|
637
|
+
if (result != false) {
|
|
638
|
+
if (result == true) return null;
|
|
639
|
+
line.forward();
|
|
640
|
+
continue start;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
let leaf = new LeafBlock(this.lineStart + line.pos, line.text.slice(line.pos));
|
|
646
|
+
for (let parse of this.parser.leafBlockParsers) if (parse) {
|
|
647
|
+
let parser = parse(this, leaf);
|
|
648
|
+
if (parser) leaf.parsers.push(parser);
|
|
649
|
+
}
|
|
650
|
+
lines: while (this.nextLine()) {
|
|
651
|
+
if (line.pos == line.text.length) break;
|
|
652
|
+
if (line.indent < line.baseIndent + 4) {
|
|
653
|
+
for (let stop of this.parser.endLeafBlock) if (stop(this, line, leaf)) break lines;
|
|
654
|
+
}
|
|
655
|
+
for (let parser of leaf.parsers) if (parser.nextLine(this, line, leaf)) return null;
|
|
656
|
+
leaf.content += "\n" + line.scrub();
|
|
657
|
+
for (let m of line.markers) leaf.marks.push(m);
|
|
658
|
+
}
|
|
659
|
+
this.finishLeaf(leaf);
|
|
660
|
+
return null;
|
|
661
|
+
}
|
|
662
|
+
stopAt(pos) {
|
|
663
|
+
if (this.stoppedAt != null && this.stoppedAt < pos) throw new RangeError("Can't move stoppedAt forward");
|
|
664
|
+
this.stoppedAt = pos;
|
|
665
|
+
}
|
|
666
|
+
reuseFragment(start) {
|
|
667
|
+
if (!this.fragments.moveTo(this.absoluteLineStart + start, this.absoluteLineStart) || !this.fragments.matches(this.block.hash)) return false;
|
|
668
|
+
let taken = this.fragments.takeNodes(this);
|
|
669
|
+
if (!taken) return false;
|
|
670
|
+
this.absoluteLineStart += taken;
|
|
671
|
+
this.lineStart = toRelative(this.absoluteLineStart, this.ranges);
|
|
672
|
+
this.moveRangeI();
|
|
673
|
+
if (this.absoluteLineStart < this.to) {
|
|
674
|
+
this.lineStart++;
|
|
675
|
+
this.absoluteLineStart++;
|
|
676
|
+
this.readLine();
|
|
677
|
+
} else {
|
|
678
|
+
this.atEnd = true;
|
|
679
|
+
this.readLine();
|
|
680
|
+
}
|
|
681
|
+
return true;
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
The number of parent blocks surrounding the current block.
|
|
685
|
+
*/
|
|
686
|
+
get depth() {
|
|
687
|
+
return this.stack.length;
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
Get the type of the parent block at the given depth. When no
|
|
691
|
+
depth is passed, return the type of the innermost parent.
|
|
692
|
+
*/
|
|
693
|
+
parentType(depth = this.depth - 1) {
|
|
694
|
+
return this.parser.nodeSet.types[this.stack[depth].type];
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
Move to the next input line. This should only be called by
|
|
698
|
+
(non-composite) [block parsers](#BlockParser.parse) that consume
|
|
699
|
+
the line directly, or leaf block parser
|
|
700
|
+
[`nextLine`](#LeafBlockParser.nextLine) methods when they
|
|
701
|
+
consume the current line (and return true).
|
|
702
|
+
*/
|
|
703
|
+
nextLine() {
|
|
704
|
+
this.lineStart += this.line.text.length;
|
|
705
|
+
if (this.absoluteLineEnd >= this.to) {
|
|
706
|
+
this.absoluteLineStart = this.absoluteLineEnd;
|
|
707
|
+
this.atEnd = true;
|
|
708
|
+
this.readLine();
|
|
709
|
+
return false;
|
|
710
|
+
} else {
|
|
711
|
+
this.lineStart++;
|
|
712
|
+
this.absoluteLineStart = this.absoluteLineEnd + 1;
|
|
713
|
+
this.moveRangeI();
|
|
714
|
+
this.readLine();
|
|
715
|
+
return true;
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
/**
|
|
719
|
+
Retrieve the text of the line after the current one, without
|
|
720
|
+
actually moving the context's current line forward.
|
|
721
|
+
*/
|
|
722
|
+
peekLine() {
|
|
723
|
+
return this.scanLine(this.absoluteLineEnd + 1).text;
|
|
724
|
+
}
|
|
725
|
+
moveRangeI() {
|
|
726
|
+
while (this.rangeI < this.ranges.length - 1 && this.absoluteLineStart >= this.ranges[this.rangeI].to) {
|
|
727
|
+
this.rangeI++;
|
|
728
|
+
this.absoluteLineStart = Math.max(this.absoluteLineStart, this.ranges[this.rangeI].from);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
@internal
|
|
733
|
+
Collect the text for the next line.
|
|
734
|
+
*/
|
|
735
|
+
scanLine(start) {
|
|
736
|
+
let r = scanLineResult;
|
|
737
|
+
r.end = start;
|
|
738
|
+
if (start >= this.to) r.text = "";
|
|
739
|
+
else {
|
|
740
|
+
r.text = this.lineChunkAt(start);
|
|
741
|
+
r.end += r.text.length;
|
|
742
|
+
if (this.ranges.length > 1) {
|
|
743
|
+
let textOffset = this.absoluteLineStart, rangeI = this.rangeI;
|
|
744
|
+
while (this.ranges[rangeI].to < r.end) {
|
|
745
|
+
rangeI++;
|
|
746
|
+
let nextFrom = this.ranges[rangeI].from;
|
|
747
|
+
let after = this.lineChunkAt(nextFrom);
|
|
748
|
+
r.end = nextFrom + after.length;
|
|
749
|
+
r.text = r.text.slice(0, this.ranges[rangeI - 1].to - textOffset) + after;
|
|
750
|
+
textOffset = r.end - r.text.length;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
return r;
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
@internal
|
|
758
|
+
Populate this.line with the content of the next line. Skip
|
|
759
|
+
leading characters covered by composite blocks.
|
|
760
|
+
*/
|
|
761
|
+
readLine() {
|
|
762
|
+
let { line } = this, { text, end } = this.scanLine(this.absoluteLineStart);
|
|
763
|
+
this.absoluteLineEnd = end;
|
|
764
|
+
line.reset(text);
|
|
765
|
+
for (; line.depth < this.stack.length; line.depth++) {
|
|
766
|
+
let cx = this.stack[line.depth], handler = this.parser.skipContextMarkup[cx.type];
|
|
767
|
+
if (!handler) throw new Error("Unhandled block context " + Type[cx.type]);
|
|
768
|
+
let marks = this.line.markers.length;
|
|
769
|
+
if (!handler(cx, this, line)) {
|
|
770
|
+
if (this.line.markers.length > marks) cx.end = this.line.markers[this.line.markers.length - 1].to;
|
|
771
|
+
line.forward();
|
|
772
|
+
break;
|
|
773
|
+
}
|
|
774
|
+
line.forward();
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
lineChunkAt(pos) {
|
|
778
|
+
let next = this.input.chunk(pos), text;
|
|
779
|
+
if (!this.input.lineChunks) {
|
|
780
|
+
let eol = next.indexOf("\n");
|
|
781
|
+
text = eol < 0 ? next : next.slice(0, eol);
|
|
782
|
+
} else text = next == "\n" ? "" : next;
|
|
783
|
+
return pos + text.length > this.to ? text.slice(0, this.to - pos) : text;
|
|
784
|
+
}
|
|
785
|
+
/**
|
|
786
|
+
The end position of the previous line.
|
|
787
|
+
*/
|
|
788
|
+
prevLineEnd() {
|
|
789
|
+
return this.atEnd ? this.lineStart : this.lineStart - 1;
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
@internal
|
|
793
|
+
*/
|
|
794
|
+
startContext(type, start, value = 0) {
|
|
795
|
+
this.block = CompositeBlock.create(type, value, this.lineStart + start, this.block.hash, this.lineStart + this.line.text.length);
|
|
796
|
+
this.stack.push(this.block);
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
Start a composite block. Should only be called from [block
|
|
800
|
+
parser functions](#BlockParser.parse) that return null.
|
|
801
|
+
*/
|
|
802
|
+
startComposite(type, start, value = 0) {
|
|
803
|
+
this.startContext(this.parser.getNodeType(type), start, value);
|
|
804
|
+
}
|
|
805
|
+
/**
|
|
806
|
+
@internal
|
|
807
|
+
*/
|
|
808
|
+
addNode(block, from, to) {
|
|
809
|
+
if (typeof block == "number") block = new Tree(this.parser.nodeSet.types[block], none, none, (to !== null && to !== void 0 ? to : this.prevLineEnd()) - from);
|
|
810
|
+
this.block.addChild(block, from - this.block.from);
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
Add a block element. Can be called by [block
|
|
814
|
+
parsers](#BlockParser.parse).
|
|
815
|
+
*/
|
|
816
|
+
addElement(elt) {
|
|
817
|
+
this.block.addChild(elt.toTree(this.parser.nodeSet), elt.from - this.block.from);
|
|
818
|
+
}
|
|
819
|
+
/**
|
|
820
|
+
Add a block element from a [leaf parser](#LeafBlockParser). This
|
|
821
|
+
makes sure any extra composite block markup (such as blockquote
|
|
822
|
+
markers) inside the block are also added to the syntax tree.
|
|
823
|
+
*/
|
|
824
|
+
addLeafElement(leaf, elt) {
|
|
825
|
+
this.addNode(this.buffer.writeElements(injectMarks(elt.children, leaf.marks), -elt.from).finish(elt.type, elt.to - elt.from), elt.from);
|
|
826
|
+
}
|
|
827
|
+
/**
|
|
828
|
+
@internal
|
|
829
|
+
*/
|
|
830
|
+
finishContext() {
|
|
831
|
+
let cx = this.stack.pop();
|
|
832
|
+
let top = this.stack[this.stack.length - 1];
|
|
833
|
+
top.addChild(cx.toTree(this.parser.nodeSet), cx.from - top.from);
|
|
834
|
+
this.block = top;
|
|
835
|
+
}
|
|
836
|
+
finish() {
|
|
837
|
+
while (this.stack.length > 1) this.finishContext();
|
|
838
|
+
return this.addGaps(this.block.toTree(this.parser.nodeSet, this.lineStart));
|
|
839
|
+
}
|
|
840
|
+
addGaps(tree) {
|
|
841
|
+
return this.ranges.length > 1 ? injectGaps(this.ranges, 0, tree.topNode, this.ranges[0].from, this.reusePlaceholders) : tree;
|
|
842
|
+
}
|
|
843
|
+
/**
|
|
844
|
+
@internal
|
|
845
|
+
*/
|
|
846
|
+
finishLeaf(leaf) {
|
|
847
|
+
for (let parser of leaf.parsers) if (parser.finish(this, leaf)) return;
|
|
848
|
+
let inline = injectMarks(this.parser.parseInline(leaf.content, leaf.start), leaf.marks);
|
|
849
|
+
this.addNode(this.buffer.writeElements(inline, -leaf.start).finish(Type.Paragraph, leaf.content.length), leaf.start);
|
|
850
|
+
}
|
|
851
|
+
elt(type, from, to, children) {
|
|
852
|
+
if (typeof type == "string") return elt(this.parser.getNodeType(type), from, to, children);
|
|
853
|
+
return new TreeElement(type, from);
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
@internal
|
|
857
|
+
*/
|
|
858
|
+
get buffer() {
|
|
859
|
+
return new Buffer(this.parser.nodeSet);
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
function injectGaps(ranges, rangeI, tree, offset, dummies) {
|
|
863
|
+
let rangeEnd = ranges[rangeI].to;
|
|
864
|
+
let children = [], positions = [], start = tree.from + offset;
|
|
865
|
+
function movePastNext(upto, inclusive) {
|
|
866
|
+
while (inclusive ? upto >= rangeEnd : upto > rangeEnd) {
|
|
867
|
+
let size = ranges[rangeI + 1].from - rangeEnd;
|
|
868
|
+
offset += size;
|
|
869
|
+
upto += size;
|
|
870
|
+
rangeI++;
|
|
871
|
+
rangeEnd = ranges[rangeI].to;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
for (let ch = tree.firstChild; ch; ch = ch.nextSibling) {
|
|
875
|
+
movePastNext(ch.from + offset, true);
|
|
876
|
+
let from = ch.from + offset, node, reuse = dummies.get(ch.tree);
|
|
877
|
+
if (reuse) node = reuse;
|
|
878
|
+
else if (ch.to + offset > rangeEnd) {
|
|
879
|
+
node = injectGaps(ranges, rangeI, ch, offset, dummies);
|
|
880
|
+
movePastNext(ch.to + offset, false);
|
|
881
|
+
} else node = ch.toTree();
|
|
882
|
+
children.push(node);
|
|
883
|
+
positions.push(from - start);
|
|
884
|
+
}
|
|
885
|
+
movePastNext(tree.to + offset, false);
|
|
886
|
+
return new Tree(tree.type, children, positions, tree.to + offset - start, tree.tree ? tree.tree.propValues : void 0);
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
A Markdown parser configuration.
|
|
890
|
+
*/
|
|
891
|
+
var MarkdownParser = class MarkdownParser extends Parser {
|
|
892
|
+
/**
|
|
893
|
+
@internal
|
|
894
|
+
*/
|
|
895
|
+
constructor(nodeSet, blockParsers, leafBlockParsers, blockNames, endLeafBlock, skipContextMarkup, inlineParsers, inlineNames, wrappers) {
|
|
896
|
+
super();
|
|
897
|
+
this.nodeSet = nodeSet;
|
|
898
|
+
this.blockParsers = blockParsers;
|
|
899
|
+
this.leafBlockParsers = leafBlockParsers;
|
|
900
|
+
this.blockNames = blockNames;
|
|
901
|
+
this.endLeafBlock = endLeafBlock;
|
|
902
|
+
this.skipContextMarkup = skipContextMarkup;
|
|
903
|
+
this.inlineParsers = inlineParsers;
|
|
904
|
+
this.inlineNames = inlineNames;
|
|
905
|
+
this.wrappers = wrappers;
|
|
906
|
+
/**
|
|
907
|
+
@internal
|
|
908
|
+
*/
|
|
909
|
+
this.nodeTypes = Object.create(null);
|
|
910
|
+
for (let t of nodeSet.types) this.nodeTypes[t.name] = t.id;
|
|
911
|
+
}
|
|
912
|
+
createParse(input, fragments, ranges) {
|
|
913
|
+
let parse = new BlockContext(this, input, fragments, ranges);
|
|
914
|
+
for (let w of this.wrappers) parse = w(parse, input, fragments, ranges);
|
|
915
|
+
return parse;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
Reconfigure the parser.
|
|
919
|
+
*/
|
|
920
|
+
configure(spec) {
|
|
921
|
+
let config = resolveConfig(spec);
|
|
922
|
+
if (!config) return this;
|
|
923
|
+
let { nodeSet, skipContextMarkup } = this;
|
|
924
|
+
let blockParsers = this.blockParsers.slice(), leafBlockParsers = this.leafBlockParsers.slice(), blockNames = this.blockNames.slice(), inlineParsers = this.inlineParsers.slice(), inlineNames = this.inlineNames.slice(), endLeafBlock = this.endLeafBlock.slice(), wrappers = this.wrappers;
|
|
925
|
+
if (nonEmpty(config.defineNodes)) {
|
|
926
|
+
skipContextMarkup = Object.assign({}, skipContextMarkup);
|
|
927
|
+
let nodeTypes = nodeSet.types.slice(), styles;
|
|
928
|
+
for (let s of config.defineNodes) {
|
|
929
|
+
let { name, block, composite, style } = typeof s == "string" ? { name: s } : s;
|
|
930
|
+
if (nodeTypes.some((t) => t.name == name)) continue;
|
|
931
|
+
if (composite) skipContextMarkup[nodeTypes.length] = (bl, cx, line) => composite(cx, line, bl.value);
|
|
932
|
+
let id = nodeTypes.length;
|
|
933
|
+
let group = composite ? ["Block", "BlockContext"] : !block ? void 0 : id >= Type.ATXHeading1 && id <= Type.SetextHeading2 ? [
|
|
934
|
+
"Block",
|
|
935
|
+
"LeafBlock",
|
|
936
|
+
"Heading"
|
|
937
|
+
] : ["Block", "LeafBlock"];
|
|
938
|
+
nodeTypes.push(NodeType.define({
|
|
939
|
+
id,
|
|
940
|
+
name,
|
|
941
|
+
props: group && [[NodeProp.group, group]]
|
|
942
|
+
}));
|
|
943
|
+
if (style) {
|
|
944
|
+
if (!styles) styles = {};
|
|
945
|
+
if (Array.isArray(style) || style instanceof Tag) styles[name] = style;
|
|
946
|
+
else Object.assign(styles, style);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
nodeSet = new NodeSet(nodeTypes);
|
|
950
|
+
if (styles) nodeSet = nodeSet.extend(styleTags(styles));
|
|
951
|
+
}
|
|
952
|
+
if (nonEmpty(config.props)) nodeSet = nodeSet.extend(...config.props);
|
|
953
|
+
if (nonEmpty(config.remove)) for (let rm of config.remove) {
|
|
954
|
+
let block = this.blockNames.indexOf(rm), inline = this.inlineNames.indexOf(rm);
|
|
955
|
+
if (block > -1) blockParsers[block] = leafBlockParsers[block] = void 0;
|
|
956
|
+
if (inline > -1) inlineParsers[inline] = void 0;
|
|
957
|
+
}
|
|
958
|
+
if (nonEmpty(config.parseBlock)) for (let spec of config.parseBlock) {
|
|
959
|
+
let found = blockNames.indexOf(spec.name);
|
|
960
|
+
if (found > -1) {
|
|
961
|
+
blockParsers[found] = spec.parse;
|
|
962
|
+
leafBlockParsers[found] = spec.leaf;
|
|
963
|
+
} else {
|
|
964
|
+
let pos = spec.before ? findName(blockNames, spec.before) : spec.after ? findName(blockNames, spec.after) + 1 : blockNames.length - 1;
|
|
965
|
+
blockParsers.splice(pos, 0, spec.parse);
|
|
966
|
+
leafBlockParsers.splice(pos, 0, spec.leaf);
|
|
967
|
+
blockNames.splice(pos, 0, spec.name);
|
|
968
|
+
}
|
|
969
|
+
if (spec.endLeaf) endLeafBlock.push(spec.endLeaf);
|
|
970
|
+
}
|
|
971
|
+
if (nonEmpty(config.parseInline)) for (let spec of config.parseInline) {
|
|
972
|
+
let found = inlineNames.indexOf(spec.name);
|
|
973
|
+
if (found > -1) inlineParsers[found] = spec.parse;
|
|
974
|
+
else {
|
|
975
|
+
let pos = spec.before ? findName(inlineNames, spec.before) : spec.after ? findName(inlineNames, spec.after) + 1 : inlineNames.length - 1;
|
|
976
|
+
inlineParsers.splice(pos, 0, spec.parse);
|
|
977
|
+
inlineNames.splice(pos, 0, spec.name);
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
if (config.wrap) wrappers = wrappers.concat(config.wrap);
|
|
981
|
+
return new MarkdownParser(nodeSet, blockParsers, leafBlockParsers, blockNames, endLeafBlock, skipContextMarkup, inlineParsers, inlineNames, wrappers);
|
|
982
|
+
}
|
|
983
|
+
/**
|
|
984
|
+
@internal
|
|
985
|
+
*/
|
|
986
|
+
getNodeType(name) {
|
|
987
|
+
let found = this.nodeTypes[name];
|
|
988
|
+
if (found == null) throw new RangeError(`Unknown node type '${name}'`);
|
|
989
|
+
return found;
|
|
990
|
+
}
|
|
991
|
+
/**
|
|
992
|
+
Parse the given piece of inline text at the given offset,
|
|
993
|
+
returning an array of [`Element`](#Element) objects representing
|
|
994
|
+
the inline content.
|
|
995
|
+
*/
|
|
996
|
+
parseInline(text, offset) {
|
|
997
|
+
let cx = new InlineContext(this, text, offset);
|
|
998
|
+
outer: for (let pos = offset; pos < cx.end;) {
|
|
999
|
+
let next = cx.char(pos);
|
|
1000
|
+
for (let token of this.inlineParsers) if (token) {
|
|
1001
|
+
let result = token(cx, next, pos);
|
|
1002
|
+
if (result >= 0) {
|
|
1003
|
+
pos = result;
|
|
1004
|
+
continue outer;
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
pos++;
|
|
1008
|
+
}
|
|
1009
|
+
return cx.resolveMarkers(0);
|
|
1010
|
+
}
|
|
1011
|
+
};
|
|
1012
|
+
function nonEmpty(a) {
|
|
1013
|
+
return a != null && a.length > 0;
|
|
1014
|
+
}
|
|
1015
|
+
function resolveConfig(spec) {
|
|
1016
|
+
if (!Array.isArray(spec)) return spec;
|
|
1017
|
+
if (spec.length == 0) return null;
|
|
1018
|
+
let conf = resolveConfig(spec[0]);
|
|
1019
|
+
if (spec.length == 1) return conf;
|
|
1020
|
+
let rest = resolveConfig(spec.slice(1));
|
|
1021
|
+
if (!rest || !conf) return conf || rest;
|
|
1022
|
+
let conc = (a, b) => (a || none).concat(b || none);
|
|
1023
|
+
let wrapA = conf.wrap, wrapB = rest.wrap;
|
|
1024
|
+
return {
|
|
1025
|
+
props: conc(conf.props, rest.props),
|
|
1026
|
+
defineNodes: conc(conf.defineNodes, rest.defineNodes),
|
|
1027
|
+
parseBlock: conc(conf.parseBlock, rest.parseBlock),
|
|
1028
|
+
parseInline: conc(conf.parseInline, rest.parseInline),
|
|
1029
|
+
remove: conc(conf.remove, rest.remove),
|
|
1030
|
+
wrap: !wrapA ? wrapB : !wrapB ? wrapA : (inner, input, fragments, ranges) => wrapA(wrapB(inner, input, fragments, ranges), input, fragments, ranges)
|
|
1031
|
+
};
|
|
1032
|
+
}
|
|
1033
|
+
function findName(names, name) {
|
|
1034
|
+
let found = names.indexOf(name);
|
|
1035
|
+
if (found < 0) throw new RangeError(`Position specified relative to unknown parser ${name}`);
|
|
1036
|
+
return found;
|
|
1037
|
+
}
|
|
1038
|
+
let nodeTypes = [NodeType.none];
|
|
1039
|
+
for (let i = 1, name; name = Type[i]; i++) nodeTypes[i] = NodeType.define({
|
|
1040
|
+
id: i,
|
|
1041
|
+
name,
|
|
1042
|
+
props: i >= Type.Escape ? [] : [[NodeProp.group, i in DefaultSkipMarkup ? ["Block", "BlockContext"] : ["Block", "LeafBlock"]]],
|
|
1043
|
+
top: name == "Document"
|
|
1044
|
+
});
|
|
1045
|
+
const none = [];
|
|
1046
|
+
var Buffer = class {
|
|
1047
|
+
constructor(nodeSet) {
|
|
1048
|
+
this.nodeSet = nodeSet;
|
|
1049
|
+
this.content = [];
|
|
1050
|
+
this.nodes = [];
|
|
1051
|
+
}
|
|
1052
|
+
write(type, from, to, children = 0) {
|
|
1053
|
+
this.content.push(type, from, to, 4 + children * 4);
|
|
1054
|
+
return this;
|
|
1055
|
+
}
|
|
1056
|
+
writeElements(elts, offset = 0) {
|
|
1057
|
+
for (let e of elts) e.writeTo(this, offset);
|
|
1058
|
+
return this;
|
|
1059
|
+
}
|
|
1060
|
+
finish(type, length) {
|
|
1061
|
+
return Tree.build({
|
|
1062
|
+
buffer: this.content,
|
|
1063
|
+
nodeSet: this.nodeSet,
|
|
1064
|
+
reused: this.nodes,
|
|
1065
|
+
topID: type,
|
|
1066
|
+
length
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
/**
|
|
1071
|
+
Elements are used to compose syntax nodes during parsing.
|
|
1072
|
+
*/
|
|
1073
|
+
var Element = class {
|
|
1074
|
+
/**
|
|
1075
|
+
@internal
|
|
1076
|
+
*/
|
|
1077
|
+
constructor(type, from, to, children = none) {
|
|
1078
|
+
this.type = type;
|
|
1079
|
+
this.from = from;
|
|
1080
|
+
this.to = to;
|
|
1081
|
+
this.children = children;
|
|
1082
|
+
}
|
|
1083
|
+
/**
|
|
1084
|
+
@internal
|
|
1085
|
+
*/
|
|
1086
|
+
writeTo(buf, offset) {
|
|
1087
|
+
let startOff = buf.content.length;
|
|
1088
|
+
buf.writeElements(this.children, offset);
|
|
1089
|
+
buf.content.push(this.type, this.from + offset, this.to + offset, buf.content.length + 4 - startOff);
|
|
1090
|
+
}
|
|
1091
|
+
/**
|
|
1092
|
+
@internal
|
|
1093
|
+
*/
|
|
1094
|
+
toTree(nodeSet) {
|
|
1095
|
+
return new Buffer(nodeSet).writeElements(this.children, -this.from).finish(this.type, this.to - this.from);
|
|
1096
|
+
}
|
|
1097
|
+
};
|
|
1098
|
+
var TreeElement = class {
|
|
1099
|
+
constructor(tree, from) {
|
|
1100
|
+
this.tree = tree;
|
|
1101
|
+
this.from = from;
|
|
1102
|
+
}
|
|
1103
|
+
get to() {
|
|
1104
|
+
return this.from + this.tree.length;
|
|
1105
|
+
}
|
|
1106
|
+
get type() {
|
|
1107
|
+
return this.tree.type.id;
|
|
1108
|
+
}
|
|
1109
|
+
get children() {
|
|
1110
|
+
return none;
|
|
1111
|
+
}
|
|
1112
|
+
writeTo(buf, offset) {
|
|
1113
|
+
buf.nodes.push(this.tree);
|
|
1114
|
+
buf.content.push(buf.nodes.length - 1, this.from + offset, this.to + offset, -1);
|
|
1115
|
+
}
|
|
1116
|
+
toTree() {
|
|
1117
|
+
return this.tree;
|
|
1118
|
+
}
|
|
1119
|
+
};
|
|
1120
|
+
function elt(type, from, to, children) {
|
|
1121
|
+
return new Element(type, from, to, children);
|
|
1122
|
+
}
|
|
1123
|
+
const EmphasisUnderscore = {
|
|
1124
|
+
resolve: "Emphasis",
|
|
1125
|
+
mark: "EmphasisMark"
|
|
1126
|
+
};
|
|
1127
|
+
const EmphasisAsterisk = {
|
|
1128
|
+
resolve: "Emphasis",
|
|
1129
|
+
mark: "EmphasisMark"
|
|
1130
|
+
};
|
|
1131
|
+
const LinkStart = {}, ImageStart = {};
|
|
1132
|
+
var InlineDelimiter = class {
|
|
1133
|
+
constructor(type, from, to, side) {
|
|
1134
|
+
this.type = type;
|
|
1135
|
+
this.from = from;
|
|
1136
|
+
this.to = to;
|
|
1137
|
+
this.side = side;
|
|
1138
|
+
}
|
|
1139
|
+
};
|
|
1140
|
+
const Escapable = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
|
|
1141
|
+
let Punctuation = /[!"#$%&'()*+,\-.\/:;<=>?@\[\\\]^_`{|}~\xA1\u2010-\u2027]/;
|
|
1142
|
+
try {
|
|
1143
|
+
Punctuation = /* @__PURE__ */ new RegExp("[\\p{S}|\\p{P}]", "u");
|
|
1144
|
+
} catch (_) {}
|
|
1145
|
+
const DefaultInline = {
|
|
1146
|
+
Escape(cx, next, start) {
|
|
1147
|
+
if (next != 92 || start == cx.end - 1) return -1;
|
|
1148
|
+
let escaped = cx.char(start + 1);
|
|
1149
|
+
for (let i = 0; i < 32; i++) if (Escapable.charCodeAt(i) == escaped) return cx.append(elt(Type.Escape, start, start + 2));
|
|
1150
|
+
return -1;
|
|
1151
|
+
},
|
|
1152
|
+
Entity(cx, next, start) {
|
|
1153
|
+
if (next != 38) return -1;
|
|
1154
|
+
let m = /^(?:#\d+|#x[a-f\d]+|\w+);/i.exec(cx.slice(start + 1, start + 31));
|
|
1155
|
+
return m ? cx.append(elt(Type.Entity, start, start + 1 + m[0].length)) : -1;
|
|
1156
|
+
},
|
|
1157
|
+
InlineCode(cx, next, start) {
|
|
1158
|
+
if (next != 96 || start && cx.char(start - 1) == 96) return -1;
|
|
1159
|
+
let pos = start + 1;
|
|
1160
|
+
while (pos < cx.end && cx.char(pos) == 96) pos++;
|
|
1161
|
+
let size = pos - start, curSize = 0;
|
|
1162
|
+
for (; pos < cx.end; pos++) if (cx.char(pos) == 96) {
|
|
1163
|
+
curSize++;
|
|
1164
|
+
if (curSize == size && cx.char(pos + 1) != 96) return cx.append(elt(Type.InlineCode, start, pos + 1, [elt(Type.CodeMark, start, start + size), elt(Type.CodeMark, pos + 1 - size, pos + 1)]));
|
|
1165
|
+
} else curSize = 0;
|
|
1166
|
+
return -1;
|
|
1167
|
+
},
|
|
1168
|
+
HTMLTag(cx, next, start) {
|
|
1169
|
+
if (next != 60 || start == cx.end - 1) return -1;
|
|
1170
|
+
let after = cx.slice(start + 1, cx.end);
|
|
1171
|
+
let url = /^(?:[a-z][-\w+.]+:[^\s>]+|[a-z\d.!#$%&'*+/=?^_`{|}~-]+@[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?(?:\.[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?)*)>/i.exec(after);
|
|
1172
|
+
if (url) return cx.append(elt(Type.Autolink, start, start + 1 + url[0].length, [
|
|
1173
|
+
elt(Type.LinkMark, start, start + 1),
|
|
1174
|
+
elt(Type.URL, start + 1, start + url[0].length),
|
|
1175
|
+
elt(Type.LinkMark, start + url[0].length, start + 1 + url[0].length)
|
|
1176
|
+
]));
|
|
1177
|
+
let comment = /^!--[^>](?:-[^-]|[^-])*?-->/i.exec(after);
|
|
1178
|
+
if (comment) return cx.append(elt(Type.Comment, start, start + 1 + comment[0].length));
|
|
1179
|
+
let procInst = /^\?[^]*?\?>/.exec(after);
|
|
1180
|
+
if (procInst) return cx.append(elt(Type.ProcessingInstruction, start, start + 1 + procInst[0].length));
|
|
1181
|
+
let m = /^(?:![A-Z][^]*?>|!\[CDATA\[[^]*?\]\]>|\/\s*[a-zA-Z][\w-]*\s*>|\s*[a-zA-Z][\w-]*(\s+[a-zA-Z:_][\w-.:]*(?:\s*=\s*(?:[^\s"'=<>`]+|'[^']*'|"[^"]*"))?)*\s*(\/\s*)?>)/.exec(after);
|
|
1182
|
+
if (!m) return -1;
|
|
1183
|
+
return cx.append(elt(Type.HTMLTag, start, start + 1 + m[0].length));
|
|
1184
|
+
},
|
|
1185
|
+
Emphasis(cx, next, start) {
|
|
1186
|
+
if (next != 95 && next != 42) return -1;
|
|
1187
|
+
let pos = start + 1;
|
|
1188
|
+
while (cx.char(pos) == next) pos++;
|
|
1189
|
+
let before = cx.slice(start - 1, start), after = cx.slice(pos, pos + 1);
|
|
1190
|
+
let pBefore = Punctuation.test(before), pAfter = Punctuation.test(after);
|
|
1191
|
+
let sBefore = /\s|^$/.test(before), sAfter = /\s|^$/.test(after);
|
|
1192
|
+
let leftFlanking = !sAfter && (!pAfter || sBefore || pBefore);
|
|
1193
|
+
let rightFlanking = !sBefore && (!pBefore || sAfter || pAfter);
|
|
1194
|
+
let canOpen = leftFlanking && (next == 42 || !rightFlanking || pBefore);
|
|
1195
|
+
let canClose = rightFlanking && (next == 42 || !leftFlanking || pAfter);
|
|
1196
|
+
return cx.append(new InlineDelimiter(next == 95 ? EmphasisUnderscore : EmphasisAsterisk, start, pos, (canOpen ? 1 : 0) | (canClose ? 2 : 0)));
|
|
1197
|
+
},
|
|
1198
|
+
HardBreak(cx, next, start) {
|
|
1199
|
+
if (next == 92 && cx.char(start + 1) == 10) return cx.append(elt(Type.HardBreak, start, start + 2));
|
|
1200
|
+
if (next == 32) {
|
|
1201
|
+
let pos = start + 1;
|
|
1202
|
+
while (cx.char(pos) == 32) pos++;
|
|
1203
|
+
if (cx.char(pos) == 10 && pos >= start + 2) return cx.append(elt(Type.HardBreak, start, pos + 1));
|
|
1204
|
+
}
|
|
1205
|
+
return -1;
|
|
1206
|
+
},
|
|
1207
|
+
Link(cx, next, start) {
|
|
1208
|
+
return next == 91 ? cx.append(new InlineDelimiter(LinkStart, start, start + 1, 1)) : -1;
|
|
1209
|
+
},
|
|
1210
|
+
Image(cx, next, start) {
|
|
1211
|
+
return next == 33 && cx.char(start + 1) == 91 ? cx.append(new InlineDelimiter(ImageStart, start, start + 2, 1)) : -1;
|
|
1212
|
+
},
|
|
1213
|
+
LinkEnd(cx, next, start) {
|
|
1214
|
+
if (next != 93) return -1;
|
|
1215
|
+
for (let i = cx.parts.length - 1; i >= 0; i--) {
|
|
1216
|
+
let part = cx.parts[i];
|
|
1217
|
+
if (part instanceof InlineDelimiter && (part.type == LinkStart || part.type == ImageStart)) {
|
|
1218
|
+
if (!part.side || cx.skipSpace(part.to) == start && !/[(\[]/.test(cx.slice(start + 1, start + 2))) {
|
|
1219
|
+
cx.parts[i] = null;
|
|
1220
|
+
return -1;
|
|
1221
|
+
}
|
|
1222
|
+
let content = cx.takeContent(i);
|
|
1223
|
+
let link = cx.parts[i] = finishLink(cx, content, part.type == LinkStart ? Type.Link : Type.Image, part.from, start + 1);
|
|
1224
|
+
if (part.type == LinkStart) for (let j = 0; j < i; j++) {
|
|
1225
|
+
let p = cx.parts[j];
|
|
1226
|
+
if (p instanceof InlineDelimiter && p.type == LinkStart) p.side = 0;
|
|
1227
|
+
}
|
|
1228
|
+
return link.to;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
return -1;
|
|
1232
|
+
}
|
|
1233
|
+
};
|
|
1234
|
+
function finishLink(cx, content, type, start, startPos) {
|
|
1235
|
+
let { text } = cx, next = cx.char(startPos), endPos = startPos;
|
|
1236
|
+
content.unshift(elt(Type.LinkMark, start, start + (type == Type.Image ? 2 : 1)));
|
|
1237
|
+
content.push(elt(Type.LinkMark, startPos - 1, startPos));
|
|
1238
|
+
if (next == 40) {
|
|
1239
|
+
let pos = cx.skipSpace(startPos + 1);
|
|
1240
|
+
let dest = parseURL(text, pos - cx.offset, cx.offset), title;
|
|
1241
|
+
if (dest) {
|
|
1242
|
+
pos = cx.skipSpace(dest.to);
|
|
1243
|
+
if (pos != dest.to) {
|
|
1244
|
+
title = parseLinkTitle(text, pos - cx.offset, cx.offset);
|
|
1245
|
+
if (title) pos = cx.skipSpace(title.to);
|
|
1246
|
+
}
|
|
1247
|
+
}
|
|
1248
|
+
if (cx.char(pos) == 41) {
|
|
1249
|
+
content.push(elt(Type.LinkMark, startPos, startPos + 1));
|
|
1250
|
+
endPos = pos + 1;
|
|
1251
|
+
if (dest) content.push(dest);
|
|
1252
|
+
if (title) content.push(title);
|
|
1253
|
+
content.push(elt(Type.LinkMark, pos, endPos));
|
|
1254
|
+
}
|
|
1255
|
+
} else if (next == 91) {
|
|
1256
|
+
let label = parseLinkLabel(text, startPos - cx.offset, cx.offset, false);
|
|
1257
|
+
if (label) {
|
|
1258
|
+
content.push(label);
|
|
1259
|
+
endPos = label.to;
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
return elt(type, start, endPos, content);
|
|
1263
|
+
}
|
|
1264
|
+
function parseURL(text, start, offset) {
|
|
1265
|
+
if (text.charCodeAt(start) == 60) {
|
|
1266
|
+
for (let pos = start + 1; pos < text.length; pos++) {
|
|
1267
|
+
let ch = text.charCodeAt(pos);
|
|
1268
|
+
if (ch == 62) return elt(Type.URL, start + offset, pos + 1 + offset);
|
|
1269
|
+
if (ch == 60 || ch == 10) return false;
|
|
1270
|
+
}
|
|
1271
|
+
return null;
|
|
1272
|
+
} else {
|
|
1273
|
+
let depth = 0, pos = start;
|
|
1274
|
+
for (let escaped = false; pos < text.length; pos++) {
|
|
1275
|
+
let ch = text.charCodeAt(pos);
|
|
1276
|
+
if (space(ch)) break;
|
|
1277
|
+
else if (escaped) escaped = false;
|
|
1278
|
+
else if (ch == 40) depth++;
|
|
1279
|
+
else if (ch == 41) {
|
|
1280
|
+
if (!depth) break;
|
|
1281
|
+
depth--;
|
|
1282
|
+
} else if (ch == 92) escaped = true;
|
|
1283
|
+
}
|
|
1284
|
+
return pos > start ? elt(Type.URL, start + offset, pos + offset) : pos == text.length ? null : false;
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
function parseLinkTitle(text, start, offset) {
|
|
1288
|
+
let next = text.charCodeAt(start);
|
|
1289
|
+
if (next != 39 && next != 34 && next != 40) return false;
|
|
1290
|
+
let end = next == 40 ? 41 : next;
|
|
1291
|
+
for (let pos = start + 1, escaped = false; pos < text.length; pos++) {
|
|
1292
|
+
let ch = text.charCodeAt(pos);
|
|
1293
|
+
if (escaped) escaped = false;
|
|
1294
|
+
else if (ch == end) return elt(Type.LinkTitle, start + offset, pos + 1 + offset);
|
|
1295
|
+
else if (ch == 92) escaped = true;
|
|
1296
|
+
}
|
|
1297
|
+
return null;
|
|
1298
|
+
}
|
|
1299
|
+
function parseLinkLabel(text, start, offset, requireNonWS) {
|
|
1300
|
+
for (let escaped = false, pos = start + 1, end = Math.min(text.length, pos + 999); pos < end; pos++) {
|
|
1301
|
+
let ch = text.charCodeAt(pos);
|
|
1302
|
+
if (escaped) escaped = false;
|
|
1303
|
+
else if (ch == 93) return requireNonWS ? false : elt(Type.LinkLabel, start + offset, pos + 1 + offset);
|
|
1304
|
+
else {
|
|
1305
|
+
if (requireNonWS && !space(ch)) requireNonWS = false;
|
|
1306
|
+
if (ch == 91) return false;
|
|
1307
|
+
else if (ch == 92) escaped = true;
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
return null;
|
|
1311
|
+
}
|
|
1312
|
+
/**
|
|
1313
|
+
Inline parsing functions get access to this context, and use it to
|
|
1314
|
+
read the content and emit syntax nodes.
|
|
1315
|
+
*/
|
|
1316
|
+
var InlineContext = class {
|
|
1317
|
+
/**
|
|
1318
|
+
@internal
|
|
1319
|
+
*/
|
|
1320
|
+
constructor(parser, text, offset) {
|
|
1321
|
+
this.parser = parser;
|
|
1322
|
+
this.text = text;
|
|
1323
|
+
this.offset = offset;
|
|
1324
|
+
/**
|
|
1325
|
+
@internal
|
|
1326
|
+
*/
|
|
1327
|
+
this.parts = [];
|
|
1328
|
+
}
|
|
1329
|
+
/**
|
|
1330
|
+
Get the character code at the given (document-relative)
|
|
1331
|
+
position.
|
|
1332
|
+
*/
|
|
1333
|
+
char(pos) {
|
|
1334
|
+
return pos >= this.end ? -1 : this.text.charCodeAt(pos - this.offset);
|
|
1335
|
+
}
|
|
1336
|
+
/**
|
|
1337
|
+
The position of the end of this inline section.
|
|
1338
|
+
*/
|
|
1339
|
+
get end() {
|
|
1340
|
+
return this.offset + this.text.length;
|
|
1341
|
+
}
|
|
1342
|
+
/**
|
|
1343
|
+
Get a substring of this inline section. Again uses
|
|
1344
|
+
document-relative positions.
|
|
1345
|
+
*/
|
|
1346
|
+
slice(from, to) {
|
|
1347
|
+
return this.text.slice(from - this.offset, to - this.offset);
|
|
1348
|
+
}
|
|
1349
|
+
/**
|
|
1350
|
+
@internal
|
|
1351
|
+
*/
|
|
1352
|
+
append(elt) {
|
|
1353
|
+
this.parts.push(elt);
|
|
1354
|
+
return elt.to;
|
|
1355
|
+
}
|
|
1356
|
+
/**
|
|
1357
|
+
Add a [delimiter](#DelimiterType) at this given position. `open`
|
|
1358
|
+
and `close` indicate whether this delimiter is opening, closing,
|
|
1359
|
+
or both. Returns the end of the delimiter, for convenient
|
|
1360
|
+
returning from [parse functions](#InlineParser.parse).
|
|
1361
|
+
*/
|
|
1362
|
+
addDelimiter(type, from, to, open, close) {
|
|
1363
|
+
return this.append(new InlineDelimiter(type, from, to, (open ? 1 : 0) | (close ? 2 : 0)));
|
|
1364
|
+
}
|
|
1365
|
+
/**
|
|
1366
|
+
Returns true when there is an unmatched link or image opening
|
|
1367
|
+
token before the current position.
|
|
1368
|
+
*/
|
|
1369
|
+
get hasOpenLink() {
|
|
1370
|
+
for (let i = this.parts.length - 1; i >= 0; i--) {
|
|
1371
|
+
let part = this.parts[i];
|
|
1372
|
+
if (part instanceof InlineDelimiter && (part.type == LinkStart || part.type == ImageStart)) return true;
|
|
1373
|
+
}
|
|
1374
|
+
return false;
|
|
1375
|
+
}
|
|
1376
|
+
/**
|
|
1377
|
+
Add an inline element. Returns the end of the element.
|
|
1378
|
+
*/
|
|
1379
|
+
addElement(elt) {
|
|
1380
|
+
return this.append(elt);
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
Resolve markers between this.parts.length and from, wrapping matched markers in the
|
|
1384
|
+
appropriate node and updating the content of this.parts. @internal
|
|
1385
|
+
*/
|
|
1386
|
+
resolveMarkers(from) {
|
|
1387
|
+
for (let i = from; i < this.parts.length; i++) {
|
|
1388
|
+
let close = this.parts[i];
|
|
1389
|
+
if (!(close instanceof InlineDelimiter && close.type.resolve && close.side & 2)) continue;
|
|
1390
|
+
let emp = close.type == EmphasisUnderscore || close.type == EmphasisAsterisk;
|
|
1391
|
+
let closeSize = close.to - close.from;
|
|
1392
|
+
let open, j = i - 1;
|
|
1393
|
+
for (; j >= from; j--) {
|
|
1394
|
+
let part = this.parts[j];
|
|
1395
|
+
if (part instanceof InlineDelimiter && part.side & 1 && part.type == close.type && !(emp && (close.side & 1 || part.side & 2) && (part.to - part.from + closeSize) % 3 == 0 && ((part.to - part.from) % 3 || closeSize % 3))) {
|
|
1396
|
+
open = part;
|
|
1397
|
+
break;
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
if (!open) continue;
|
|
1401
|
+
let type = close.type.resolve, content = [];
|
|
1402
|
+
let start = open.from, end = close.to;
|
|
1403
|
+
if (emp) {
|
|
1404
|
+
let size = Math.min(2, open.to - open.from, closeSize);
|
|
1405
|
+
start = open.to - size;
|
|
1406
|
+
end = close.from + size;
|
|
1407
|
+
type = size == 1 ? "Emphasis" : "StrongEmphasis";
|
|
1408
|
+
}
|
|
1409
|
+
if (open.type.mark) content.push(this.elt(open.type.mark, start, open.to));
|
|
1410
|
+
for (let k = j + 1; k < i; k++) {
|
|
1411
|
+
if (this.parts[k] instanceof Element) content.push(this.parts[k]);
|
|
1412
|
+
this.parts[k] = null;
|
|
1413
|
+
}
|
|
1414
|
+
if (close.type.mark) content.push(this.elt(close.type.mark, close.from, end));
|
|
1415
|
+
let element = this.elt(type, start, end, content);
|
|
1416
|
+
this.parts[j] = emp && open.from != start ? new InlineDelimiter(open.type, open.from, start, open.side) : null;
|
|
1417
|
+
if (this.parts[i] = emp && close.to != end ? new InlineDelimiter(close.type, end, close.to, close.side) : null) this.parts.splice(i, 0, element);
|
|
1418
|
+
else this.parts[i] = element;
|
|
1419
|
+
}
|
|
1420
|
+
let result = [];
|
|
1421
|
+
for (let i = from; i < this.parts.length; i++) {
|
|
1422
|
+
let part = this.parts[i];
|
|
1423
|
+
if (part instanceof Element) result.push(part);
|
|
1424
|
+
}
|
|
1425
|
+
return result;
|
|
1426
|
+
}
|
|
1427
|
+
/**
|
|
1428
|
+
Find an opening delimiter of the given type. Returns `null` if
|
|
1429
|
+
no delimiter is found, or an index that can be passed to
|
|
1430
|
+
[`takeContent`](#InlineContext.takeContent) otherwise.
|
|
1431
|
+
*/
|
|
1432
|
+
findOpeningDelimiter(type) {
|
|
1433
|
+
for (let i = this.parts.length - 1; i >= 0; i--) {
|
|
1434
|
+
let part = this.parts[i];
|
|
1435
|
+
if (part instanceof InlineDelimiter && part.type == type && part.side & 1) return i;
|
|
1436
|
+
}
|
|
1437
|
+
return null;
|
|
1438
|
+
}
|
|
1439
|
+
/**
|
|
1440
|
+
Remove all inline elements and delimiters starting from the
|
|
1441
|
+
given index (which you should get from
|
|
1442
|
+
[`findOpeningDelimiter`](#InlineContext.findOpeningDelimiter),
|
|
1443
|
+
resolve delimiters inside of them, and return them as an array
|
|
1444
|
+
of elements.
|
|
1445
|
+
*/
|
|
1446
|
+
takeContent(startIndex) {
|
|
1447
|
+
let content = this.resolveMarkers(startIndex);
|
|
1448
|
+
this.parts.length = startIndex;
|
|
1449
|
+
return content;
|
|
1450
|
+
}
|
|
1451
|
+
/**
|
|
1452
|
+
Return the delimiter at the given index. Mostly useful to get
|
|
1453
|
+
additional info out of a delimiter index returned by
|
|
1454
|
+
[`findOpeningDelimiter`](#InlineContext.findOpeningDelimiter).
|
|
1455
|
+
Returns null if there is no delimiter at this index.
|
|
1456
|
+
*/
|
|
1457
|
+
getDelimiterAt(index) {
|
|
1458
|
+
let part = this.parts[index];
|
|
1459
|
+
return part instanceof InlineDelimiter ? part : null;
|
|
1460
|
+
}
|
|
1461
|
+
/**
|
|
1462
|
+
Skip space after the given (document) position, returning either
|
|
1463
|
+
the position of the next non-space character or the end of the
|
|
1464
|
+
section.
|
|
1465
|
+
*/
|
|
1466
|
+
skipSpace(from) {
|
|
1467
|
+
return skipSpace(this.text, from - this.offset) + this.offset;
|
|
1468
|
+
}
|
|
1469
|
+
elt(type, from, to, children) {
|
|
1470
|
+
if (typeof type == "string") return elt(this.parser.getNodeType(type), from, to, children);
|
|
1471
|
+
return new TreeElement(type, from);
|
|
1472
|
+
}
|
|
1473
|
+
};
|
|
1474
|
+
/**
|
|
1475
|
+
The opening delimiter type used by the standard link parser.
|
|
1476
|
+
*/
|
|
1477
|
+
InlineContext.linkStart = LinkStart;
|
|
1478
|
+
/**
|
|
1479
|
+
Opening delimiter type used for standard images.
|
|
1480
|
+
*/
|
|
1481
|
+
InlineContext.imageStart = ImageStart;
|
|
1482
|
+
function injectMarks(elements, marks) {
|
|
1483
|
+
if (!marks.length) return elements;
|
|
1484
|
+
if (!elements.length) return marks;
|
|
1485
|
+
let elts = elements.slice(), eI = 0;
|
|
1486
|
+
for (let mark of marks) {
|
|
1487
|
+
while (eI < elts.length && elts[eI].to < mark.to) eI++;
|
|
1488
|
+
if (eI < elts.length && elts[eI].from < mark.from) {
|
|
1489
|
+
let e = elts[eI];
|
|
1490
|
+
if (e instanceof Element) elts[eI] = new Element(e.type, e.from, e.to, injectMarks(e.children, [mark]));
|
|
1491
|
+
} else elts.splice(eI++, 0, mark);
|
|
1492
|
+
}
|
|
1493
|
+
return elts;
|
|
1494
|
+
}
|
|
1495
|
+
const NotLast = [
|
|
1496
|
+
Type.CodeBlock,
|
|
1497
|
+
Type.ListItem,
|
|
1498
|
+
Type.OrderedList,
|
|
1499
|
+
Type.BulletList
|
|
1500
|
+
];
|
|
1501
|
+
var FragmentCursor = class {
|
|
1502
|
+
constructor(fragments, input) {
|
|
1503
|
+
this.fragments = fragments;
|
|
1504
|
+
this.input = input;
|
|
1505
|
+
this.i = 0;
|
|
1506
|
+
this.fragment = null;
|
|
1507
|
+
this.fragmentEnd = -1;
|
|
1508
|
+
this.cursor = null;
|
|
1509
|
+
if (fragments.length) this.fragment = fragments[this.i++];
|
|
1510
|
+
}
|
|
1511
|
+
nextFragment() {
|
|
1512
|
+
this.fragment = this.i < this.fragments.length ? this.fragments[this.i++] : null;
|
|
1513
|
+
this.cursor = null;
|
|
1514
|
+
this.fragmentEnd = -1;
|
|
1515
|
+
}
|
|
1516
|
+
moveTo(pos, lineStart) {
|
|
1517
|
+
while (this.fragment && this.fragment.to <= pos) this.nextFragment();
|
|
1518
|
+
if (!this.fragment || this.fragment.from > (pos ? pos - 1 : 0)) return false;
|
|
1519
|
+
if (this.fragmentEnd < 0) {
|
|
1520
|
+
let end = this.fragment.to;
|
|
1521
|
+
while (end > 0 && this.input.read(end - 1, end) != "\n") end--;
|
|
1522
|
+
this.fragmentEnd = end ? end - 1 : 0;
|
|
1523
|
+
}
|
|
1524
|
+
let c = this.cursor;
|
|
1525
|
+
if (!c) {
|
|
1526
|
+
c = this.cursor = this.fragment.tree.cursor();
|
|
1527
|
+
c.firstChild();
|
|
1528
|
+
}
|
|
1529
|
+
let rPos = pos + this.fragment.offset;
|
|
1530
|
+
while (c.to <= rPos) if (!c.parent()) return false;
|
|
1531
|
+
for (;;) {
|
|
1532
|
+
if (c.from >= rPos) return this.fragment.from <= lineStart;
|
|
1533
|
+
if (!c.childAfter(rPos)) return false;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
matches(hash) {
|
|
1537
|
+
let tree = this.cursor.tree;
|
|
1538
|
+
return tree && tree.prop(NodeProp.contextHash) == hash;
|
|
1539
|
+
}
|
|
1540
|
+
takeNodes(cx) {
|
|
1541
|
+
let cur = this.cursor, off = this.fragment.offset, fragEnd = this.fragmentEnd - (this.fragment.openEnd ? 1 : 0);
|
|
1542
|
+
let start = cx.absoluteLineStart, end = start, blockI = cx.block.children.length;
|
|
1543
|
+
let prevEnd = end, prevI = blockI;
|
|
1544
|
+
for (;;) {
|
|
1545
|
+
if (cur.to - off > fragEnd) {
|
|
1546
|
+
if (cur.type.isAnonymous && cur.firstChild()) continue;
|
|
1547
|
+
break;
|
|
1548
|
+
}
|
|
1549
|
+
let pos = toRelative(cur.from - off, cx.ranges);
|
|
1550
|
+
if (cur.to - off <= cx.ranges[cx.rangeI].to) cx.addNode(cur.tree, pos);
|
|
1551
|
+
else {
|
|
1552
|
+
let dummy = new Tree(cx.parser.nodeSet.types[Type.Paragraph], [], [], 0, cx.block.hashProp);
|
|
1553
|
+
cx.reusePlaceholders.set(dummy, cur.tree);
|
|
1554
|
+
cx.addNode(dummy, pos);
|
|
1555
|
+
}
|
|
1556
|
+
if (cur.type.is("Block")) {
|
|
1557
|
+
if (NotLast.indexOf(cur.type.id) < 0) {
|
|
1558
|
+
end = cur.to - off;
|
|
1559
|
+
blockI = cx.block.children.length;
|
|
1560
|
+
} else {
|
|
1561
|
+
end = prevEnd;
|
|
1562
|
+
blockI = prevI;
|
|
1563
|
+
}
|
|
1564
|
+
prevEnd = cur.to - off;
|
|
1565
|
+
prevI = cx.block.children.length;
|
|
1566
|
+
}
|
|
1567
|
+
if (!cur.nextSibling()) break;
|
|
1568
|
+
}
|
|
1569
|
+
while (cx.block.children.length > blockI) {
|
|
1570
|
+
cx.block.children.pop();
|
|
1571
|
+
cx.block.positions.pop();
|
|
1572
|
+
}
|
|
1573
|
+
return end - start;
|
|
1574
|
+
}
|
|
1575
|
+
};
|
|
1576
|
+
function toRelative(abs, ranges) {
|
|
1577
|
+
let pos = abs;
|
|
1578
|
+
for (let i = 1; i < ranges.length; i++) {
|
|
1579
|
+
let gapFrom = ranges[i - 1].to, gapTo = ranges[i].from;
|
|
1580
|
+
if (gapFrom < abs) pos -= gapTo - gapFrom;
|
|
1581
|
+
}
|
|
1582
|
+
return pos;
|
|
1583
|
+
}
|
|
1584
|
+
const markdownHighlighting = styleTags({
|
|
1585
|
+
"Blockquote/...": tags.quote,
|
|
1586
|
+
HorizontalRule: tags.contentSeparator,
|
|
1587
|
+
"ATXHeading1/... SetextHeading1/...": tags.heading1,
|
|
1588
|
+
"ATXHeading2/... SetextHeading2/...": tags.heading2,
|
|
1589
|
+
"ATXHeading3/...": tags.heading3,
|
|
1590
|
+
"ATXHeading4/...": tags.heading4,
|
|
1591
|
+
"ATXHeading5/...": tags.heading5,
|
|
1592
|
+
"ATXHeading6/...": tags.heading6,
|
|
1593
|
+
"Comment CommentBlock": tags.comment,
|
|
1594
|
+
Escape: tags.escape,
|
|
1595
|
+
Entity: tags.character,
|
|
1596
|
+
"Emphasis/...": tags.emphasis,
|
|
1597
|
+
"StrongEmphasis/...": tags.strong,
|
|
1598
|
+
"Link/... Image/...": tags.link,
|
|
1599
|
+
"OrderedList/... BulletList/...": tags.list,
|
|
1600
|
+
"BlockQuote/...": tags.quote,
|
|
1601
|
+
"InlineCode CodeText": tags.monospace,
|
|
1602
|
+
"URL Autolink": tags.url,
|
|
1603
|
+
"HeaderMark HardBreak QuoteMark ListMark LinkMark EmphasisMark CodeMark": tags.processingInstruction,
|
|
1604
|
+
"CodeInfo LinkLabel": tags.labelName,
|
|
1605
|
+
LinkTitle: tags.string,
|
|
1606
|
+
Paragraph: tags.content
|
|
1607
|
+
});
|
|
1608
|
+
/**
|
|
1609
|
+
The default CommonMark parser.
|
|
1610
|
+
*/
|
|
1611
|
+
const parser = new MarkdownParser(new NodeSet(nodeTypes).extend(markdownHighlighting), Object.keys(DefaultBlockParsers).map((n) => DefaultBlockParsers[n]), Object.keys(DefaultBlockParsers).map((n) => DefaultLeafBlocks[n]), Object.keys(DefaultBlockParsers), DefaultEndLeaf, DefaultSkipMarkup, Object.keys(DefaultInline).map((n) => DefaultInline[n]), Object.keys(DefaultInline), []);
|
|
1612
|
+
function leftOverSpace(node, from, to) {
|
|
1613
|
+
let ranges = [];
|
|
1614
|
+
for (let n = node.firstChild, pos = from;; n = n.nextSibling) {
|
|
1615
|
+
let nextPos = n ? n.from : to;
|
|
1616
|
+
if (nextPos > pos) ranges.push({
|
|
1617
|
+
from: pos,
|
|
1618
|
+
to: nextPos
|
|
1619
|
+
});
|
|
1620
|
+
if (!n) break;
|
|
1621
|
+
pos = n.to;
|
|
1622
|
+
}
|
|
1623
|
+
return ranges;
|
|
1624
|
+
}
|
|
1625
|
+
/**
|
|
1626
|
+
Create a Markdown extension to enable nested parsing on code
|
|
1627
|
+
blocks and/or embedded HTML.
|
|
1628
|
+
*/
|
|
1629
|
+
function parseCode(config) {
|
|
1630
|
+
let { codeParser, htmlParser } = config;
|
|
1631
|
+
return { wrap: parseMixed((node, input) => {
|
|
1632
|
+
let id = node.type.id;
|
|
1633
|
+
if (codeParser && (id == Type.CodeBlock || id == Type.FencedCode)) {
|
|
1634
|
+
let info = "";
|
|
1635
|
+
if (id == Type.FencedCode) {
|
|
1636
|
+
let infoNode = node.node.getChild(Type.CodeInfo);
|
|
1637
|
+
if (infoNode) info = input.read(infoNode.from, infoNode.to);
|
|
1638
|
+
}
|
|
1639
|
+
let parser = codeParser(info);
|
|
1640
|
+
if (parser) return {
|
|
1641
|
+
parser,
|
|
1642
|
+
overlay: (node) => node.type.id == Type.CodeText,
|
|
1643
|
+
bracketed: id == Type.FencedCode
|
|
1644
|
+
};
|
|
1645
|
+
} else if (htmlParser && (id == Type.HTMLBlock || id == Type.HTMLTag || id == Type.CommentBlock)) return {
|
|
1646
|
+
parser: htmlParser,
|
|
1647
|
+
overlay: leftOverSpace(node.node, node.from, node.to)
|
|
1648
|
+
};
|
|
1649
|
+
return null;
|
|
1650
|
+
}) };
|
|
1651
|
+
}
|
|
1652
|
+
const StrikethroughDelim = {
|
|
1653
|
+
resolve: "Strikethrough",
|
|
1654
|
+
mark: "StrikethroughMark"
|
|
1655
|
+
};
|
|
1656
|
+
/**
|
|
1657
|
+
An extension that implements
|
|
1658
|
+
[GFM-style](https://github.github.com/gfm/#strikethrough-extension-)
|
|
1659
|
+
Strikethrough syntax using `~~` delimiters.
|
|
1660
|
+
*/
|
|
1661
|
+
const Strikethrough = {
|
|
1662
|
+
defineNodes: [{
|
|
1663
|
+
name: "Strikethrough",
|
|
1664
|
+
style: { "Strikethrough/...": tags.strikethrough }
|
|
1665
|
+
}, {
|
|
1666
|
+
name: "StrikethroughMark",
|
|
1667
|
+
style: tags.processingInstruction
|
|
1668
|
+
}],
|
|
1669
|
+
parseInline: [{
|
|
1670
|
+
name: "Strikethrough",
|
|
1671
|
+
parse(cx, next, pos) {
|
|
1672
|
+
if (next != 126 || cx.char(pos + 1) != 126 || cx.char(pos + 2) == 126) return -1;
|
|
1673
|
+
let before = cx.slice(pos - 1, pos), after = cx.slice(pos + 2, pos + 3);
|
|
1674
|
+
let sBefore = /\s|^$/.test(before), sAfter = /\s|^$/.test(after);
|
|
1675
|
+
let pBefore = Punctuation.test(before), pAfter = Punctuation.test(after);
|
|
1676
|
+
return cx.addDelimiter(StrikethroughDelim, pos, pos + 2, !sAfter && (!pAfter || sBefore || pBefore), !sBefore && (!pBefore || sAfter || pAfter));
|
|
1677
|
+
},
|
|
1678
|
+
after: "Emphasis"
|
|
1679
|
+
}]
|
|
1680
|
+
};
|
|
1681
|
+
function parseRow(cx, line, startI = 0, elts, offset = 0) {
|
|
1682
|
+
let count = 0, first = true, cellStart = -1, cellEnd = -1, esc = false;
|
|
1683
|
+
let parseCell = () => {
|
|
1684
|
+
elts.push(cx.elt("TableCell", offset + cellStart, offset + cellEnd, cx.parser.parseInline(line.slice(cellStart, cellEnd), offset + cellStart)));
|
|
1685
|
+
};
|
|
1686
|
+
for (let i = startI; i < line.length; i++) {
|
|
1687
|
+
let next = line.charCodeAt(i);
|
|
1688
|
+
if (next == 124 && !esc) {
|
|
1689
|
+
if (!first || cellStart > -1) count++;
|
|
1690
|
+
first = false;
|
|
1691
|
+
if (elts) {
|
|
1692
|
+
if (cellStart > -1) parseCell();
|
|
1693
|
+
elts.push(cx.elt("TableDelimiter", i + offset, i + offset + 1));
|
|
1694
|
+
}
|
|
1695
|
+
cellStart = cellEnd = -1;
|
|
1696
|
+
} else if (esc || next != 32 && next != 9) {
|
|
1697
|
+
if (cellStart < 0) cellStart = i;
|
|
1698
|
+
cellEnd = i + 1;
|
|
1699
|
+
}
|
|
1700
|
+
esc = !esc && next == 92;
|
|
1701
|
+
}
|
|
1702
|
+
if (cellStart > -1) {
|
|
1703
|
+
count++;
|
|
1704
|
+
if (elts) parseCell();
|
|
1705
|
+
}
|
|
1706
|
+
return count;
|
|
1707
|
+
}
|
|
1708
|
+
function hasPipe(str, start) {
|
|
1709
|
+
for (let i = start; i < str.length; i++) {
|
|
1710
|
+
let next = str.charCodeAt(i);
|
|
1711
|
+
if (next == 124) return true;
|
|
1712
|
+
if (next == 92) i++;
|
|
1713
|
+
}
|
|
1714
|
+
return false;
|
|
1715
|
+
}
|
|
1716
|
+
const delimiterLine = /^\|?(\s*:?-+:?\s*\|)+(\s*:?-+:?\s*)?$/;
|
|
1717
|
+
var TableParser = class {
|
|
1718
|
+
constructor() {
|
|
1719
|
+
this.rows = null;
|
|
1720
|
+
}
|
|
1721
|
+
nextLine(cx, line, leaf) {
|
|
1722
|
+
if (this.rows == null) {
|
|
1723
|
+
this.rows = false;
|
|
1724
|
+
let lineText;
|
|
1725
|
+
if ((line.next == 45 || line.next == 58 || line.next == 124) && delimiterLine.test(lineText = line.text.slice(line.pos))) {
|
|
1726
|
+
let firstRow = [];
|
|
1727
|
+
if (parseRow(cx, leaf.content, 0, firstRow, leaf.start) == parseRow(cx, lineText, line.pos)) this.rows = [cx.elt("TableHeader", leaf.start, leaf.start + leaf.content.length, firstRow), cx.elt("TableDelimiter", cx.lineStart + line.pos, cx.lineStart + line.text.length)];
|
|
1728
|
+
}
|
|
1729
|
+
} else if (this.rows) {
|
|
1730
|
+
let content = [];
|
|
1731
|
+
parseRow(cx, line.text, line.pos, content, cx.lineStart);
|
|
1732
|
+
this.rows.push(cx.elt("TableRow", cx.lineStart + line.pos, cx.lineStart + line.text.length, content));
|
|
1733
|
+
}
|
|
1734
|
+
return false;
|
|
1735
|
+
}
|
|
1736
|
+
finish(cx, leaf) {
|
|
1737
|
+
if (!this.rows) return false;
|
|
1738
|
+
cx.addLeafElement(leaf, cx.elt("Table", leaf.start, leaf.start + leaf.content.length, this.rows));
|
|
1739
|
+
return true;
|
|
1740
|
+
}
|
|
1741
|
+
};
|
|
1742
|
+
/**
|
|
1743
|
+
This extension provides
|
|
1744
|
+
[GFM-style](https://github.github.com/gfm/#tables-extension-)
|
|
1745
|
+
tables, using syntax like this:
|
|
1746
|
+
|
|
1747
|
+
```
|
|
1748
|
+
| head 1 | head 2 |
|
|
1749
|
+
| --- | --- |
|
|
1750
|
+
| cell 1 | cell 2 |
|
|
1751
|
+
```
|
|
1752
|
+
*/
|
|
1753
|
+
const Table = {
|
|
1754
|
+
defineNodes: [
|
|
1755
|
+
{
|
|
1756
|
+
name: "Table",
|
|
1757
|
+
block: true
|
|
1758
|
+
},
|
|
1759
|
+
{
|
|
1760
|
+
name: "TableHeader",
|
|
1761
|
+
style: { "TableHeader/...": tags.heading }
|
|
1762
|
+
},
|
|
1763
|
+
"TableRow",
|
|
1764
|
+
{
|
|
1765
|
+
name: "TableCell",
|
|
1766
|
+
style: tags.content
|
|
1767
|
+
},
|
|
1768
|
+
{
|
|
1769
|
+
name: "TableDelimiter",
|
|
1770
|
+
style: tags.processingInstruction
|
|
1771
|
+
}
|
|
1772
|
+
],
|
|
1773
|
+
parseBlock: [{
|
|
1774
|
+
name: "Table",
|
|
1775
|
+
leaf(_, leaf) {
|
|
1776
|
+
return hasPipe(leaf.content, 0) ? new TableParser() : null;
|
|
1777
|
+
},
|
|
1778
|
+
endLeaf(cx, line, leaf) {
|
|
1779
|
+
if (leaf.parsers.some((p) => p instanceof TableParser) || !hasPipe(line.text, line.basePos)) return false;
|
|
1780
|
+
let next = cx.peekLine();
|
|
1781
|
+
return delimiterLine.test(next) && parseRow(cx, line.text, line.basePos) == parseRow(cx, next, line.basePos);
|
|
1782
|
+
},
|
|
1783
|
+
before: "SetextHeading"
|
|
1784
|
+
}]
|
|
1785
|
+
};
|
|
1786
|
+
var TaskParser = class {
|
|
1787
|
+
nextLine() {
|
|
1788
|
+
return false;
|
|
1789
|
+
}
|
|
1790
|
+
finish(cx, leaf) {
|
|
1791
|
+
cx.addLeafElement(leaf, cx.elt("Task", leaf.start, leaf.start + leaf.content.length, [cx.elt("TaskMarker", leaf.start, leaf.start + 3), ...cx.parser.parseInline(leaf.content.slice(3), leaf.start + 3)]));
|
|
1792
|
+
return true;
|
|
1793
|
+
}
|
|
1794
|
+
};
|
|
1795
|
+
/**
|
|
1796
|
+
Extension providing
|
|
1797
|
+
[GFM-style](https://github.github.com/gfm/#task-list-items-extension-)
|
|
1798
|
+
task list items, where list items can be prefixed with `[ ]` or
|
|
1799
|
+
`[x]` to add a checkbox.
|
|
1800
|
+
*/
|
|
1801
|
+
const TaskList = {
|
|
1802
|
+
defineNodes: [{
|
|
1803
|
+
name: "Task",
|
|
1804
|
+
block: true,
|
|
1805
|
+
style: tags.list
|
|
1806
|
+
}, {
|
|
1807
|
+
name: "TaskMarker",
|
|
1808
|
+
style: tags.atom
|
|
1809
|
+
}],
|
|
1810
|
+
parseBlock: [{
|
|
1811
|
+
name: "TaskList",
|
|
1812
|
+
leaf(cx, leaf) {
|
|
1813
|
+
return /^\[[ xX]\][ \t]/.test(leaf.content) && cx.parentType().name == "ListItem" ? new TaskParser() : null;
|
|
1814
|
+
},
|
|
1815
|
+
after: "SetextHeading"
|
|
1816
|
+
}]
|
|
1817
|
+
};
|
|
1818
|
+
const autolinkRE = /(www\.)|(https?:\/\/)|([\w.+-]{1,100}@)|(mailto:|xmpp:)/gy;
|
|
1819
|
+
const urlRE = /[\w-]+(\.[\w-]+)+(\/[^\s<]*)?/gy;
|
|
1820
|
+
const lastTwoDomainWords = /[\w-]+\.[\w-]+($|\/)/;
|
|
1821
|
+
const emailRE = /[\w.+-]+@[\w-]+(\.[\w.-]+)+/gy;
|
|
1822
|
+
const xmppResourceRE = /\/[a-zA-Z\d@.]+/gy;
|
|
1823
|
+
function count(str, from, to, ch) {
|
|
1824
|
+
let result = 0;
|
|
1825
|
+
for (let i = from; i < to; i++) if (str[i] == ch) result++;
|
|
1826
|
+
return result;
|
|
1827
|
+
}
|
|
1828
|
+
function autolinkURLEnd(text, from) {
|
|
1829
|
+
urlRE.lastIndex = from;
|
|
1830
|
+
let m = urlRE.exec(text);
|
|
1831
|
+
if (!m || lastTwoDomainWords.exec(m[0])[0].indexOf("_") > -1) return -1;
|
|
1832
|
+
let end = from + m[0].length;
|
|
1833
|
+
for (;;) {
|
|
1834
|
+
let last = text[end - 1], m;
|
|
1835
|
+
if (/[?!.,:*_~]/.test(last) || last == ")" && count(text, from, end, ")") > count(text, from, end, "(")) end--;
|
|
1836
|
+
else if (last == ";" && (m = /&(?:#\d+|#x[a-f\d]+|\w+);$/.exec(text.slice(from, end)))) end = from + m.index;
|
|
1837
|
+
else break;
|
|
1838
|
+
}
|
|
1839
|
+
return end;
|
|
1840
|
+
}
|
|
1841
|
+
function autolinkEmailEnd(text, from) {
|
|
1842
|
+
emailRE.lastIndex = from;
|
|
1843
|
+
let m = emailRE.exec(text);
|
|
1844
|
+
if (!m) return -1;
|
|
1845
|
+
let last = m[0][m[0].length - 1];
|
|
1846
|
+
return last == "_" || last == "-" ? -1 : from + m[0].length - (last == "." ? 1 : 0);
|
|
1847
|
+
}
|
|
1848
|
+
/**
|
|
1849
|
+
Extension that implements autolinking for
|
|
1850
|
+
`www.`/`http://`/`https://`/`mailto:`/`xmpp:` URLs and email
|
|
1851
|
+
addresses.
|
|
1852
|
+
*/
|
|
1853
|
+
const Autolink = { parseInline: [{
|
|
1854
|
+
name: "Autolink",
|
|
1855
|
+
parse(cx, next, absPos) {
|
|
1856
|
+
let pos = absPos - cx.offset;
|
|
1857
|
+
if (pos && /\w/.test(cx.text[pos - 1])) return -1;
|
|
1858
|
+
autolinkRE.lastIndex = pos;
|
|
1859
|
+
let m = autolinkRE.exec(cx.text), end = -1;
|
|
1860
|
+
if (!m) return -1;
|
|
1861
|
+
if (m[1] || m[2]) {
|
|
1862
|
+
end = autolinkURLEnd(cx.text, pos + m[0].length);
|
|
1863
|
+
if (end > -1 && cx.hasOpenLink) end = pos + /([^\[\]]|\[[^\]]*\])*/.exec(cx.text.slice(pos, end))[0].length;
|
|
1864
|
+
} else if (m[3]) end = autolinkEmailEnd(cx.text, pos);
|
|
1865
|
+
else {
|
|
1866
|
+
end = autolinkEmailEnd(cx.text, pos + m[0].length);
|
|
1867
|
+
if (end > -1 && m[0] == "xmpp:") {
|
|
1868
|
+
xmppResourceRE.lastIndex = end;
|
|
1869
|
+
m = xmppResourceRE.exec(cx.text);
|
|
1870
|
+
if (m) end = m.index + m[0].length;
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
if (end < 0) return -1;
|
|
1874
|
+
cx.addElement(cx.elt("URL", absPos, end + cx.offset));
|
|
1875
|
+
return end + cx.offset;
|
|
1876
|
+
}
|
|
1877
|
+
}] };
|
|
1878
|
+
/**
|
|
1879
|
+
Extension bundle containing [`Table`](#Table),
|
|
1880
|
+
[`TaskList`](#TaskList), [`Strikethrough`](#Strikethrough), and
|
|
1881
|
+
[`Autolink`](#Autolink).
|
|
1882
|
+
*/
|
|
1883
|
+
const GFM = [
|
|
1884
|
+
Table,
|
|
1885
|
+
TaskList,
|
|
1886
|
+
Strikethrough,
|
|
1887
|
+
Autolink
|
|
1888
|
+
];
|
|
1889
|
+
function parseSubSuper(ch, node, mark) {
|
|
1890
|
+
return (cx, next, pos) => {
|
|
1891
|
+
if (next != ch || cx.char(pos + 1) == ch) return -1;
|
|
1892
|
+
let elts = [cx.elt(mark, pos, pos + 1)];
|
|
1893
|
+
for (let i = pos + 1; i < cx.end; i++) {
|
|
1894
|
+
let next = cx.char(i);
|
|
1895
|
+
if (next == ch) return cx.addElement(cx.elt(node, pos, i + 1, elts.concat(cx.elt(mark, i, i + 1))));
|
|
1896
|
+
if (next == 92) elts.push(cx.elt("Escape", i, i++ + 2));
|
|
1897
|
+
if (space(next)) break;
|
|
1898
|
+
}
|
|
1899
|
+
return -1;
|
|
1900
|
+
};
|
|
1901
|
+
}
|
|
1902
|
+
/**
|
|
1903
|
+
Extension providing
|
|
1904
|
+
[Pandoc-style](https://pandoc.org/MANUAL.html#superscripts-and-subscripts)
|
|
1905
|
+
superscript using `^` markers.
|
|
1906
|
+
*/
|
|
1907
|
+
const Superscript = {
|
|
1908
|
+
defineNodes: [{
|
|
1909
|
+
name: "Superscript",
|
|
1910
|
+
style: tags.special(tags.content)
|
|
1911
|
+
}, {
|
|
1912
|
+
name: "SuperscriptMark",
|
|
1913
|
+
style: tags.processingInstruction
|
|
1914
|
+
}],
|
|
1915
|
+
parseInline: [{
|
|
1916
|
+
name: "Superscript",
|
|
1917
|
+
parse: parseSubSuper(94, "Superscript", "SuperscriptMark")
|
|
1918
|
+
}]
|
|
1919
|
+
};
|
|
1920
|
+
/**
|
|
1921
|
+
Extension providing
|
|
1922
|
+
[Pandoc-style](https://pandoc.org/MANUAL.html#superscripts-and-subscripts)
|
|
1923
|
+
subscript using `~` markers.
|
|
1924
|
+
*/
|
|
1925
|
+
const Subscript = {
|
|
1926
|
+
defineNodes: [{
|
|
1927
|
+
name: "Subscript",
|
|
1928
|
+
style: tags.special(tags.content)
|
|
1929
|
+
}, {
|
|
1930
|
+
name: "SubscriptMark",
|
|
1931
|
+
style: tags.processingInstruction
|
|
1932
|
+
}],
|
|
1933
|
+
parseInline: [{
|
|
1934
|
+
name: "Subscript",
|
|
1935
|
+
parse: parseSubSuper(126, "Subscript", "SubscriptMark")
|
|
1936
|
+
}]
|
|
1937
|
+
};
|
|
1938
|
+
/**
|
|
1939
|
+
Extension that parses two colons with only letters, underscores,
|
|
1940
|
+
and numbers between them as `Emoji` nodes.
|
|
1941
|
+
*/
|
|
1942
|
+
const Emoji = {
|
|
1943
|
+
defineNodes: [{
|
|
1944
|
+
name: "Emoji",
|
|
1945
|
+
style: tags.character
|
|
1946
|
+
}],
|
|
1947
|
+
parseInline: [{
|
|
1948
|
+
name: "Emoji",
|
|
1949
|
+
parse(cx, next, pos) {
|
|
1950
|
+
let match;
|
|
1951
|
+
if (next != 58 || !(match = /^[a-zA-Z_0-9]+:/.exec(cx.slice(pos + 1, cx.end)))) return -1;
|
|
1952
|
+
return cx.addElement(cx.elt("Emoji", pos, pos + 1 + match[0].length));
|
|
1953
|
+
}
|
|
1954
|
+
}]
|
|
1955
|
+
};
|
|
1956
|
+
|
|
1957
|
+
//#endregion
|
|
1958
|
+
//#region ../../node_modules/.bun/@codemirror+lang-markdown@6.5.0/node_modules/@codemirror/lang-markdown/dist/index.js
|
|
1959
|
+
const data = /* @__PURE__ */ defineLanguageFacet({ commentTokens: { block: {
|
|
1960
|
+
open: "<!--",
|
|
1961
|
+
close: "-->"
|
|
1962
|
+
} } });
|
|
1963
|
+
const headingProp = /* @__PURE__ */ new NodeProp();
|
|
1964
|
+
const commonmark = /* @__PURE__ */ parser.configure({ props: [
|
|
1965
|
+
/* @__PURE__ */ foldNodeProp.add((type) => {
|
|
1966
|
+
return !type.is("Block") || type.is("Document") || isHeading(type) != null || isList(type) ? void 0 : (tree, state) => ({
|
|
1967
|
+
from: state.doc.lineAt(tree.from).to,
|
|
1968
|
+
to: tree.to
|
|
1969
|
+
});
|
|
1970
|
+
}),
|
|
1971
|
+
/* @__PURE__ */ headingProp.add(isHeading),
|
|
1972
|
+
/* @__PURE__ */ indentNodeProp.add({ Document: () => null }),
|
|
1973
|
+
/* @__PURE__ */ languageDataProp.add({ Document: data })
|
|
1974
|
+
] });
|
|
1975
|
+
function isHeading(type) {
|
|
1976
|
+
let match = /^(?:ATX|Setext)Heading(\d)$/.exec(type.name);
|
|
1977
|
+
return match ? +match[1] : void 0;
|
|
1978
|
+
}
|
|
1979
|
+
function isList(type) {
|
|
1980
|
+
return type.name == "OrderedList" || type.name == "BulletList";
|
|
1981
|
+
}
|
|
1982
|
+
function findSectionEnd(headerNode, level) {
|
|
1983
|
+
let last = headerNode;
|
|
1984
|
+
for (;;) {
|
|
1985
|
+
let next = last.nextSibling, heading;
|
|
1986
|
+
if (!next || (heading = isHeading(next.type)) != null && heading <= level) break;
|
|
1987
|
+
last = next;
|
|
1988
|
+
}
|
|
1989
|
+
return last.to;
|
|
1990
|
+
}
|
|
1991
|
+
const headerIndent = /* @__PURE__ */ foldService.of((state, start, end) => {
|
|
1992
|
+
for (let node = syntaxTree(state).resolveInner(end, -1); node; node = node.parent) {
|
|
1993
|
+
if (node.from < start) break;
|
|
1994
|
+
let heading = node.type.prop(headingProp);
|
|
1995
|
+
if (heading == null) continue;
|
|
1996
|
+
let upto = findSectionEnd(node, heading);
|
|
1997
|
+
if (upto > end) return {
|
|
1998
|
+
from: end,
|
|
1999
|
+
to: upto
|
|
2000
|
+
};
|
|
2001
|
+
}
|
|
2002
|
+
return null;
|
|
2003
|
+
});
|
|
2004
|
+
function mkLang(parser) {
|
|
2005
|
+
return new Language(data, parser, [], "markdown");
|
|
2006
|
+
}
|
|
2007
|
+
/**
|
|
2008
|
+
Language support for strict CommonMark.
|
|
2009
|
+
*/
|
|
2010
|
+
const commonmarkLanguage = /* @__PURE__ */ mkLang(commonmark);
|
|
2011
|
+
/**
|
|
2012
|
+
Language support for [GFM](https://github.github.com/gfm/) plus
|
|
2013
|
+
subscript, superscript, and emoji syntax.
|
|
2014
|
+
*/
|
|
2015
|
+
const markdownLanguage = /* @__PURE__ */ mkLang(/* @__PURE__ */ commonmark.configure([
|
|
2016
|
+
GFM,
|
|
2017
|
+
Subscript,
|
|
2018
|
+
Superscript,
|
|
2019
|
+
Emoji,
|
|
2020
|
+
{ props: [/* @__PURE__ */ foldNodeProp.add({ Table: (tree, state) => ({
|
|
2021
|
+
from: state.doc.lineAt(tree.from).to,
|
|
2022
|
+
to: tree.to
|
|
2023
|
+
}) })] }
|
|
2024
|
+
]));
|
|
2025
|
+
function getCodeParser(languages, defaultLanguage) {
|
|
2026
|
+
return (info) => {
|
|
2027
|
+
if (info && languages) {
|
|
2028
|
+
let found = null;
|
|
2029
|
+
info = /\S*/.exec(info)[0];
|
|
2030
|
+
if (typeof languages == "function") found = languages(info);
|
|
2031
|
+
else found = LanguageDescription.matchLanguageName(languages, info, true);
|
|
2032
|
+
if (found instanceof LanguageDescription) return found.support ? found.support.language.parser : ParseContext.getSkippingParser(found.load());
|
|
2033
|
+
else if (found) return found.parser;
|
|
2034
|
+
}
|
|
2035
|
+
return defaultLanguage ? defaultLanguage.parser : null;
|
|
2036
|
+
};
|
|
2037
|
+
}
|
|
2038
|
+
var Context = class {
|
|
2039
|
+
constructor(node, from, to, spaceBefore, spaceAfter, type, item) {
|
|
2040
|
+
this.node = node;
|
|
2041
|
+
this.from = from;
|
|
2042
|
+
this.to = to;
|
|
2043
|
+
this.spaceBefore = spaceBefore;
|
|
2044
|
+
this.spaceAfter = spaceAfter;
|
|
2045
|
+
this.type = type;
|
|
2046
|
+
this.item = item;
|
|
2047
|
+
}
|
|
2048
|
+
blank(maxWidth, trailing = true) {
|
|
2049
|
+
let result = this.spaceBefore + (this.node.name == "Blockquote" ? ">" : "");
|
|
2050
|
+
if (maxWidth != null) {
|
|
2051
|
+
while (result.length < maxWidth) result += " ";
|
|
2052
|
+
return result;
|
|
2053
|
+
} else {
|
|
2054
|
+
for (let i = this.to - this.from - result.length - this.spaceAfter.length; i > 0; i--) result += " ";
|
|
2055
|
+
return result + (trailing ? this.spaceAfter : "");
|
|
2056
|
+
}
|
|
2057
|
+
}
|
|
2058
|
+
marker(doc, add) {
|
|
2059
|
+
let number = this.node.name == "OrderedList" ? String(+itemNumber(this.item, doc)[2] + add) : "";
|
|
2060
|
+
return this.spaceBefore + number + this.type + this.spaceAfter;
|
|
2061
|
+
}
|
|
2062
|
+
};
|
|
2063
|
+
function getContext(node, doc) {
|
|
2064
|
+
let nodes = [], context = [];
|
|
2065
|
+
for (let cur = node; cur; cur = cur.parent) {
|
|
2066
|
+
if (cur.name == "FencedCode") return context;
|
|
2067
|
+
if (cur.name == "ListItem" || cur.name == "Blockquote") nodes.push(cur);
|
|
2068
|
+
}
|
|
2069
|
+
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
2070
|
+
let node = nodes[i], match;
|
|
2071
|
+
let line = doc.lineAt(node.from), startPos = node.from - line.from;
|
|
2072
|
+
if (node.name == "Blockquote" && (match = /^ *>( ?)/.exec(line.text.slice(startPos)))) context.push(new Context(node, startPos, startPos + match[0].length, "", match[1], ">", null));
|
|
2073
|
+
else if (node.name == "ListItem" && node.parent.name == "OrderedList" && (match = /^( *)\d+([.)])( *)/.exec(line.text.slice(startPos)))) {
|
|
2074
|
+
let after = match[3], len = match[0].length;
|
|
2075
|
+
if (after.length >= 4) {
|
|
2076
|
+
after = after.slice(0, after.length - 4);
|
|
2077
|
+
len -= 4;
|
|
2078
|
+
}
|
|
2079
|
+
context.push(new Context(node.parent, startPos, startPos + len, match[1], after, match[2], node));
|
|
2080
|
+
} else if (node.name == "ListItem" && node.parent.name == "BulletList" && (match = /^( *)([-+*])( {1,4}\[[ xX]\])?( +)/.exec(line.text.slice(startPos)))) {
|
|
2081
|
+
let after = match[4], len = match[0].length;
|
|
2082
|
+
if (after.length > 4) {
|
|
2083
|
+
after = after.slice(0, after.length - 4);
|
|
2084
|
+
len -= 4;
|
|
2085
|
+
}
|
|
2086
|
+
let type = match[2];
|
|
2087
|
+
if (match[3]) type += match[3].replace(/[xX]/, " ");
|
|
2088
|
+
context.push(new Context(node.parent, startPos, startPos + len, match[1], after, type, node));
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
return context;
|
|
2092
|
+
}
|
|
2093
|
+
function itemNumber(item, doc) {
|
|
2094
|
+
return /^(\s*)(\d+)(?=[.)])/.exec(doc.sliceString(item.from, item.from + 10));
|
|
2095
|
+
}
|
|
2096
|
+
function renumberList(after, doc, changes, offset = 0) {
|
|
2097
|
+
for (let prev = -1, node = after;;) {
|
|
2098
|
+
if (node.name == "ListItem") {
|
|
2099
|
+
let m = itemNumber(node, doc);
|
|
2100
|
+
let number = +m[2];
|
|
2101
|
+
if (prev >= 0) {
|
|
2102
|
+
if (number != prev + 1) return;
|
|
2103
|
+
changes.push({
|
|
2104
|
+
from: node.from + m[1].length,
|
|
2105
|
+
to: node.from + m[0].length,
|
|
2106
|
+
insert: String(prev + 2 + offset)
|
|
2107
|
+
});
|
|
2108
|
+
}
|
|
2109
|
+
prev = number;
|
|
2110
|
+
}
|
|
2111
|
+
let next = node.nextSibling;
|
|
2112
|
+
if (!next) break;
|
|
2113
|
+
node = next;
|
|
2114
|
+
}
|
|
2115
|
+
}
|
|
2116
|
+
function normalizeIndent(content, state) {
|
|
2117
|
+
let blank = /^[ \t]*/.exec(content)[0].length;
|
|
2118
|
+
if (!blank || state.facet(indentUnit) != " ") return content;
|
|
2119
|
+
let col = countColumn(content, 4, blank);
|
|
2120
|
+
let space = "";
|
|
2121
|
+
for (let i = col; i > 0;) if (i >= 4) {
|
|
2122
|
+
space += " ";
|
|
2123
|
+
i -= 4;
|
|
2124
|
+
} else {
|
|
2125
|
+
space += " ";
|
|
2126
|
+
i--;
|
|
2127
|
+
}
|
|
2128
|
+
return space + content.slice(blank);
|
|
2129
|
+
}
|
|
2130
|
+
/**
|
|
2131
|
+
Returns a command like
|
|
2132
|
+
[`insertNewlineContinueMarkup`](https://codemirror.net/6/docs/ref/#lang-markdown.insertNewlineContinueMarkup),
|
|
2133
|
+
allowing further configuration.
|
|
2134
|
+
*/
|
|
2135
|
+
const insertNewlineContinueMarkupCommand = (config = {}) => ({ state, dispatch }) => {
|
|
2136
|
+
let tree = syntaxTree(state), { doc } = state;
|
|
2137
|
+
let dont = null, changes = state.changeByRange((range) => {
|
|
2138
|
+
if (!range.empty || !markdownLanguage.isActiveAt(state, range.from, -1) && !markdownLanguage.isActiveAt(state, range.from, 1)) return dont = { range };
|
|
2139
|
+
let pos = range.from, line = doc.lineAt(pos);
|
|
2140
|
+
let context = getContext(tree.resolveInner(pos, -1), doc);
|
|
2141
|
+
while (context.length && context[context.length - 1].from > pos - line.from) context.pop();
|
|
2142
|
+
if (!context.length) return dont = { range };
|
|
2143
|
+
let inner = context[context.length - 1];
|
|
2144
|
+
if (inner.to - inner.spaceAfter.length > pos - line.from) return dont = { range };
|
|
2145
|
+
let emptyLine = pos >= inner.to - inner.spaceAfter.length && !/\S/.test(line.text.slice(inner.to));
|
|
2146
|
+
if (inner.item && emptyLine) {
|
|
2147
|
+
let first = inner.node.firstChild, second = inner.node.getChild("ListItem", "ListItem");
|
|
2148
|
+
if (first.to >= pos || second && second.to < pos || line.from > 0 && !/[^\s>]/.test(doc.lineAt(line.from - 1).text) || config.nonTightLists === false) {
|
|
2149
|
+
let next = context.length > 1 ? context[context.length - 2] : null;
|
|
2150
|
+
let delTo, insert = "";
|
|
2151
|
+
if (next && next.item) {
|
|
2152
|
+
delTo = line.from + next.from;
|
|
2153
|
+
insert = next.marker(doc, 1);
|
|
2154
|
+
} else delTo = line.from + (next ? next.to : 0);
|
|
2155
|
+
let changes = [{
|
|
2156
|
+
from: delTo,
|
|
2157
|
+
to: pos,
|
|
2158
|
+
insert
|
|
2159
|
+
}];
|
|
2160
|
+
if (inner.node.name == "OrderedList") renumberList(inner.item, doc, changes, -2);
|
|
2161
|
+
if (next && next.node.name == "OrderedList") renumberList(next.item, doc, changes);
|
|
2162
|
+
return {
|
|
2163
|
+
range: EditorSelection.cursor(delTo + insert.length),
|
|
2164
|
+
changes
|
|
2165
|
+
};
|
|
2166
|
+
} else {
|
|
2167
|
+
let insert = blankLine(context, state, line);
|
|
2168
|
+
return {
|
|
2169
|
+
range: EditorSelection.cursor(pos + insert.length + 1),
|
|
2170
|
+
changes: {
|
|
2171
|
+
from: line.from,
|
|
2172
|
+
insert: insert + state.lineBreak
|
|
2173
|
+
}
|
|
2174
|
+
};
|
|
2175
|
+
}
|
|
2176
|
+
}
|
|
2177
|
+
if (inner.node.name == "Blockquote" && emptyLine && line.from) {
|
|
2178
|
+
let prevLine = doc.lineAt(line.from - 1), quoted = />\s*$/.exec(prevLine.text);
|
|
2179
|
+
if (quoted && quoted.index == inner.from) {
|
|
2180
|
+
let changes = state.changes([{
|
|
2181
|
+
from: prevLine.from + quoted.index,
|
|
2182
|
+
to: prevLine.to
|
|
2183
|
+
}, {
|
|
2184
|
+
from: line.from + inner.from,
|
|
2185
|
+
to: line.to
|
|
2186
|
+
}]);
|
|
2187
|
+
return {
|
|
2188
|
+
range: range.map(changes),
|
|
2189
|
+
changes
|
|
2190
|
+
};
|
|
2191
|
+
}
|
|
2192
|
+
}
|
|
2193
|
+
let changes = [];
|
|
2194
|
+
if (inner.node.name == "OrderedList") renumberList(inner.item, doc, changes);
|
|
2195
|
+
let continued = inner.item && inner.item.from < line.from;
|
|
2196
|
+
let insert = "";
|
|
2197
|
+
if (!continued || /^[\s\d.)\-+*>]*/.exec(line.text)[0].length >= inner.to) for (let i = 0, e = context.length - 1; i <= e; i++) insert += i == e && !continued ? context[i].marker(doc, 1) : context[i].blank(i < e ? countColumn(line.text, 4, context[i + 1].from) - insert.length : null);
|
|
2198
|
+
let from = pos;
|
|
2199
|
+
while (from > line.from && /\s/.test(line.text.charAt(from - line.from - 1))) from--;
|
|
2200
|
+
insert = normalizeIndent(insert, state);
|
|
2201
|
+
if (nonTightList(inner.node, state.doc)) insert = blankLine(context, state, line) + state.lineBreak + insert;
|
|
2202
|
+
changes.push({
|
|
2203
|
+
from,
|
|
2204
|
+
to: pos,
|
|
2205
|
+
insert: state.lineBreak + insert
|
|
2206
|
+
});
|
|
2207
|
+
return {
|
|
2208
|
+
range: EditorSelection.cursor(from + insert.length + 1),
|
|
2209
|
+
changes
|
|
2210
|
+
};
|
|
2211
|
+
});
|
|
2212
|
+
if (dont) return false;
|
|
2213
|
+
dispatch(state.update(changes, {
|
|
2214
|
+
scrollIntoView: true,
|
|
2215
|
+
userEvent: "input"
|
|
2216
|
+
}));
|
|
2217
|
+
return true;
|
|
2218
|
+
};
|
|
2219
|
+
/**
|
|
2220
|
+
This command, when invoked in Markdown context with cursor
|
|
2221
|
+
selection(s), will create a new line with the markup for
|
|
2222
|
+
blockquotes and lists that were active on the old line. If the
|
|
2223
|
+
cursor was directly after the end of the markup for the old line,
|
|
2224
|
+
trailing whitespace and list markers are removed from that line.
|
|
2225
|
+
|
|
2226
|
+
The command does nothing in non-Markdown context, so it should
|
|
2227
|
+
not be used as the only binding for Enter (even in a Markdown
|
|
2228
|
+
document, HTML and code regions might use a different language).
|
|
2229
|
+
*/
|
|
2230
|
+
const insertNewlineContinueMarkup = /* @__PURE__ */ insertNewlineContinueMarkupCommand();
|
|
2231
|
+
function isMark(node) {
|
|
2232
|
+
return node.name == "QuoteMark" || node.name == "ListMark";
|
|
2233
|
+
}
|
|
2234
|
+
function nonTightList(node, doc) {
|
|
2235
|
+
if (node.name != "OrderedList" && node.name != "BulletList") return false;
|
|
2236
|
+
let first = node.firstChild, second = node.getChild("ListItem", "ListItem");
|
|
2237
|
+
if (!second) return false;
|
|
2238
|
+
let line1 = doc.lineAt(first.to), line2 = doc.lineAt(second.from);
|
|
2239
|
+
let empty = /^[\s>]*$/.test(line1.text);
|
|
2240
|
+
return line1.number + (empty ? 0 : 1) < line2.number;
|
|
2241
|
+
}
|
|
2242
|
+
function blankLine(context, state, line) {
|
|
2243
|
+
let insert = "";
|
|
2244
|
+
for (let i = 0, e = context.length - 2; i <= e; i++) insert += context[i].blank(i < e ? countColumn(line.text, 4, context[i + 1].from) - insert.length : null, i < e);
|
|
2245
|
+
return normalizeIndent(insert, state);
|
|
2246
|
+
}
|
|
2247
|
+
function contextNodeForDelete(tree, pos) {
|
|
2248
|
+
let node = tree.resolveInner(pos, -1), scan = pos;
|
|
2249
|
+
if (isMark(node)) {
|
|
2250
|
+
scan = node.from;
|
|
2251
|
+
node = node.parent;
|
|
2252
|
+
}
|
|
2253
|
+
for (let prev; prev = node.childBefore(scan);) if (isMark(prev)) scan = prev.from;
|
|
2254
|
+
else if (prev.name == "OrderedList" || prev.name == "BulletList") {
|
|
2255
|
+
node = prev.lastChild;
|
|
2256
|
+
scan = node.to;
|
|
2257
|
+
} else break;
|
|
2258
|
+
return node;
|
|
2259
|
+
}
|
|
2260
|
+
/**
|
|
2261
|
+
This command will, when invoked in a Markdown context with the
|
|
2262
|
+
cursor directly after list or blockquote markup, delete one level
|
|
2263
|
+
of markup. When the markup is for a list, it will be replaced by
|
|
2264
|
+
spaces on the first invocation (a further invocation will delete
|
|
2265
|
+
the spaces), to make it easy to continue a list.
|
|
2266
|
+
|
|
2267
|
+
When not after Markdown block markup, this command will return
|
|
2268
|
+
false, so it is intended to be bound alongside other deletion
|
|
2269
|
+
commands, with a higher precedence than the more generic commands.
|
|
2270
|
+
*/
|
|
2271
|
+
const deleteMarkupBackward = ({ state, dispatch }) => {
|
|
2272
|
+
let tree = syntaxTree(state);
|
|
2273
|
+
let dont = null, changes = state.changeByRange((range) => {
|
|
2274
|
+
let pos = range.from, { doc } = state;
|
|
2275
|
+
if (range.empty && markdownLanguage.isActiveAt(state, range.from)) {
|
|
2276
|
+
let line = doc.lineAt(pos);
|
|
2277
|
+
let context = getContext(contextNodeForDelete(tree, pos), doc);
|
|
2278
|
+
if (context.length) {
|
|
2279
|
+
let inner = context[context.length - 1];
|
|
2280
|
+
let spaceEnd = inner.to - inner.spaceAfter.length + (inner.spaceAfter ? 1 : 0);
|
|
2281
|
+
if (pos - line.from > spaceEnd && !/\S/.test(line.text.slice(spaceEnd, pos - line.from))) return {
|
|
2282
|
+
range: EditorSelection.cursor(line.from + spaceEnd),
|
|
2283
|
+
changes: {
|
|
2284
|
+
from: line.from + spaceEnd,
|
|
2285
|
+
to: pos
|
|
2286
|
+
}
|
|
2287
|
+
};
|
|
2288
|
+
if (pos - line.from == spaceEnd && (!inner.item || line.from <= inner.item.from || !/\S/.test(line.text.slice(0, inner.to)))) {
|
|
2289
|
+
let start = line.from + inner.from;
|
|
2290
|
+
if (inner.item && inner.node.from < inner.item.from && /\S/.test(line.text.slice(inner.from, inner.to))) {
|
|
2291
|
+
let insert = inner.blank(countColumn(line.text, 4, inner.to) - countColumn(line.text, 4, inner.from));
|
|
2292
|
+
if (start == line.from) insert = normalizeIndent(insert, state);
|
|
2293
|
+
return {
|
|
2294
|
+
range: EditorSelection.cursor(start + insert.length),
|
|
2295
|
+
changes: {
|
|
2296
|
+
from: start,
|
|
2297
|
+
to: line.from + inner.to,
|
|
2298
|
+
insert
|
|
2299
|
+
}
|
|
2300
|
+
};
|
|
2301
|
+
}
|
|
2302
|
+
if (start < pos) return {
|
|
2303
|
+
range: EditorSelection.cursor(start),
|
|
2304
|
+
changes: {
|
|
2305
|
+
from: start,
|
|
2306
|
+
to: pos
|
|
2307
|
+
}
|
|
2308
|
+
};
|
|
2309
|
+
}
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
return dont = { range };
|
|
2313
|
+
});
|
|
2314
|
+
if (dont) return false;
|
|
2315
|
+
dispatch(state.update(changes, {
|
|
2316
|
+
scrollIntoView: true,
|
|
2317
|
+
userEvent: "delete"
|
|
2318
|
+
}));
|
|
2319
|
+
return true;
|
|
2320
|
+
};
|
|
2321
|
+
/**
|
|
2322
|
+
A small keymap with Markdown-specific bindings. Binds Enter to
|
|
2323
|
+
[`insertNewlineContinueMarkup`](https://codemirror.net/6/docs/ref/#lang-markdown.insertNewlineContinueMarkup)
|
|
2324
|
+
and Backspace to
|
|
2325
|
+
[`deleteMarkupBackward`](https://codemirror.net/6/docs/ref/#lang-markdown.deleteMarkupBackward).
|
|
2326
|
+
*/
|
|
2327
|
+
const markdownKeymap = [{
|
|
2328
|
+
key: "Enter",
|
|
2329
|
+
run: insertNewlineContinueMarkup
|
|
2330
|
+
}, {
|
|
2331
|
+
key: "Backspace",
|
|
2332
|
+
run: deleteMarkupBackward
|
|
2333
|
+
}];
|
|
2334
|
+
const htmlNoMatch = /* @__PURE__ */ html({ matchClosingTags: false });
|
|
2335
|
+
/**
|
|
2336
|
+
Markdown language support.
|
|
2337
|
+
*/
|
|
2338
|
+
function markdown(config = {}) {
|
|
2339
|
+
let { codeLanguages, defaultCodeLanguage, addKeymap = true, base: { parser } = commonmarkLanguage, completeHTMLTags = true, pasteURLAsLink: pasteURL = true, htmlTagLanguage = htmlNoMatch } = config;
|
|
2340
|
+
if (!(parser instanceof MarkdownParser)) throw new RangeError("Base parser provided to `markdown` should be a Markdown parser");
|
|
2341
|
+
let extensions = config.extensions ? [config.extensions] : [];
|
|
2342
|
+
let support = [htmlTagLanguage.support, headerIndent], defaultCode;
|
|
2343
|
+
if (pasteURL) support.push(pasteURLAsLink);
|
|
2344
|
+
if (defaultCodeLanguage instanceof LanguageSupport) {
|
|
2345
|
+
support.push(defaultCodeLanguage.support);
|
|
2346
|
+
defaultCode = defaultCodeLanguage.language;
|
|
2347
|
+
} else if (defaultCodeLanguage) defaultCode = defaultCodeLanguage;
|
|
2348
|
+
let codeParser = codeLanguages || defaultCode ? getCodeParser(codeLanguages, defaultCode) : void 0;
|
|
2349
|
+
extensions.push(parseCode({
|
|
2350
|
+
codeParser,
|
|
2351
|
+
htmlParser: htmlTagLanguage.language.parser
|
|
2352
|
+
}));
|
|
2353
|
+
if (addKeymap) support.push(Prec.high(keymap.of(markdownKeymap)));
|
|
2354
|
+
let lang = mkLang(parser.configure(extensions));
|
|
2355
|
+
if (completeHTMLTags) support.push(lang.data.of({ autocomplete: htmlTagCompletion }));
|
|
2356
|
+
return new LanguageSupport(lang, support);
|
|
2357
|
+
}
|
|
2358
|
+
function htmlTagCompletion(context) {
|
|
2359
|
+
let { state, pos } = context, m = /<[:\-\.\w\u00b7-\uffff]*$/.exec(state.sliceDoc(pos - 25, pos));
|
|
2360
|
+
if (!m) return null;
|
|
2361
|
+
let tree = syntaxTree(state).resolveInner(pos, -1);
|
|
2362
|
+
while (tree && !tree.type.isTop) {
|
|
2363
|
+
if (tree.name == "CodeBlock" || tree.name == "FencedCode" || tree.name == "ProcessingInstructionBlock" || tree.name == "CommentBlock" || tree.name == "Link" || tree.name == "Image") return null;
|
|
2364
|
+
tree = tree.parent;
|
|
2365
|
+
}
|
|
2366
|
+
return {
|
|
2367
|
+
from: pos - m[0].length,
|
|
2368
|
+
to: pos,
|
|
2369
|
+
options: htmlTagCompletions(),
|
|
2370
|
+
validFor: /^<[:\-\.\w\u00b7-\uffff]*$/
|
|
2371
|
+
};
|
|
2372
|
+
}
|
|
2373
|
+
let _tagCompletions = null;
|
|
2374
|
+
function htmlTagCompletions() {
|
|
2375
|
+
if (_tagCompletions) return _tagCompletions;
|
|
2376
|
+
let result = htmlCompletionSource(new CompletionContext(EditorState.create({ extensions: htmlNoMatch }), 0, true));
|
|
2377
|
+
return _tagCompletions = result ? result.options : [];
|
|
2378
|
+
}
|
|
2379
|
+
const nonPlainText = /code|horizontalrule|html|link|comment|processing|escape|entity|image|mark|url/i;
|
|
2380
|
+
/**
|
|
2381
|
+
An extension that intercepts pastes when the pasted content looks
|
|
2382
|
+
like a URL and the selection is non-empty and selects regular
|
|
2383
|
+
text, making the selection a link with the pasted URL as target.
|
|
2384
|
+
*/
|
|
2385
|
+
const pasteURLAsLink = /* @__PURE__ */ EditorView.domEventHandlers({ paste: (event, view) => {
|
|
2386
|
+
var _a;
|
|
2387
|
+
let { main } = view.state.selection;
|
|
2388
|
+
if (main.empty) return false;
|
|
2389
|
+
let link = (_a = event.clipboardData) === null || _a === void 0 ? void 0 : _a.getData("text/plain");
|
|
2390
|
+
if (!link || !/^(https?:\/\/|mailto:|xmpp:|www\.)/.test(link)) return false;
|
|
2391
|
+
if (/^www\./.test(link)) link = "https://" + link;
|
|
2392
|
+
if (!markdownLanguage.isActiveAt(view.state, main.from, 1)) return false;
|
|
2393
|
+
let tree = syntaxTree(view.state), crossesNode = false;
|
|
2394
|
+
tree.iterate({
|
|
2395
|
+
from: main.from,
|
|
2396
|
+
to: main.to,
|
|
2397
|
+
enter: (node) => {
|
|
2398
|
+
if (node.from > main.from || nonPlainText.test(node.name)) crossesNode = true;
|
|
2399
|
+
},
|
|
2400
|
+
leave: (node) => {
|
|
2401
|
+
if (node.to < main.to) crossesNode = true;
|
|
2402
|
+
}
|
|
2403
|
+
});
|
|
2404
|
+
if (crossesNode) return false;
|
|
2405
|
+
view.dispatch({
|
|
2406
|
+
changes: [{
|
|
2407
|
+
from: main.from,
|
|
2408
|
+
insert: "["
|
|
2409
|
+
}, {
|
|
2410
|
+
from: main.to,
|
|
2411
|
+
insert: `](${link})`
|
|
2412
|
+
}],
|
|
2413
|
+
userEvent: "input.paste",
|
|
2414
|
+
scrollIntoView: true
|
|
2415
|
+
});
|
|
2416
|
+
return true;
|
|
2417
|
+
} });
|
|
2418
|
+
|
|
2419
|
+
//#endregion
|
|
2420
|
+
export { markdown };
|
|
2421
|
+
//# sourceMappingURL=dist-v09vikKr.js.map
|