@incremark/core 0.3.0 → 0.3.2

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.
@@ -1,7 +1,7 @@
1
1
  import { Root, RootContent } from 'mdast';
2
- import { C as ContainerConfig, B as BlockStatus, P as ParsedBlock } from '../../index-mZ7yCqNH.js';
3
- import { I as IAstBuilder, E as EngineParserOptions } from '../../types-C_EW5vfp.js';
4
- export { b as IncremarkPlugin, c as MicromarkEngineExtension } from '../../types-C_EW5vfp.js';
2
+ import { C as ContainerConfig, B as BlockStatus, P as ParsedBlock } from '../../index-CWuosVAK.js';
3
+ import { I as IAstBuilder, E as EngineParserOptions } from '../../types-B7GTGJc2.js';
4
+ export { b as IncremarkPlugin, c as MicromarkEngineExtension } from '../../types-B7GTGJc2.js';
5
5
  import 'micromark-util-types';
6
6
  import 'mdast-util-from-markdown';
7
7
  import 'marked';
@@ -79,10 +79,19 @@ declare class MicromarkAstBuilder implements IAstBuilder {
79
79
  * @param startOffset 起始偏移量
80
80
  * @param rawText 原始文本
81
81
  * @param status 块状态
82
- * @param generateBlockId 生成块 ID 的函数
82
+ * @param generateBlockId 生成块 ID 的函数(接收 startOffset 参数)
83
83
  * @returns ParsedBlock 列表
84
84
  */
85
- nodesToBlocks(nodes: RootContent[], startOffset: number, rawText: string, status: BlockStatus, generateBlockId: () => string): ParsedBlock[];
85
+ nodesToBlocks(nodes: RootContent[], startOffset: number, rawText: string, status: BlockStatus, generateBlockId: (startOffset: number) => string): ParsedBlock[];
86
+ /**
87
+ * 更新配置选项
88
+ *
89
+ * 注意:由于 micromark 的扩展是在 constructor 中缓存的,
90
+ * 更新配置需要重新初始化扩展。
91
+ *
92
+ * @param options 部分配置选项
93
+ */
94
+ updateOptions(options: Partial<EngineParserOptions>): void;
86
95
  }
87
96
 
88
97
  /**
@@ -2,12 +2,12 @@ import { fromMarkdown } from 'mdast-util-from-markdown';
2
2
  import { gfmFromMarkdown } from 'mdast-util-gfm';
3
3
  import { gfm } from 'micromark-extension-gfm';
4
4
  import { gfmFootnoteFromMarkdown } from 'mdast-util-gfm-footnote';
5
- import { math } from 'micromark-extension-math';
6
5
  import { mathFromMarkdown } from 'mdast-util-math';
7
6
  import { directive } from 'micromark-extension-directive';
8
7
  import { directiveFromMarkdown } from 'mdast-util-directive';
9
- import { codes, constants, types } from 'micromark-util-symbol';
10
- import { markdownLineEndingOrSpace } from 'micromark-util-character';
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
11
  import { factoryDestination } from 'micromark-factory-destination';
12
12
  import { factoryTitle } from 'micromark-factory-title';
13
13
  import { factoryLabel } from 'micromark-factory-label';
@@ -16,6 +16,488 @@ import { gfmFootnote } from 'micromark-extension-gfm-footnote';
16
16
  import { normalizeIdentifier } from 'micromark-util-normalize-identifier';
17
17
 
18
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
+ }
19
501
 
20
502
  // src/extensions/html-extension/index.ts
21
503
  var DEFAULT_TAG_BLACKLIST = [
@@ -945,7 +1427,11 @@ var MicromarkAstBuilder = class {
945
1427
  this.cachedMdastExtensions.push(...gfmFromMarkdown(), gfmFootnoteFromMarkdown());
946
1428
  }
947
1429
  if (this.options.math) {
948
- this.cachedExtensions.push(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
+ }));
949
1435
  this.cachedMdastExtensions.push(mathFromMarkdown());
950
1436
  }
951
1437
  if (this.containerConfig !== void 0) {
@@ -1083,7 +1569,7 @@ var MicromarkAstBuilder = class {
1083
1569
  * @param startOffset 起始偏移量
1084
1570
  * @param rawText 原始文本
1085
1571
  * @param status 块状态
1086
- * @param generateBlockId 生成块 ID 的函数
1572
+ * @param generateBlockId 生成块 ID 的函数(接收 startOffset 参数)
1087
1573
  * @returns ParsedBlock 列表
1088
1574
  */
1089
1575
  nodesToBlocks(nodes, startOffset, rawText, status, generateBlockId) {
@@ -1095,7 +1581,7 @@ var MicromarkAstBuilder = class {
1095
1581
  const absoluteStart = startOffset + relativeStart;
1096
1582
  const absoluteEnd = startOffset + relativeEnd;
1097
1583
  blocks.push({
1098
- id: generateBlockId(),
1584
+ id: generateBlockId(absoluteStart),
1099
1585
  status,
1100
1586
  node,
1101
1587
  startOffset: absoluteStart,
@@ -1105,6 +1591,26 @@ var MicromarkAstBuilder = class {
1105
1591
  }
1106
1592
  return blocks;
1107
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
+ }
1108
1614
  };
1109
1615
 
1110
1616
  // src/engines/micromark/index.ts