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