@incremark/core 0.2.7 → 0.3.1

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.
@@ -0,0 +1,1667 @@
1
+ import { fromMarkdown } from 'mdast-util-from-markdown';
2
+ import { gfmFromMarkdown } from 'mdast-util-gfm';
3
+ import { gfm } from 'micromark-extension-gfm';
4
+ import { gfmFootnoteFromMarkdown } from 'mdast-util-gfm-footnote';
5
+ import { mathFromMarkdown } from 'mdast-util-math';
6
+ import { directive } from 'micromark-extension-directive';
7
+ import { directiveFromMarkdown } from 'mdast-util-directive';
8
+ import { codes, types, constants } from 'micromark-util-symbol';
9
+ import { factorySpace } from 'micromark-factory-space';
10
+ import { markdownLineEnding, markdownLineEndingOrSpace } from 'micromark-util-character';
11
+ import { factoryDestination } from 'micromark-factory-destination';
12
+ import { factoryTitle } from 'micromark-factory-title';
13
+ import { factoryLabel } from 'micromark-factory-label';
14
+ import { factoryWhitespace } from 'micromark-factory-whitespace';
15
+ import { gfmFootnote } from 'micromark-extension-gfm-footnote';
16
+ import { normalizeIdentifier } from 'micromark-util-normalize-identifier';
17
+
18
+ // src/parser/ast/MicromarkAstBuilder.ts
19
+ function mathFlow(_options) {
20
+ return {
21
+ tokenize: tokenizeMathFenced,
22
+ concrete: true,
23
+ name: "mathFlow"
24
+ };
25
+ }
26
+ var nonLazyContinuation = {
27
+ tokenize: tokenizeNonLazyContinuation,
28
+ partial: true
29
+ };
30
+ function tokenizeMathFenced(effects, ok, nok) {
31
+ const self = this;
32
+ const tail = self.events[self.events.length - 1];
33
+ const initialSize = tail && tail[1].type === types.linePrefix ? tail[2].sliceSerialize(tail[1], true).length : 0;
34
+ let sizeOpen = 0;
35
+ return start;
36
+ function start(code) {
37
+ if (code !== codes.dollarSign) return nok(code);
38
+ effects.enter("mathFlow");
39
+ effects.enter("mathFlowFence");
40
+ effects.enter("mathFlowFenceSequence");
41
+ return sequenceOpen(code);
42
+ }
43
+ function sequenceOpen(code) {
44
+ if (code === codes.dollarSign) {
45
+ effects.consume(code);
46
+ sizeOpen++;
47
+ return sequenceOpen;
48
+ }
49
+ if (sizeOpen < 2) {
50
+ return nok(code);
51
+ }
52
+ effects.exit("mathFlowFenceSequence");
53
+ return factorySpace(effects, metaBefore, types.whitespace)(code);
54
+ }
55
+ function metaBefore(code) {
56
+ if (code === codes.eof || markdownLineEnding(code)) {
57
+ return metaAfter(code);
58
+ }
59
+ effects.enter("mathFlowFenceMeta");
60
+ effects.enter(types.chunkString, { contentType: constants.contentTypeString });
61
+ return meta(code);
62
+ }
63
+ function meta(code) {
64
+ if (code === codes.eof || markdownLineEnding(code)) {
65
+ effects.exit(types.chunkString);
66
+ effects.exit("mathFlowFenceMeta");
67
+ return metaAfter(code);
68
+ }
69
+ if (code === codes.dollarSign) {
70
+ return nok(code);
71
+ }
72
+ effects.consume(code);
73
+ return meta;
74
+ }
75
+ function metaAfter(code) {
76
+ effects.exit("mathFlowFence");
77
+ if (self.interrupt) {
78
+ return ok(code);
79
+ }
80
+ return effects.attempt(nonLazyContinuation, beforeNonLazyContinuation, after)(code);
81
+ }
82
+ function beforeNonLazyContinuation(code) {
83
+ return effects.attempt(
84
+ { tokenize: tokenizeClosingFence, partial: true },
85
+ after,
86
+ contentStart
87
+ )(code);
88
+ }
89
+ function contentStart(code) {
90
+ return (initialSize ? factorySpace(effects, beforeContentChunk, types.linePrefix, initialSize + 1) : beforeContentChunk)(code);
91
+ }
92
+ function beforeContentChunk(code) {
93
+ if (code === codes.eof) {
94
+ return after(code);
95
+ }
96
+ if (markdownLineEnding(code)) {
97
+ return effects.attempt(nonLazyContinuation, beforeNonLazyContinuation, after)(code);
98
+ }
99
+ effects.enter("mathFlowValue");
100
+ return contentChunk(code);
101
+ }
102
+ function contentChunk(code) {
103
+ if (code === codes.eof || markdownLineEnding(code)) {
104
+ effects.exit("mathFlowValue");
105
+ return beforeContentChunk(code);
106
+ }
107
+ effects.consume(code);
108
+ return contentChunk;
109
+ }
110
+ function after(code) {
111
+ effects.exit("mathFlow");
112
+ return ok(code);
113
+ }
114
+ function tokenizeClosingFence(effects2, ok2, nok2) {
115
+ let size = 0;
116
+ return factorySpace(
117
+ effects2,
118
+ beforeSequenceClose,
119
+ types.linePrefix,
120
+ self.parser.constructs.disable?.null?.includes("codeIndented") ? void 0 : constants.tabSize
121
+ );
122
+ function beforeSequenceClose(code) {
123
+ effects2.enter("mathFlowFence");
124
+ effects2.enter("mathFlowFenceSequence");
125
+ return sequenceClose(code);
126
+ }
127
+ function sequenceClose(code) {
128
+ if (code === codes.dollarSign) {
129
+ size++;
130
+ effects2.consume(code);
131
+ return sequenceClose;
132
+ }
133
+ if (size < sizeOpen) {
134
+ return nok2(code);
135
+ }
136
+ effects2.exit("mathFlowFenceSequence");
137
+ return factorySpace(effects2, afterSequenceClose, types.whitespace)(code);
138
+ }
139
+ function afterSequenceClose(code) {
140
+ if (code === codes.eof || markdownLineEnding(code)) {
141
+ effects2.exit("mathFlowFence");
142
+ return ok2(code);
143
+ }
144
+ return nok2(code);
145
+ }
146
+ }
147
+ }
148
+ function mathFlowTex(_options) {
149
+ return {
150
+ tokenize: tokenizeMathFencedTex,
151
+ concrete: true,
152
+ name: "mathFlowTex"
153
+ };
154
+ }
155
+ function tokenizeMathFencedTex(effects, ok, nok) {
156
+ const self = this;
157
+ return start;
158
+ function start(code) {
159
+ if (code !== codes.backslash) return nok(code);
160
+ effects.enter("mathFlow");
161
+ effects.enter("mathFlowFence");
162
+ effects.enter("mathFlowFenceSequence");
163
+ effects.consume(code);
164
+ return afterBackslash;
165
+ }
166
+ function afterBackslash(code) {
167
+ if (code !== codes.leftSquareBracket) {
168
+ return nok(code);
169
+ }
170
+ effects.consume(code);
171
+ effects.exit("mathFlowFenceSequence");
172
+ effects.exit("mathFlowFence");
173
+ if (self.interrupt) {
174
+ return ok(code);
175
+ }
176
+ return contentStart;
177
+ }
178
+ function contentStart(code) {
179
+ if (code === codes.backslash) {
180
+ return effects.attempt(
181
+ { tokenize: tokenizeClosingFenceTex, partial: true },
182
+ afterClose,
183
+ beginContent
184
+ )(code);
185
+ }
186
+ return beginContent(code);
187
+ }
188
+ function beginContent(code) {
189
+ if (code === codes.eof) {
190
+ return after(code);
191
+ }
192
+ if (markdownLineEnding(code)) {
193
+ return effects.attempt(nonLazyContinuation, afterLineEnding, after)(code);
194
+ }
195
+ effects.enter("mathFlowValue");
196
+ return contentChunk(code);
197
+ }
198
+ function afterLineEnding(code) {
199
+ return contentStart(code);
200
+ }
201
+ function contentChunk(code) {
202
+ if (code === codes.eof) {
203
+ effects.exit("mathFlowValue");
204
+ return after(code);
205
+ }
206
+ if (markdownLineEnding(code)) {
207
+ effects.exit("mathFlowValue");
208
+ return effects.attempt(nonLazyContinuation, afterLineEnding, after)(code);
209
+ }
210
+ if (code === codes.backslash) {
211
+ effects.exit("mathFlowValue");
212
+ return effects.attempt(
213
+ { tokenize: tokenizeClosingFenceTex, partial: true },
214
+ afterClose,
215
+ continueContent
216
+ )(code);
217
+ }
218
+ effects.consume(code);
219
+ return contentChunk;
220
+ }
221
+ function continueContent(code) {
222
+ effects.enter("mathFlowValue");
223
+ return contentChunk(code);
224
+ }
225
+ function afterClose(code) {
226
+ return after(code);
227
+ }
228
+ function after(code) {
229
+ effects.exit("mathFlow");
230
+ return ok(code);
231
+ }
232
+ function tokenizeClosingFenceTex(effects2, ok2, nok2) {
233
+ return beforeSequenceClose;
234
+ function beforeSequenceClose(code) {
235
+ if (code !== codes.backslash) {
236
+ return nok2(code);
237
+ }
238
+ effects2.enter("mathFlowFence");
239
+ effects2.enter("mathFlowFenceSequence");
240
+ effects2.consume(code);
241
+ return afterBackslashClose;
242
+ }
243
+ function afterBackslashClose(code) {
244
+ if (code !== codes.rightSquareBracket) {
245
+ return nok2(code);
246
+ }
247
+ effects2.consume(code);
248
+ effects2.exit("mathFlowFenceSequence");
249
+ effects2.exit("mathFlowFence");
250
+ return ok2(code);
251
+ }
252
+ }
253
+ }
254
+ function tokenizeNonLazyContinuation(effects, ok, nok) {
255
+ const self = this;
256
+ return start;
257
+ function start(code) {
258
+ if (code === null) {
259
+ return ok(code);
260
+ }
261
+ if (!markdownLineEnding(code)) {
262
+ return nok(code);
263
+ }
264
+ effects.enter(types.lineEnding);
265
+ effects.consume(code);
266
+ effects.exit(types.lineEnding);
267
+ return lineStart;
268
+ }
269
+ function lineStart(code) {
270
+ return self.parser.lazy[self.now().line] ? nok(code) : ok(code);
271
+ }
272
+ }
273
+ function mathText(options) {
274
+ return {
275
+ tokenize: tokenizeMathText,
276
+ resolve: resolveMathText,
277
+ previous: previousDollar,
278
+ name: "mathText"
279
+ };
280
+ function tokenizeMathText(effects, ok, nok) {
281
+ const self = this;
282
+ let sizeOpen = 0;
283
+ let size;
284
+ let token;
285
+ return start;
286
+ function start(code) {
287
+ if (code !== codes.dollarSign) return nok(code);
288
+ if (!previousDollar.call(self, self.previous)) return nok(code);
289
+ effects.enter("mathText");
290
+ effects.enter("mathTextSequence");
291
+ return sequenceOpen(code);
292
+ }
293
+ function sequenceOpen(code) {
294
+ if (code === codes.dollarSign) {
295
+ effects.consume(code);
296
+ sizeOpen++;
297
+ return sequenceOpen;
298
+ }
299
+ effects.exit("mathTextSequence");
300
+ return between(code);
301
+ }
302
+ function between(code) {
303
+ if (code === codes.eof) {
304
+ return nok(code);
305
+ }
306
+ if (code === codes.dollarSign) {
307
+ token = effects.enter("mathTextSequence");
308
+ size = 0;
309
+ return sequenceClose(code);
310
+ }
311
+ if (code === codes.space) {
312
+ effects.enter("space");
313
+ effects.consume(code);
314
+ effects.exit("space");
315
+ return between;
316
+ }
317
+ if (markdownLineEnding(code)) {
318
+ effects.enter(types.lineEnding);
319
+ effects.consume(code);
320
+ effects.exit(types.lineEnding);
321
+ return between;
322
+ }
323
+ effects.enter("mathTextData");
324
+ return data(code);
325
+ }
326
+ function data(code) {
327
+ if (code === codes.eof || code === codes.space || code === codes.dollarSign || markdownLineEnding(code)) {
328
+ effects.exit("mathTextData");
329
+ return between(code);
330
+ }
331
+ effects.consume(code);
332
+ return data;
333
+ }
334
+ function sequenceClose(code) {
335
+ if (code === codes.dollarSign) {
336
+ effects.consume(code);
337
+ size++;
338
+ return sequenceClose;
339
+ }
340
+ if (size === sizeOpen) {
341
+ effects.exit("mathTextSequence");
342
+ effects.exit("mathText");
343
+ return ok(code);
344
+ }
345
+ token.type = "mathTextData";
346
+ return data(code);
347
+ }
348
+ }
349
+ }
350
+ function mathTextTex(options) {
351
+ return {
352
+ tokenize: tokenizeMathTextTex,
353
+ resolve: resolveMathText,
354
+ previous: previousBackslash,
355
+ name: "mathTextTex"
356
+ };
357
+ function tokenizeMathTextTex(effects, ok, nok) {
358
+ const self = this;
359
+ return start;
360
+ function start(code) {
361
+ if (code !== codes.backslash) return nok(code);
362
+ if (!previousBackslash.call(self, self.previous)) return nok(code);
363
+ effects.enter("mathText");
364
+ effects.enter("mathTextSequence");
365
+ effects.consume(code);
366
+ return afterBackslash;
367
+ }
368
+ function afterBackslash(code) {
369
+ if (code !== codes.leftParenthesis) {
370
+ return nok(code);
371
+ }
372
+ effects.consume(code);
373
+ effects.exit("mathTextSequence");
374
+ return between;
375
+ }
376
+ function between(code) {
377
+ if (code === codes.eof) {
378
+ return nok(code);
379
+ }
380
+ if (code === codes.backslash) {
381
+ effects.enter("mathTextSequence");
382
+ effects.consume(code);
383
+ return checkClose;
384
+ }
385
+ if (code === codes.space) {
386
+ effects.enter("space");
387
+ effects.consume(code);
388
+ effects.exit("space");
389
+ return between;
390
+ }
391
+ if (markdownLineEnding(code)) {
392
+ effects.enter(types.lineEnding);
393
+ effects.consume(code);
394
+ effects.exit(types.lineEnding);
395
+ return between;
396
+ }
397
+ effects.enter("mathTextData");
398
+ return data(code);
399
+ }
400
+ function checkClose(code) {
401
+ if (code === codes.rightParenthesis) {
402
+ effects.consume(code);
403
+ effects.exit("mathTextSequence");
404
+ effects.exit("mathText");
405
+ return ok;
406
+ }
407
+ effects.exit("mathTextSequence");
408
+ if (code === codes.backslash || code === codes.space || markdownLineEnding(code) || code === codes.eof) {
409
+ return between(code);
410
+ }
411
+ effects.enter("mathTextData");
412
+ return data(code);
413
+ }
414
+ function data(code) {
415
+ if (code === codes.eof) {
416
+ effects.exit("mathTextData");
417
+ return nok(code);
418
+ }
419
+ if (code === codes.backslash) {
420
+ effects.exit("mathTextData");
421
+ return between(code);
422
+ }
423
+ if (code === codes.space || markdownLineEnding(code)) {
424
+ effects.exit("mathTextData");
425
+ return between(code);
426
+ }
427
+ effects.consume(code);
428
+ return data;
429
+ }
430
+ }
431
+ }
432
+ var resolveMathText = (events) => {
433
+ let tailExitIndex = events.length - 4;
434
+ let headEnterIndex = 3;
435
+ let index;
436
+ let enter;
437
+ if ((events[headEnterIndex][1].type === types.lineEnding || events[headEnterIndex][1].type === "space") && (events[tailExitIndex][1].type === types.lineEnding || events[tailExitIndex][1].type === "space")) {
438
+ index = headEnterIndex;
439
+ while (++index < tailExitIndex) {
440
+ if (events[index][1].type === "mathTextData") {
441
+ events[tailExitIndex][1].type = "mathTextPadding";
442
+ events[headEnterIndex][1].type = "mathTextPadding";
443
+ headEnterIndex += 2;
444
+ tailExitIndex -= 2;
445
+ break;
446
+ }
447
+ }
448
+ }
449
+ index = headEnterIndex - 1;
450
+ tailExitIndex++;
451
+ while (++index <= tailExitIndex) {
452
+ if (enter === void 0) {
453
+ if (index !== tailExitIndex && events[index][1].type !== types.lineEnding) {
454
+ enter = index;
455
+ }
456
+ } else if (index === tailExitIndex || events[index][1].type === types.lineEnding) {
457
+ events[enter][1].type = "mathTextData";
458
+ if (index !== enter + 2) {
459
+ events[enter][1].end = events[index - 1][1].end;
460
+ events.splice(enter + 2, index - enter - 2);
461
+ tailExitIndex -= index - enter - 2;
462
+ index = enter + 2;
463
+ }
464
+ enter = void 0;
465
+ }
466
+ }
467
+ return events;
468
+ };
469
+ function previousDollar(code) {
470
+ return code !== codes.dollarSign || this.events[this.events.length - 1][1].type === types.characterEscape;
471
+ }
472
+ function previousBackslash(code) {
473
+ return code !== codes.backslash || this.events[this.events.length - 1][1].type === types.characterEscape;
474
+ }
475
+
476
+ // src/extensions/micromark-extension-math/types.ts
477
+ function resolveMathOptions(options) {
478
+ return {
479
+ singleDollarTextMath: options?.singleDollarTextMath,
480
+ tex: options?.tex ?? false
481
+ };
482
+ }
483
+
484
+ // src/extensions/micromark-extension-math/index.ts
485
+ function math(options) {
486
+ const resolved = resolveMathOptions(options);
487
+ const extension = {
488
+ flow: {
489
+ [codes.dollarSign]: mathFlow()
490
+ },
491
+ text: {
492
+ [codes.dollarSign]: mathText()
493
+ }
494
+ };
495
+ if (resolved.tex) {
496
+ extension.flow[codes.backslash] = mathFlowTex();
497
+ extension.text[codes.backslash] = mathTextTex();
498
+ }
499
+ return extension;
500
+ }
501
+
502
+ // src/extensions/html-extension/index.ts
503
+ var DEFAULT_TAG_BLACKLIST = [
504
+ "script",
505
+ "style",
506
+ "iframe",
507
+ "object",
508
+ "embed",
509
+ "form",
510
+ "input",
511
+ "button",
512
+ "textarea",
513
+ "select",
514
+ "meta",
515
+ "link",
516
+ "base",
517
+ "frame",
518
+ "frameset",
519
+ "applet",
520
+ "noscript",
521
+ "template"
522
+ ];
523
+ var DEFAULT_ATTR_BLACKLIST = [
524
+ // 事件属性通过正则匹配
525
+ "formaction",
526
+ "xlink:href",
527
+ "xmlns",
528
+ "srcdoc"
529
+ ];
530
+ var DEFAULT_PROTOCOL_BLACKLIST = [
531
+ "javascript:",
532
+ "vbscript:",
533
+ "data:"
534
+ // 注意:data:image/ 会被特殊处理允许
535
+ ];
536
+ var URL_ATTRS = ["href", "src", "action", "formaction", "poster", "background"];
537
+ var VOID_ELEMENTS = ["br", "hr", "img", "input", "meta", "link", "area", "base", "col", "embed", "source", "track", "wbr"];
538
+ function detectHtmlContentType(html) {
539
+ const trimmed = html.trim();
540
+ if (!trimmed) return "unknown";
541
+ if (!trimmed.startsWith("<")) return "unknown";
542
+ const closingMatch = trimmed.match(/^<\/([a-zA-Z][a-zA-Z0-9-]*)\s*>$/);
543
+ if (closingMatch) {
544
+ return "closing";
545
+ }
546
+ const singleTagMatch = trimmed.match(/^<([a-zA-Z][a-zA-Z0-9-]*)(\s[^]*?)?(\/?)>$/);
547
+ if (singleTagMatch) {
548
+ const [fullMatch, tagName, attrsString, selfClosingSlash] = singleTagMatch;
549
+ if (attrsString) {
550
+ let inQuote = "";
551
+ let hasUnquotedBracket = false;
552
+ for (let i = 0; i < attrsString.length; i++) {
553
+ const char = attrsString[i];
554
+ if (inQuote) {
555
+ if (char === inQuote) inQuote = "";
556
+ } else {
557
+ if (char === '"' || char === "'") inQuote = char;
558
+ else if (char === "<") {
559
+ hasUnquotedBracket = true;
560
+ break;
561
+ }
562
+ }
563
+ }
564
+ if (hasUnquotedBracket) {
565
+ return "fragment";
566
+ }
567
+ }
568
+ const isSelfClosing = selfClosingSlash === "/" || VOID_ELEMENTS.includes(tagName.toLowerCase());
569
+ return isSelfClosing ? "self-closing" : "opening";
570
+ }
571
+ let bracketCount = 0;
572
+ for (const char of trimmed) {
573
+ if (char === "<") bracketCount++;
574
+ }
575
+ if (bracketCount > 1) {
576
+ return "fragment";
577
+ }
578
+ return "unknown";
579
+ }
580
+ function parseHtmlTag(html) {
581
+ const trimmed = html.trim();
582
+ const contentType = detectHtmlContentType(trimmed);
583
+ if (contentType !== "opening" && contentType !== "closing" && contentType !== "self-closing") {
584
+ return null;
585
+ }
586
+ if (contentType === "closing") {
587
+ const match2 = trimmed.match(/^<\/([a-zA-Z][a-zA-Z0-9-]*)\s*>$/);
588
+ if (!match2) return null;
589
+ return {
590
+ tagName: match2[1].toLowerCase(),
591
+ attrs: {},
592
+ isClosing: true,
593
+ isSelfClosing: false,
594
+ rawHtml: html
595
+ };
596
+ }
597
+ const match = trimmed.match(/^<([a-zA-Z][a-zA-Z0-9-]*)(\s[^]*?)?(\/?)>$/);
598
+ if (!match) return null;
599
+ const [, tagName, attrsString, selfClosingSlash] = match;
600
+ const isSelfClosing = selfClosingSlash === "/" || VOID_ELEMENTS.includes(tagName.toLowerCase());
601
+ const attrs = {};
602
+ if (attrsString) {
603
+ const attrRegex = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
604
+ let attrMatch;
605
+ while ((attrMatch = attrRegex.exec(attrsString)) !== null) {
606
+ const [, name, doubleQuoted, singleQuoted, unquoted] = attrMatch;
607
+ const value = doubleQuoted ?? singleQuoted ?? unquoted ?? "";
608
+ attrs[name.toLowerCase()] = decodeHtmlEntities(value);
609
+ }
610
+ }
611
+ return {
612
+ tagName: tagName.toLowerCase(),
613
+ attrs,
614
+ isClosing: false,
615
+ isSelfClosing,
616
+ rawHtml: html
617
+ };
618
+ }
619
+ function decodeHtmlEntities(text) {
620
+ const entities = {
621
+ "&amp;": "&",
622
+ "&lt;": "<",
623
+ "&gt;": ">",
624
+ "&quot;": '"',
625
+ "&#39;": "'",
626
+ "&apos;": "'",
627
+ "&nbsp;": " "
628
+ };
629
+ return text.replace(/&(?:#(\d+)|#x([a-fA-F0-9]+)|([a-zA-Z]+));/g, (match, dec, hex, name) => {
630
+ if (dec) return String.fromCharCode(parseInt(dec, 10));
631
+ if (hex) return String.fromCharCode(parseInt(hex, 16));
632
+ return entities[`&${name};`] || match;
633
+ });
634
+ }
635
+ function parseTagDirect(tag) {
636
+ const trimmed = tag.trim();
637
+ const closingMatch = trimmed.match(/^<\/([a-zA-Z][a-zA-Z0-9-]*)\s*>$/);
638
+ if (closingMatch) {
639
+ return {
640
+ tagName: closingMatch[1].toLowerCase(),
641
+ attrs: {},
642
+ isClosing: true,
643
+ isSelfClosing: false,
644
+ rawHtml: tag
645
+ };
646
+ }
647
+ const openMatch = trimmed.match(/^<([a-zA-Z][a-zA-Z0-9-]*)([\s\S]*?)(\/?)>$/);
648
+ if (!openMatch) return null;
649
+ const [, tagName, attrsString, selfClosingSlash] = openMatch;
650
+ const isSelfClosing = selfClosingSlash === "/" || VOID_ELEMENTS.includes(tagName.toLowerCase());
651
+ const attrs = {};
652
+ if (attrsString) {
653
+ const attrRegex = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)\s*(?:=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
654
+ let attrMatch;
655
+ while ((attrMatch = attrRegex.exec(attrsString)) !== null) {
656
+ const [, name, doubleQuoted, singleQuoted, unquoted] = attrMatch;
657
+ const value = doubleQuoted ?? singleQuoted ?? unquoted ?? "";
658
+ attrs[name.toLowerCase()] = decodeHtmlEntities(value);
659
+ }
660
+ }
661
+ return {
662
+ tagName: tagName.toLowerCase(),
663
+ attrs,
664
+ isClosing: false,
665
+ isSelfClosing,
666
+ rawHtml: tag
667
+ };
668
+ }
669
+ function parseHtmlFragment(html, options = {}) {
670
+ const result = [];
671
+ const stack = [];
672
+ const tokenRegex = /(<\/?[a-zA-Z][^>]*>)|([^<]+)/g;
673
+ let match;
674
+ while ((match = tokenRegex.exec(html)) !== null) {
675
+ const [, tag, text] = match;
676
+ if (tag) {
677
+ const parsed = parseTagDirect(tag);
678
+ if (!parsed) continue;
679
+ if (isTagBlacklisted(parsed.tagName, options)) {
680
+ continue;
681
+ }
682
+ if (parsed.isClosing) {
683
+ let found = false;
684
+ for (let i = stack.length - 1; i >= 0; i--) {
685
+ if (stack[i].tagName === parsed.tagName) {
686
+ const node = stack.pop();
687
+ if (stack.length > 0) {
688
+ stack[stack.length - 1].children.push(node);
689
+ } else {
690
+ result.push(node);
691
+ }
692
+ found = true;
693
+ break;
694
+ }
695
+ }
696
+ if (!found) continue;
697
+ } else {
698
+ const sanitizedAttrs = sanitizeAttrs(parsed.attrs, options);
699
+ const node = {
700
+ type: "htmlElement",
701
+ tagName: parsed.tagName,
702
+ attrs: sanitizedAttrs,
703
+ children: [],
704
+ data: options.preserveRawHtml !== false ? {
705
+ rawHtml: tag,
706
+ parsed: true
707
+ } : void 0
708
+ };
709
+ if (parsed.isSelfClosing) {
710
+ if (stack.length > 0) {
711
+ stack[stack.length - 1].children.push(node);
712
+ } else {
713
+ result.push(node);
714
+ }
715
+ } else {
716
+ stack.push(node);
717
+ }
718
+ }
719
+ } else if (text && text.trim()) {
720
+ const textNode = {
721
+ type: "text",
722
+ value: text
723
+ };
724
+ if (stack.length > 0) {
725
+ stack[stack.length - 1].children.push(textNode);
726
+ }
727
+ }
728
+ }
729
+ while (stack.length > 0) {
730
+ const node = stack.pop();
731
+ if (stack.length > 0) {
732
+ stack[stack.length - 1].children.push(node);
733
+ } else {
734
+ result.push(node);
735
+ }
736
+ }
737
+ return result;
738
+ }
739
+ function isTagBlacklisted(tagName, options) {
740
+ const blacklist = options.tagBlacklist ?? DEFAULT_TAG_BLACKLIST;
741
+ return blacklist.includes(tagName.toLowerCase());
742
+ }
743
+ function isAttrBlacklisted(attrName, options) {
744
+ const name = attrName.toLowerCase();
745
+ const blacklist = options.attrBlacklist ?? DEFAULT_ATTR_BLACKLIST;
746
+ if (name.startsWith("on")) return true;
747
+ return blacklist.includes(name);
748
+ }
749
+ function isProtocolDangerous(url, options) {
750
+ const protocolBlacklist = options.protocolBlacklist ?? DEFAULT_PROTOCOL_BLACKLIST;
751
+ const normalizedUrl = url.trim().toLowerCase();
752
+ for (const protocol of protocolBlacklist) {
753
+ if (normalizedUrl.startsWith(protocol)) {
754
+ if (protocol === "data:" && normalizedUrl.startsWith("data:image/")) {
755
+ return false;
756
+ }
757
+ return true;
758
+ }
759
+ }
760
+ return false;
761
+ }
762
+ function sanitizeAttrs(attrs, options) {
763
+ const result = {};
764
+ for (const [name, value] of Object.entries(attrs)) {
765
+ if (isAttrBlacklisted(name, options)) continue;
766
+ if (URL_ATTRS.includes(name.toLowerCase())) {
767
+ if (isProtocolDangerous(value, options)) continue;
768
+ }
769
+ result[name] = value;
770
+ }
771
+ return result;
772
+ }
773
+ function isHtmlNode(node) {
774
+ return node.type === "html";
775
+ }
776
+ function hasChildren(node) {
777
+ return "children" in node && Array.isArray(node.children);
778
+ }
779
+ function mergeFragmentedHtmlNodes(nodes) {
780
+ const result = [];
781
+ let i = 0;
782
+ while (i < nodes.length) {
783
+ const node = nodes[i];
784
+ if (!isHtmlNode(node)) {
785
+ result.push(node);
786
+ i++;
787
+ continue;
788
+ }
789
+ const unclosedTags = findUnclosedTags(node.value);
790
+ if (unclosedTags.length === 0) {
791
+ result.push(node);
792
+ i++;
793
+ continue;
794
+ }
795
+ const mergedParts = [node.value];
796
+ let j = i + 1;
797
+ let currentUnclosed = [...unclosedTags];
798
+ while (j < nodes.length && currentUnclosed.length > 0) {
799
+ const nextNode = nodes[j];
800
+ if (isHtmlNode(nextNode)) {
801
+ const closingInfo = checkClosingTags(nextNode.value, currentUnclosed);
802
+ if (closingInfo.hasRelevantClosing) {
803
+ mergedParts.push(nextNode.value);
804
+ currentUnclosed = closingInfo.remainingUnclosed;
805
+ if (currentUnclosed.length === 0) {
806
+ j++;
807
+ break;
808
+ }
809
+ } else {
810
+ mergedParts.push(nextNode.value);
811
+ }
812
+ } else {
813
+ break;
814
+ }
815
+ j++;
816
+ }
817
+ if (mergedParts.length > 1) {
818
+ const mergedValue = mergedParts.join("\n");
819
+ const mergedNode = {
820
+ type: "html",
821
+ value: mergedValue
822
+ };
823
+ result.push(mergedNode);
824
+ i = j;
825
+ } else {
826
+ result.push(node);
827
+ i++;
828
+ }
829
+ }
830
+ return result;
831
+ }
832
+ function findUnclosedTags(html) {
833
+ const tagStack = [];
834
+ const tagRegex = /<\/?([a-zA-Z][a-zA-Z0-9-]*)[^>]*\/?>/g;
835
+ let match;
836
+ while ((match = tagRegex.exec(html)) !== null) {
837
+ const fullTag = match[0];
838
+ const tagName = match[1].toLowerCase();
839
+ if (VOID_ELEMENTS.includes(tagName) || fullTag.endsWith("/>")) {
840
+ continue;
841
+ }
842
+ if (fullTag.startsWith("</")) {
843
+ const lastIndex = tagStack.lastIndexOf(tagName);
844
+ if (lastIndex !== -1) {
845
+ tagStack.splice(lastIndex, 1);
846
+ }
847
+ } else {
848
+ tagStack.push(tagName);
849
+ }
850
+ }
851
+ return tagStack;
852
+ }
853
+ function checkClosingTags(html, unclosedTags) {
854
+ const remaining = [...unclosedTags];
855
+ let hasRelevant = false;
856
+ const closeTagRegex = /<\/([a-zA-Z][a-zA-Z0-9-]*)\s*>/g;
857
+ let match;
858
+ while ((match = closeTagRegex.exec(html)) !== null) {
859
+ const tagName = match[1].toLowerCase();
860
+ const index = remaining.lastIndexOf(tagName);
861
+ if (index !== -1) {
862
+ remaining.splice(index, 1);
863
+ hasRelevant = true;
864
+ }
865
+ }
866
+ return {
867
+ hasRelevantClosing: hasRelevant,
868
+ remainingUnclosed: remaining
869
+ };
870
+ }
871
+ function processHtmlNodesInArray(nodes, options) {
872
+ const mergedNodes = mergeFragmentedHtmlNodes(nodes);
873
+ const result = [];
874
+ let i = 0;
875
+ while (i < mergedNodes.length) {
876
+ const node = mergedNodes[i];
877
+ if (isHtmlNode(node)) {
878
+ const contentType = detectHtmlContentType(node.value);
879
+ if (contentType === "fragment") {
880
+ const fragmentNodes = parseHtmlFragment(node.value, options);
881
+ if (fragmentNodes.length > 0) {
882
+ result.push(...fragmentNodes);
883
+ } else {
884
+ result.push(node);
885
+ }
886
+ i++;
887
+ } else if (contentType === "self-closing") {
888
+ const parsed = parseHtmlTag(node.value);
889
+ if (parsed && !isTagBlacklisted(parsed.tagName, options)) {
890
+ const elementNode = {
891
+ type: "htmlElement",
892
+ tagName: parsed.tagName,
893
+ attrs: sanitizeAttrs(parsed.attrs, options),
894
+ children: [],
895
+ data: options.preserveRawHtml !== false ? {
896
+ rawHtml: node.value,
897
+ parsed: true,
898
+ originalType: "html"
899
+ } : void 0
900
+ };
901
+ result.push(elementNode);
902
+ }
903
+ i++;
904
+ } else if (contentType === "closing") {
905
+ i++;
906
+ } else if (contentType === "opening") {
907
+ const parsed = parseHtmlTag(node.value);
908
+ if (!parsed || isTagBlacklisted(parsed.tagName, options)) {
909
+ i++;
910
+ continue;
911
+ }
912
+ const tagName = parsed.tagName;
913
+ const contentNodes = [];
914
+ let depth = 1;
915
+ let j = i + 1;
916
+ let foundClosing = false;
917
+ while (j < mergedNodes.length && depth > 0) {
918
+ const nextNode = mergedNodes[j];
919
+ if (isHtmlNode(nextNode)) {
920
+ const nextType = detectHtmlContentType(nextNode.value);
921
+ if (nextType === "closing") {
922
+ const nextParsed = parseHtmlTag(nextNode.value);
923
+ if (nextParsed && nextParsed.tagName === tagName) {
924
+ depth--;
925
+ if (depth === 0) {
926
+ foundClosing = true;
927
+ break;
928
+ }
929
+ }
930
+ } else if (nextType === "opening") {
931
+ const nextParsed = parseHtmlTag(nextNode.value);
932
+ if (nextParsed && nextParsed.tagName === tagName) {
933
+ depth++;
934
+ }
935
+ }
936
+ }
937
+ contentNodes.push(nextNode);
938
+ j++;
939
+ }
940
+ const elementNode = {
941
+ type: "htmlElement",
942
+ tagName: parsed.tagName,
943
+ attrs: sanitizeAttrs(parsed.attrs, options),
944
+ children: processHtmlNodesInArray(contentNodes, options),
945
+ data: options.preserveRawHtml !== false ? {
946
+ rawHtml: node.value,
947
+ parsed: true,
948
+ originalType: "html"
949
+ } : void 0
950
+ };
951
+ result.push(elementNode);
952
+ i = foundClosing ? j + 1 : j;
953
+ } else {
954
+ result.push(node);
955
+ i++;
956
+ }
957
+ } else {
958
+ if (hasChildren(node)) {
959
+ const processed = processHtmlNodesInArray(
960
+ node.children,
961
+ options
962
+ );
963
+ result.push({
964
+ ...node,
965
+ children: processed
966
+ });
967
+ } else {
968
+ result.push(node);
969
+ }
970
+ i++;
971
+ }
972
+ }
973
+ return result;
974
+ }
975
+ function transformHtmlNodes(ast, options = {}) {
976
+ return {
977
+ ...ast,
978
+ children: processHtmlNodesInArray(ast.children, options)
979
+ };
980
+ }
981
+ function micromarkReferenceExtension() {
982
+ return {
983
+ // 在 text 中使用 codes.rightSquareBracket 键覆盖 labelEnd
984
+ text: {
985
+ [codes.rightSquareBracket]: {
986
+ name: "labelEnd",
987
+ resolveAll: resolveAllLabelEnd,
988
+ resolveTo: resolveToLabelEnd,
989
+ tokenize: tokenizeLabelEnd,
990
+ // 添加 add: 'before' 确保先被尝试
991
+ add: "before"
992
+ }
993
+ }
994
+ };
995
+ }
996
+ function resolveAllLabelEnd(events) {
997
+ let index = -1;
998
+ const newEvents = [];
999
+ while (++index < events.length) {
1000
+ const token = events[index][1];
1001
+ newEvents.push(events[index]);
1002
+ if (token.type === types.labelImage || token.type === types.labelLink || token.type === types.labelEnd) {
1003
+ const offset = token.type === types.labelImage ? 4 : 2;
1004
+ token.type = types.data;
1005
+ index += offset;
1006
+ }
1007
+ }
1008
+ if (events.length !== newEvents.length) {
1009
+ events.length = 0;
1010
+ events.push(...newEvents);
1011
+ }
1012
+ return events;
1013
+ }
1014
+ function resolveToLabelEnd(events, context) {
1015
+ let index = events.length;
1016
+ let offset = 0;
1017
+ let token;
1018
+ let open;
1019
+ let close;
1020
+ let media;
1021
+ while (index--) {
1022
+ token = events[index][1];
1023
+ if (open !== void 0) {
1024
+ if (token.type === types.link || token.type === types.labelLink && token._inactive) {
1025
+ break;
1026
+ }
1027
+ if (events[index][0] === "enter" && token.type === types.labelLink) {
1028
+ token._inactive = true;
1029
+ }
1030
+ } else if (close !== void 0) {
1031
+ if (events[index][0] === "enter" && (token.type === types.labelImage || token.type === types.labelLink) && !token._balanced) {
1032
+ open = index;
1033
+ if (token.type !== types.labelLink) {
1034
+ offset = 2;
1035
+ break;
1036
+ }
1037
+ }
1038
+ } else if (token.type === types.labelEnd) {
1039
+ close = index;
1040
+ }
1041
+ }
1042
+ if (open === void 0 || close === void 0) {
1043
+ return events;
1044
+ }
1045
+ const group = {
1046
+ type: events[open][1].type === types.labelLink ? types.link : types.image,
1047
+ start: { ...events[open][1].start },
1048
+ end: { ...events[events.length - 1][1].end }
1049
+ };
1050
+ const label = {
1051
+ type: types.label,
1052
+ start: { ...events[open][1].start },
1053
+ end: { ...events[close][1].end }
1054
+ };
1055
+ const text = {
1056
+ type: types.labelText,
1057
+ start: { ...events[open + offset + 2][1].end },
1058
+ end: { ...events[close - 2][1].start }
1059
+ };
1060
+ media = [
1061
+ ["enter", group, context],
1062
+ ["enter", label, context]
1063
+ ];
1064
+ media.push(...events.slice(open + 1, open + offset + 3));
1065
+ media.push(["enter", text, context]);
1066
+ media.push(...events.slice(open + offset + 4, close - 3));
1067
+ media.push(
1068
+ ["exit", text, context],
1069
+ events[close - 2],
1070
+ events[close - 1],
1071
+ ["exit", label, context]
1072
+ );
1073
+ media.push(...events.slice(close + 1));
1074
+ media.push(["exit", group, context]);
1075
+ events.splice(open, events.length - open, ...media);
1076
+ return events;
1077
+ }
1078
+ function tokenizeLabelEnd(effects, ok, nok) {
1079
+ const self = this;
1080
+ let index = self.events.length;
1081
+ let labelStart;
1082
+ while (index--) {
1083
+ if ((self.events[index][1].type === types.labelImage || self.events[index][1].type === types.labelLink) && !self.events[index][1]._balanced) {
1084
+ labelStart = self.events[index][1];
1085
+ break;
1086
+ }
1087
+ }
1088
+ return start;
1089
+ function start(code) {
1090
+ if (!labelStart) {
1091
+ return nok(code);
1092
+ }
1093
+ if (labelStart._inactive) {
1094
+ return labelEndNok(code);
1095
+ }
1096
+ if (labelStart.type === types.labelLink) {
1097
+ const labelText = self.sliceSerialize({ start: labelStart.end, end: self.now() });
1098
+ if (labelText.startsWith("^")) {
1099
+ return nok(code);
1100
+ }
1101
+ }
1102
+ effects.enter(types.labelEnd);
1103
+ effects.enter(types.labelMarker);
1104
+ effects.consume(code);
1105
+ effects.exit(types.labelMarker);
1106
+ effects.exit(types.labelEnd);
1107
+ return after;
1108
+ }
1109
+ function after(code) {
1110
+ if (code === codes.leftParenthesis) {
1111
+ return effects.attempt(
1112
+ {
1113
+ tokenize: tokenizeResource,
1114
+ partial: false
1115
+ },
1116
+ labelEndOk,
1117
+ labelEndNok
1118
+ // 修复:resource 解析失败时返回 nok
1119
+ )(code);
1120
+ }
1121
+ if (code === codes.leftSquareBracket) {
1122
+ return effects.attempt(
1123
+ {
1124
+ tokenize: tokenizeReferenceFull,
1125
+ partial: false
1126
+ },
1127
+ labelEndOk,
1128
+ referenceNotFull
1129
+ // 修改:即使不是 full reference,也尝试 collapsed
1130
+ )(code);
1131
+ }
1132
+ return labelEndOk(code);
1133
+ }
1134
+ function referenceNotFull(code) {
1135
+ return effects.attempt(
1136
+ {
1137
+ tokenize: tokenizeReferenceCollapsed,
1138
+ partial: false
1139
+ },
1140
+ labelEndOk,
1141
+ labelEndOk
1142
+ // 修改:即使失败也返回 ok
1143
+ )(code);
1144
+ }
1145
+ function labelEndOk(code) {
1146
+ return ok(code);
1147
+ }
1148
+ function labelEndNok(code) {
1149
+ labelStart._balanced = true;
1150
+ return nok(code);
1151
+ }
1152
+ }
1153
+ function tokenizeResource(effects, ok, nok) {
1154
+ return resourceStart;
1155
+ function resourceStart(code) {
1156
+ if (code !== codes.leftParenthesis) {
1157
+ return nok(code);
1158
+ }
1159
+ effects.enter(types.resource);
1160
+ effects.enter(types.resourceMarker);
1161
+ effects.consume(code);
1162
+ effects.exit(types.resourceMarker);
1163
+ return resourceBefore;
1164
+ }
1165
+ function resourceBefore(code) {
1166
+ return markdownLineEndingOrSpace(code) ? factoryWhitespace(effects, resourceOpen)(code) : resourceOpen(code);
1167
+ }
1168
+ function resourceOpen(code) {
1169
+ if (code === codes.rightParenthesis) {
1170
+ return resourceEnd(code);
1171
+ }
1172
+ return factoryDestination(
1173
+ effects,
1174
+ resourceDestinationAfter,
1175
+ resourceDestinationMissing,
1176
+ types.resourceDestination,
1177
+ types.resourceDestinationLiteral,
1178
+ types.resourceDestinationLiteralMarker,
1179
+ types.resourceDestinationRaw,
1180
+ types.resourceDestinationString,
1181
+ constants.linkResourceDestinationBalanceMax
1182
+ )(code);
1183
+ }
1184
+ function resourceDestinationAfter(code) {
1185
+ return markdownLineEndingOrSpace(code) ? factoryWhitespace(effects, resourceBetween)(code) : resourceEnd(code);
1186
+ }
1187
+ function resourceDestinationMissing(code) {
1188
+ return nok(code);
1189
+ }
1190
+ function resourceBetween(code) {
1191
+ if (code === codes.quotationMark || code === codes.apostrophe || code === codes.leftParenthesis) {
1192
+ return factoryTitle(
1193
+ effects,
1194
+ resourceTitleAfter,
1195
+ nok,
1196
+ types.resourceTitle,
1197
+ types.resourceTitleMarker,
1198
+ types.resourceTitleString
1199
+ )(code);
1200
+ }
1201
+ return resourceEnd(code);
1202
+ }
1203
+ function resourceTitleAfter(code) {
1204
+ return markdownLineEndingOrSpace(code) ? factoryWhitespace(effects, resourceEnd)(code) : resourceEnd(code);
1205
+ }
1206
+ function resourceEnd(code) {
1207
+ if (code === codes.rightParenthesis) {
1208
+ effects.enter(types.resourceMarker);
1209
+ effects.consume(code);
1210
+ effects.exit(types.resourceMarker);
1211
+ effects.exit(types.resource);
1212
+ return ok;
1213
+ }
1214
+ return nok(code);
1215
+ }
1216
+ }
1217
+ function tokenizeReferenceFull(effects, ok, nok) {
1218
+ const self = this;
1219
+ return referenceFull;
1220
+ function referenceFull(code) {
1221
+ if (code !== codes.leftSquareBracket) {
1222
+ return nok(code);
1223
+ }
1224
+ return factoryLabel.call(
1225
+ self,
1226
+ effects,
1227
+ referenceFullAfter,
1228
+ referenceFullMissing,
1229
+ types.reference,
1230
+ types.referenceMarker,
1231
+ types.referenceString
1232
+ )(code);
1233
+ }
1234
+ function referenceFullAfter(code) {
1235
+ return ok(code);
1236
+ }
1237
+ function referenceFullMissing(code) {
1238
+ return nok(code);
1239
+ }
1240
+ }
1241
+ function tokenizeReferenceCollapsed(effects, ok, nok) {
1242
+ return referenceCollapsedStart;
1243
+ function referenceCollapsedStart(code) {
1244
+ if (code !== codes.leftSquareBracket) {
1245
+ return nok(code);
1246
+ }
1247
+ effects.enter(types.reference);
1248
+ effects.enter(types.referenceMarker);
1249
+ effects.consume(code);
1250
+ effects.exit(types.referenceMarker);
1251
+ return referenceCollapsedOpen;
1252
+ }
1253
+ function referenceCollapsedOpen(code) {
1254
+ if (code === codes.rightSquareBracket) {
1255
+ effects.enter(types.referenceMarker);
1256
+ effects.consume(code);
1257
+ effects.exit(types.referenceMarker);
1258
+ effects.exit(types.reference);
1259
+ return ok;
1260
+ }
1261
+ return nok(code);
1262
+ }
1263
+ }
1264
+ function gfmFootnoteIncremental() {
1265
+ const original = gfmFootnote();
1266
+ return {
1267
+ ...original,
1268
+ text: {
1269
+ ...original.text,
1270
+ // 覆盖 text[91] (`[` 的处理) - 这是脚注引用解析的起点
1271
+ [codes.leftSquareBracket]: {
1272
+ ...original.text[codes.leftSquareBracket],
1273
+ tokenize: tokenizeGfmFootnoteCallIncremental
1274
+ },
1275
+ // 覆盖 text[93] (`]` 的处理) - 用于处理 ![^1] 这样的情况
1276
+ [codes.rightSquareBracket]: {
1277
+ ...original.text[codes.rightSquareBracket],
1278
+ tokenize: tokenizePotentialGfmFootnoteCallIncremental
1279
+ }
1280
+ }
1281
+ };
1282
+ }
1283
+ function tokenizeGfmFootnoteCallIncremental(effects, ok, nok) {
1284
+ let size = 0;
1285
+ let data = false;
1286
+ return start;
1287
+ function start(code) {
1288
+ if (code !== codes.leftSquareBracket) {
1289
+ return nok(code);
1290
+ }
1291
+ effects.enter("gfmFootnoteCall");
1292
+ effects.enter("gfmFootnoteCallLabelMarker");
1293
+ effects.consume(code);
1294
+ effects.exit("gfmFootnoteCallLabelMarker");
1295
+ return callStart;
1296
+ }
1297
+ function callStart(code) {
1298
+ if (code !== codes.caret) {
1299
+ return nok(code);
1300
+ }
1301
+ effects.enter("gfmFootnoteCallMarker");
1302
+ effects.consume(code);
1303
+ effects.exit("gfmFootnoteCallMarker");
1304
+ effects.enter("gfmFootnoteCallString");
1305
+ const token = effects.enter("chunkString");
1306
+ token.contentType = "string";
1307
+ return callData;
1308
+ }
1309
+ function callData(code) {
1310
+ if (
1311
+ // 太长
1312
+ size > constants.linkReferenceSizeMax || // 右括号但没有数据
1313
+ code === codes.rightSquareBracket && !data || // EOF、换行、空格、制表符、左括号不支持
1314
+ code === codes.eof || code === codes.leftSquareBracket || markdownLineEndingOrSpace(code)
1315
+ ) {
1316
+ return nok(code);
1317
+ }
1318
+ if (code === codes.rightSquareBracket) {
1319
+ effects.exit("chunkString");
1320
+ effects.exit("gfmFootnoteCallString");
1321
+ effects.enter("gfmFootnoteCallLabelMarker");
1322
+ effects.consume(code);
1323
+ effects.exit("gfmFootnoteCallLabelMarker");
1324
+ effects.exit("gfmFootnoteCall");
1325
+ return ok;
1326
+ }
1327
+ if (!markdownLineEndingOrSpace(code)) {
1328
+ data = true;
1329
+ }
1330
+ size++;
1331
+ effects.consume(code);
1332
+ return code === codes.backslash ? callEscape : callData;
1333
+ }
1334
+ function callEscape(code) {
1335
+ if (code === codes.leftSquareBracket || code === codes.backslash || code === codes.rightSquareBracket) {
1336
+ effects.consume(code);
1337
+ size++;
1338
+ return callData;
1339
+ }
1340
+ return callData(code);
1341
+ }
1342
+ }
1343
+ function tokenizePotentialGfmFootnoteCallIncremental(effects, ok, nok) {
1344
+ const self = this;
1345
+ let index = self.events.length;
1346
+ let labelStart;
1347
+ while (index--) {
1348
+ const token = self.events[index][1];
1349
+ if (token.type === "labelImage") {
1350
+ labelStart = token;
1351
+ break;
1352
+ }
1353
+ if (token.type === "gfmFootnoteCall" || token.type === "labelLink" || token.type === "label" || token.type === "image" || token.type === "link") {
1354
+ break;
1355
+ }
1356
+ }
1357
+ return start;
1358
+ function start(code) {
1359
+ if (code !== codes.rightSquareBracket) {
1360
+ return nok(code);
1361
+ }
1362
+ if (!labelStart || !labelStart._balanced) {
1363
+ return nok(code);
1364
+ }
1365
+ const id = normalizeIdentifier(
1366
+ self.sliceSerialize({
1367
+ start: labelStart.end,
1368
+ end: self.now()
1369
+ })
1370
+ );
1371
+ if (id.codePointAt(0) !== codes.caret) {
1372
+ return nok(code);
1373
+ }
1374
+ effects.enter("gfmFootnoteCallLabelMarker");
1375
+ effects.consume(code);
1376
+ effects.exit("gfmFootnoteCallLabelMarker");
1377
+ return ok(code);
1378
+ }
1379
+ }
1380
+
1381
+ // src/parser/ast/types.ts
1382
+ function extractMicromarkExtensions(plugins) {
1383
+ const extensions = [];
1384
+ const mdastExtensions = [];
1385
+ for (const plugin of plugins) {
1386
+ if ((plugin.type === "micromark" || plugin.type === "both") && plugin.micromark) {
1387
+ extensions.push(...plugin.micromark.extensions);
1388
+ mdastExtensions.push(...plugin.micromark.mdastExtensions);
1389
+ }
1390
+ }
1391
+ return { extensions, mdastExtensions };
1392
+ }
1393
+
1394
+ // src/parser/ast/MicromarkAstBuilder.ts
1395
+ var INLINE_CONTAINER_TYPES = [
1396
+ "paragraph",
1397
+ "heading",
1398
+ "tableCell",
1399
+ "delete",
1400
+ "emphasis",
1401
+ "strong",
1402
+ "link",
1403
+ "linkReference"
1404
+ ];
1405
+ function isInlineContainer(node) {
1406
+ return INLINE_CONTAINER_TYPES.includes(node.type);
1407
+ }
1408
+ var MicromarkAstBuilder = class {
1409
+ options;
1410
+ containerConfig;
1411
+ htmlTreeConfig;
1412
+ /** 缓存的扩展实例,避免每次 parse 都重新创建 */
1413
+ cachedExtensions = [];
1414
+ cachedMdastExtensions = [];
1415
+ constructor(options = {}) {
1416
+ this.options = options;
1417
+ this.containerConfig = this.computeContainerConfig(options);
1418
+ this.htmlTreeConfig = this.computeHtmlTreeConfig(options);
1419
+ this.initExtensions();
1420
+ }
1421
+ /**
1422
+ * 初始化并缓存扩展实例
1423
+ */
1424
+ initExtensions() {
1425
+ if (this.options.gfm) {
1426
+ this.cachedExtensions.push(gfm());
1427
+ this.cachedMdastExtensions.push(...gfmFromMarkdown(), gfmFootnoteFromMarkdown());
1428
+ }
1429
+ if (this.options.math) {
1430
+ const mathOptions = typeof this.options.math === "object" ? this.options.math : {};
1431
+ this.cachedExtensions.push(math({
1432
+ singleDollarTextMath: true,
1433
+ tex: mathOptions.tex ?? false
1434
+ }));
1435
+ this.cachedMdastExtensions.push(mathFromMarkdown());
1436
+ }
1437
+ if (this.containerConfig !== void 0) {
1438
+ this.cachedExtensions.push(directive());
1439
+ this.cachedMdastExtensions.push(directiveFromMarkdown());
1440
+ }
1441
+ if (this.options.plugins) {
1442
+ const { extensions, mdastExtensions } = extractMicromarkExtensions(this.options.plugins);
1443
+ this.cachedExtensions.push(...extensions);
1444
+ this.cachedMdastExtensions.push(...mdastExtensions);
1445
+ }
1446
+ if (this.options.extensions) {
1447
+ this.cachedExtensions.push(...this.options.extensions);
1448
+ }
1449
+ if (this.options.mdastExtensions) {
1450
+ this.cachedMdastExtensions.push(...this.options.mdastExtensions);
1451
+ }
1452
+ if (this.options.gfm) {
1453
+ this.cachedExtensions.push(gfmFootnoteIncremental());
1454
+ }
1455
+ this.cachedExtensions.push(micromarkReferenceExtension());
1456
+ }
1457
+ /**
1458
+ * 计算容器配置
1459
+ */
1460
+ computeContainerConfig(options) {
1461
+ const containers = options.containers;
1462
+ if (!containers) return void 0;
1463
+ return containers === true ? {} : containers;
1464
+ }
1465
+ /**
1466
+ * 计算 HTML 树配置
1467
+ */
1468
+ computeHtmlTreeConfig(options) {
1469
+ const htmlTree = options.htmlTree;
1470
+ if (!htmlTree) return void 0;
1471
+ return htmlTree === true ? {} : htmlTree;
1472
+ }
1473
+ /**
1474
+ * 解析文本为 AST
1475
+ *
1476
+ * @param text Markdown 文本
1477
+ * @returns AST
1478
+ */
1479
+ parse(text) {
1480
+ const ast = fromMarkdown(text, {
1481
+ extensions: this.cachedExtensions,
1482
+ mdastExtensions: this.cachedMdastExtensions
1483
+ });
1484
+ if (this.htmlTreeConfig) {
1485
+ return transformHtmlNodes(ast, this.htmlTreeConfig);
1486
+ } else {
1487
+ return this.convertHtmlToText(ast);
1488
+ }
1489
+ }
1490
+ /**
1491
+ * 将 HTML 节点转换为纯文本(当未启用 HTML 树转换时)
1492
+ *
1493
+ * @param ast AST
1494
+ * @returns 转换后的 AST
1495
+ */
1496
+ convertHtmlToText(ast) {
1497
+ return {
1498
+ ...ast,
1499
+ children: this.processBlockChildren(ast.children)
1500
+ };
1501
+ }
1502
+ /**
1503
+ * 处理块级节点
1504
+ */
1505
+ processBlockChildren(children) {
1506
+ return children.map((node) => {
1507
+ if (node.type === "html") {
1508
+ return this.convertBlockHtmlToParagraph(node);
1509
+ }
1510
+ if ("children" in node && Array.isArray(node.children)) {
1511
+ const parent = node;
1512
+ const children2 = isInlineContainer(node) ? this.processInlineChildren(parent.children) : this.processBlockChildren(parent.children);
1513
+ return {
1514
+ ...parent,
1515
+ children: children2
1516
+ };
1517
+ }
1518
+ return node;
1519
+ });
1520
+ }
1521
+ /**
1522
+ * 处理内联节点
1523
+ */
1524
+ processInlineChildren(children) {
1525
+ return children.map((node) => {
1526
+ const n = node;
1527
+ if (n.type === "html") {
1528
+ return this.convertInlineHtmlToText(n);
1529
+ }
1530
+ if ("children" in n && Array.isArray(n.children)) {
1531
+ const parent = n;
1532
+ return {
1533
+ ...parent,
1534
+ children: this.processInlineChildren(parent.children)
1535
+ };
1536
+ }
1537
+ return n;
1538
+ });
1539
+ }
1540
+ /**
1541
+ * 将块级 HTML 节点转换为段落
1542
+ */
1543
+ convertBlockHtmlToParagraph(htmlNode) {
1544
+ const textNode = {
1545
+ type: "text",
1546
+ value: htmlNode.value
1547
+ };
1548
+ const paragraphNode = {
1549
+ type: "paragraph",
1550
+ children: [textNode],
1551
+ position: htmlNode.position
1552
+ };
1553
+ return paragraphNode;
1554
+ }
1555
+ /**
1556
+ * 将内联 HTML 节点转换为纯文本节点
1557
+ */
1558
+ convertInlineHtmlToText(htmlNode) {
1559
+ return {
1560
+ type: "text",
1561
+ value: htmlNode.value,
1562
+ position: htmlNode.position
1563
+ };
1564
+ }
1565
+ /**
1566
+ * 将 AST 节点转换为 ParsedBlock
1567
+ *
1568
+ * @param nodes AST 节点列表
1569
+ * @param startOffset 起始偏移量
1570
+ * @param rawText 原始文本
1571
+ * @param status 块状态
1572
+ * @param generateBlockId 生成块 ID 的函数
1573
+ * @returns ParsedBlock 列表
1574
+ */
1575
+ nodesToBlocks(nodes, startOffset, rawText, status, generateBlockId) {
1576
+ const blocks = [];
1577
+ for (const node of nodes) {
1578
+ const relativeStart = node.position?.start?.offset ?? 0;
1579
+ const relativeEnd = node.position?.end?.offset ?? 1;
1580
+ const nodeText = rawText.substring(relativeStart, relativeEnd);
1581
+ const absoluteStart = startOffset + relativeStart;
1582
+ const absoluteEnd = startOffset + relativeEnd;
1583
+ blocks.push({
1584
+ id: generateBlockId(),
1585
+ status,
1586
+ node,
1587
+ startOffset: absoluteStart,
1588
+ endOffset: absoluteEnd,
1589
+ rawText: nodeText
1590
+ });
1591
+ }
1592
+ return blocks;
1593
+ }
1594
+ /**
1595
+ * 更新配置选项
1596
+ *
1597
+ * 注意:由于 micromark 的扩展是在 constructor 中缓存的,
1598
+ * 更新配置需要重新初始化扩展。
1599
+ *
1600
+ * @param options 部分配置选项
1601
+ */
1602
+ updateOptions(options) {
1603
+ Object.assign(this.options, options);
1604
+ if ("containers" in options) {
1605
+ this.containerConfig = this.computeContainerConfig(this.options);
1606
+ }
1607
+ if ("htmlTree" in options) {
1608
+ this.htmlTreeConfig = this.computeHtmlTreeConfig(this.options);
1609
+ }
1610
+ this.cachedExtensions.length = 0;
1611
+ this.cachedMdastExtensions.length = 0;
1612
+ this.initExtensions();
1613
+ }
1614
+ };
1615
+
1616
+ // src/engines/micromark/index.ts
1617
+ function createMicromarkBuilder(options = {}) {
1618
+ return new MicromarkAstBuilder(options);
1619
+ }
1620
+ /**
1621
+ * @file Micromark 扩展:支持增量解析的 Reference 语法
1622
+ *
1623
+ * @description
1624
+ * 在增量解析场景中,引用式图片/链接(如 `![Alt][id]`)可能在定义(`[id]: url`)之前出现。
1625
+ * 标准 micromark 会检查 parser.defined,如果 id 未定义就解析为文本。
1626
+ *
1627
+ * 本扩展通过覆盖 labelEnd 构造,移除 parser.defined 检查,
1628
+ * 使得 reference 语法总是被解析为 reference token,
1629
+ * 由渲染层根据实际的 definitionMap 决定如何渲染。
1630
+ *
1631
+ * @module micromark-reference-extension
1632
+ *
1633
+ * @features
1634
+ * - ✅ 支持所有 resource 语法(带 title 的图片/链接)
1635
+ * - ✅ 支持所有 reference 语法(full, collapsed, shortcut)
1636
+ * - ✅ 延迟验证:解析时不检查定义是否存在
1637
+ * - ✅ 使用官方 factory 函数,保证与 CommonMark 标准一致
1638
+ *
1639
+ * @dependencies
1640
+ * - micromark-factory-destination: 解析 URL(支持尖括号、括号平衡)
1641
+ * - micromark-factory-title: 解析 title(支持三种引号,支持多行)
1642
+ * - micromark-factory-label: 解析 label(支持转义、长度限制)
1643
+ * - micromark-factory-whitespace: 解析空白符(正确生成 lineEnding/linePrefix token)
1644
+ * - micromark-util-character: 字符判断工具
1645
+ * - micromark-util-symbol: 常量(codes, types, constants)
1646
+ * - micromark-util-types: TypeScript 类型定义
1647
+ *
1648
+ * @see {@link https://github.com/micromark/micromark} - micromark 官方文档
1649
+ * @see {@link https://spec.commonmark.org/0.30/#images} - CommonMark 图片规范
1650
+ * @see {@link https://spec.commonmark.org/0.30/#links} - CommonMark 链接规范
1651
+ *
1652
+ * @example
1653
+ * ```typescript
1654
+ * import { micromarkReferenceExtension } from './micromark-reference-extension'
1655
+ * import { fromMarkdown } from 'mdast-util-from-markdown'
1656
+ *
1657
+ * const extensions = [micromarkReferenceExtension()]
1658
+ * const ast = fromMarkdown(text, { extensions })
1659
+ * ```
1660
+ *
1661
+ * @author Incremark Team
1662
+ * @license MIT
1663
+ */
1664
+
1665
+ export { MicromarkAstBuilder, createMicromarkBuilder };
1666
+ //# sourceMappingURL=index.js.map
1667
+ //# sourceMappingURL=index.js.map