@incremark/core 0.2.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,13 +1,39 @@
1
- import { P as ParserOptions, I as IncrementalUpdate, a as ParsedBlock, D as DefinitionMap, F as FootnoteDefinitionMap, b as ParserState, B as BlockStatus } from './index-BMUkM7mT.js';
2
- export { A as AstNode, c as BlockContext, e as BlockTypeInfo, C as ContainerConfig, d as ContainerMatch, J as HTML_ATTR_BLACKLIST, K as HTML_PROTOCOL_BLACKLIST, H as HTML_TAG_BLACKLIST, N as HtmlAttrInfo, R as HtmlContentType, M as HtmlElementNode, Q as HtmlTreeExtensionOptions, O as ParsedHtmlTag, s as createHtmlTreeTransformer, r as createInitialContext, o as detectContainer, p as detectContainerEnd, g as detectFenceEnd, f as detectFenceStart, x as detectHtmlContentType, E as findHtmlElementsByTag, G as htmlElementToString, L as htmlTreeExtension, q as isBlockBoundary, l as isBlockquoteStart, i as isEmptyLine, h as isHeading, m as isHtmlBlock, y as isHtmlElementNode, k as isListItemStart, n as isTableDelimiter, j as isThematicBreak, w as parseHtmlFragment, v as parseHtmlTag, t as transformHtmlNodes, u as updateContext, z as walkHtmlElements } from './index-BMUkM7mT.js';
1
+ import { I as IncrementalUpdate, P as ParsedBlock, D as DefinitionMap, F as FootnoteDefinitionMap, a as ParserState, B as BlockStatus } from './index-mZ7yCqNH.js';
2
+ export { A as AstNode, b as ParserOptions } from './index-mZ7yCqNH.js';
3
+ import { E as EngineParserOptions, I as IAstBuilder } from './types-C_EW5vfp.js';
4
+ export { a as EngineType, b as IncremarkPlugin, M as MarkedEngineExtension, c as MicromarkEngineExtension } from './types-C_EW5vfp.js';
3
5
  import { Root, RootContent, Text } from 'mdast';
4
6
  export { Root, RootContent } from 'mdast';
5
- export { calculateLineOffset, generateId, joinLines, resetIdCounter, splitLines } from './utils/index.js';
7
+ export { M as MarkedAstBuilder } from './MarkedAstBuildter-BsjxZko_.js';
6
8
  import 'micromark-util-types';
7
9
  import 'mdast-util-from-markdown';
10
+ import 'marked';
8
11
 
12
+ /**
13
+ * AST 构建器类型(用于注入)
14
+ */
15
+ type AstBuilderClass = new (options: EngineParserOptions) => IAstBuilder;
16
+ /**
17
+ * 扩展的解析器选项(支持注入自定义 AstBuilder)
18
+ */
19
+ interface IncremarkParserOptions extends EngineParserOptions {
20
+ /**
21
+ * 自定义 AST 构建器类
22
+ *
23
+ * 用于注入不同的引擎实现,实现 tree-shaking
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * // 使用 micromark 引擎
28
+ * import { MicromarkAstBuilder } from '@incremark/core/engines/micromark'
29
+ * const parser = createIncremarkParser({
30
+ * astBuilder: MicromarkAstBuilder
31
+ * })
32
+ * ```
33
+ */
34
+ astBuilder?: AstBuilderClass;
35
+ }
9
36
  declare class IncremarkParser {
10
- private buffer;
11
37
  private lines;
12
38
  /** 行偏移量前缀和:lineOffsets[i] = 第i行起始位置的偏移量 */
13
39
  private lineOffsets;
@@ -16,41 +42,25 @@ declare class IncremarkParser {
16
42
  private blockIdCounter;
17
43
  private context;
18
44
  private options;
19
- /** 缓存的容器配置,避免重复计算 */
20
- private readonly containerConfig;
21
- /** 缓存的 HTML 树配置,避免重复计算 */
22
- private readonly htmlTreeConfig;
45
+ /** 边界检测器 */
46
+ private readonly boundaryDetector;
47
+ /** AST 构建器 */
48
+ private readonly astBuilder;
49
+ /** Definition 管理器 */
50
+ private readonly definitionManager;
51
+ /** Footnote 管理器 */
52
+ private readonly footnoteManager;
23
53
  /** 上次 append 返回的 pending blocks,用于 getAst 复用 */
24
54
  private lastPendingBlocks;
25
- /** Definition 映射表(用于引用式图片和链接) */
26
- private definitionMap;
27
- /** Footnote Definition 映射表 */
28
- private footnoteDefinitionMap;
29
- /** Footnote Reference 出现顺序(按引用在文档中的顺序) */
30
- private footnoteReferenceOrder;
31
- constructor(options?: ParserOptions);
55
+ constructor(options?: IncremarkParserOptions);
32
56
  private generateBlockId;
33
- private computeContainerConfig;
34
- private computeHtmlTreeConfig;
35
57
  /**
36
- * HTML 节点转换为纯文本
37
- * 递归处理 AST 中所有 html 类型的节点
38
- * - 块级 HTML 节点 → 转换为 paragraph 包含 text
39
- * - 内联 HTML 节点(在段落内部)→ 转换为 text 节点
58
+ * 更新已完成的 blocks 中的 definitions 和 footnote definitions
40
59
  */
41
- private convertHtmlToText;
42
- private parse;
43
60
  private updateDefinitionsFromCompletedBlocks;
44
- private findDefinition;
45
- private findFootnoteDefinition;
46
- /**
47
- * 收集 AST 中的脚注引用(按出现顺序)
48
- * 用于确定脚注的显示顺序
49
- */
50
- private collectFootnoteReferences;
51
61
  /**
52
62
  * 增量更新 lines 和 lineOffsets
53
- * 只处理新增的内容,避免全量 split
63
+ * 优化策略:只 split 新增的 chunk,不拼接旧字符串,避免长行性能劣化
54
64
  */
55
65
  private updateLines;
56
66
  /**
@@ -62,23 +72,6 @@ declare class IncremarkParser {
62
72
  * 返回稳定边界行号和该行对应的上下文(用于后续更新,避免重复计算)
63
73
  */
64
74
  private findStableBoundary;
65
- private checkStability;
66
- /**
67
- * 从指定行向上查找脚注定义的起始行
68
- *
69
- * @param fromLine 开始查找的行索引
70
- * @returns 脚注起始行索引,如果不属于脚注返回 -1
71
- *
72
- * @example
73
- * // 假设 lines 为:
74
- * // 0: "[^1]: 第一行"
75
- * // 1: " 第二行"
76
- * // 2: " 第三行"
77
- * findFootnoteStart(2) // 返回 0
78
- * findFootnoteStart(1) // 返回 0
79
- */
80
- private findFootnoteStart;
81
- private nodesToBlocks;
82
75
  /**
83
76
  * 追加新的 chunk 并返回增量更新
84
77
  */
@@ -139,8 +132,25 @@ declare class IncremarkParser {
139
132
  }
140
133
  /**
141
134
  * 创建 Incremark 解析器实例
135
+ *
136
+ * @param options 解析器配置
137
+ * @param options.astBuilder 自定义 AST 构建器类(用于切换引擎)
138
+ * @param options.plugins 统一插件列表
139
+ *
140
+ * @example
141
+ * ```ts
142
+ * // 使用默认的 marked 引擎(极速模式)
143
+ * const parser = createIncremarkParser({ gfm: true, math: true })
144
+ *
145
+ * // 使用 micromark 引擎(需要单独导入,支持 tree-shaking)
146
+ * import { MicromarkAstBuilder } from '@incremark/core/engines/micromark'
147
+ * const parser = createIncremarkParser({
148
+ * astBuilder: MicromarkAstBuilder,
149
+ * gfm: true
150
+ * })
151
+ * ```
142
152
  */
143
- declare function createIncremarkParser(options?: ParserOptions): IncremarkParser;
153
+ declare function createIncremarkParser(options?: IncremarkParserOptions): IncremarkParser;
144
154
 
145
155
  /**
146
156
  * 源 Block 类型(来自解析器)
@@ -452,15 +462,19 @@ interface AccumulatedChunks {
452
462
  /**
453
463
  * 截断 AST 节点,只保留前 maxChars 个字符
454
464
  * 支持 chunks(用于渐入动画)
455
- * 支持增量模式:跳过已处理的字符,只处理新增部分
456
465
  *
457
- * @param node 原始节点
466
+ * 性能说明:
467
+ * - 此函数每次调用都会从头遍历 AST 节点
468
+ * - 但由于 BlockTransformer 是按 block 处理的,每次只遍历单个 block 的 AST
469
+ * - 单个 block(如一个段落、一个代码块)通常只有几百个字符,性能开销很小
470
+ * - 对于整个文档的渲染,已完成的 blocks 不会重新遍历
471
+ *
472
+ * @param node 原始节点(单个 block 的 AST)
458
473
  * @param maxChars 最大字符数
459
474
  * @param accumulatedChunks 累积的 chunks 信息(用于渐入动画)
460
- * @param skipChars 跳过前 N 个字符(已处理的部分,用于增量追加)
461
475
  * @returns 截断后的节点,如果 maxChars <= 0 返回 null
462
476
  */
463
- declare function sliceAst(node: RootContent, maxChars: number, accumulatedChunks?: AccumulatedChunks, skipChars?: number): RootContent | null;
477
+ declare function sliceAst(node: RootContent, maxChars: number, accumulatedChunks?: AccumulatedChunks): RootContent | null;
464
478
  /**
465
479
  * 深拷贝 AST 节点
466
480
  * 使用递归浅拷贝实现,比 JSON.parse/stringify 更高效
@@ -520,4 +534,4 @@ declare const allPlugins: TransformerPlugin[];
520
534
  */
521
535
  declare function createPlugin(name: string, matcher: (node: RootContent) => boolean, options?: Partial<Omit<TransformerPlugin, 'name' | 'match'>>): TransformerPlugin;
522
536
 
523
- export { type AnimationEffect, BlockStatus, BlockTransformer, type DisplayBlock, IncremarkParser, IncrementalUpdate, ParsedBlock, ParserOptions, ParserState, type SourceBlock, type TextChunk, type TextNodeWithChunks, type TransformerOptions, type TransformerPlugin, type TransformerState, allPlugins, cloneNode, codeBlockPlugin, countChars, createBlockTransformer, createIncremarkParser, createPlugin, defaultPlugins, imagePlugin, mathPlugin, mermaidPlugin, sliceAst, thematicBreakPlugin };
537
+ export { type AnimationEffect, type AstBuilderClass, BlockStatus, BlockTransformer, type DisplayBlock, EngineParserOptions, IAstBuilder, IncremarkParser, type IncremarkParserOptions, IncrementalUpdate, ParsedBlock, ParserState, type SourceBlock, type TextChunk, type TextNodeWithChunks, type TransformerOptions, type TransformerPlugin, type TransformerState, allPlugins, cloneNode, codeBlockPlugin, countChars, createBlockTransformer, createIncremarkParser, createPlugin, defaultPlugins, imagePlugin, mathPlugin, mermaidPlugin, sliceAst, thematicBreakPlugin };