@incremark/core 0.2.6 → 0.2.7

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,225 @@
1
+ import { Parent, RootContent, Root, Definition, FootnoteDefinition } from 'mdast';
2
+ import { Extension } from 'micromark-util-types';
3
+ import { Extension as Extension$1 } from 'mdast-util-from-markdown';
4
+
5
+ declare module 'mdast' {
6
+ interface RootContentMap {
7
+ htmlElement: HtmlElementNode;
8
+ }
9
+ interface PhrasingContentMap {
10
+ htmlElement: HtmlElementNode;
11
+ }
12
+ }
13
+ /**
14
+ * 自定义 HTML 元素节点类型
15
+ */
16
+ interface HtmlElementNode extends Parent {
17
+ type: 'htmlElement';
18
+ tagName: string;
19
+ attrs: Record<string, string>;
20
+ children: RootContent[];
21
+ data?: {
22
+ rawHtml?: string;
23
+ parsed?: boolean;
24
+ originalType?: string;
25
+ };
26
+ }
27
+ /**
28
+ * HTML 树扩展配置
29
+ */
30
+ interface HtmlTreeExtensionOptions {
31
+ /**
32
+ * 标签黑名单 - 这些标签会被过滤掉(XSS 防护)
33
+ * 默认包含危险标签:script, style, iframe, object, embed, form, input, button, textarea, select
34
+ */
35
+ tagBlacklist?: string[];
36
+ /**
37
+ * 属性黑名单 - 这些属性会被过滤掉(XSS 防护)
38
+ * 默认包含所有 on* 事件属性和 javascript: 协议
39
+ */
40
+ attrBlacklist?: string[];
41
+ /**
42
+ * 协议黑名单 - URL 属性中禁止的协议
43
+ * 默认包含 javascript:, vbscript:, data: (允许 data:image/)
44
+ */
45
+ protocolBlacklist?: string[];
46
+ /**
47
+ * 是否保留原始 HTML 在 data 中
48
+ * 默认为 true
49
+ */
50
+ preserveRawHtml?: boolean;
51
+ /**
52
+ * 自定义标签处理器
53
+ * 可以对特定标签进行自定义处理
54
+ */
55
+ tagHandlers?: Record<string, (node: HtmlElementNode) => HtmlElementNode | null>;
56
+ }
57
+
58
+ /**
59
+ * Definition 映射类型
60
+ */
61
+ interface DefinitionMap {
62
+ [identifier: string]: Definition;
63
+ }
64
+ interface FootnoteDefinitionMap {
65
+ [identifier: string]: FootnoteDefinition;
66
+ }
67
+ /**
68
+ * 解析块的状态
69
+ */
70
+ type BlockStatus = 'pending' | 'completed';
71
+ /**
72
+ * AST 节点的通用接口(用于遍历)
73
+ * 统一定义,避免各模块重复声明
74
+ */
75
+ interface AstNode {
76
+ type: string;
77
+ value?: string;
78
+ children?: AstNode[];
79
+ [key: string]: unknown;
80
+ }
81
+ /**
82
+ * 解析出的块
83
+ */
84
+ interface ParsedBlock {
85
+ /** 块的唯一 ID */
86
+ id: string;
87
+ /** 块状态 */
88
+ status: BlockStatus;
89
+ /** AST 节点 */
90
+ node: RootContent;
91
+ /** 原始文本起始位置(相对于完整文档) */
92
+ startOffset: number;
93
+ /** 原始文本结束位置 */
94
+ endOffset: number;
95
+ /** 原始文本内容 */
96
+ rawText: string;
97
+ }
98
+ /**
99
+ * 增量更新事件
100
+ */
101
+ interface IncrementalUpdate {
102
+ /** 新完成的块 */
103
+ completed: ParsedBlock[];
104
+ /** 更新的块(内容变化) */
105
+ updated: ParsedBlock[];
106
+ /** 当前正在解析中的块(可能不完整) */
107
+ pending: ParsedBlock[];
108
+ /** 完整的 AST(包含所有已解析的内容) */
109
+ ast: Root;
110
+ /** Definition 映射表(用于引用式图片和链接) */
111
+ definitions: DefinitionMap;
112
+ /** Footnote Definition 映射表 */
113
+ footnoteDefinitions: FootnoteDefinitionMap;
114
+ /** 脚注引用的出现顺序(用于渲染时排序) */
115
+ footnoteReferenceOrder: string[];
116
+ }
117
+ /**
118
+ * 容器语法配置
119
+ */
120
+ interface ContainerConfig {
121
+ /** 容器标记字符,默认 ':' */
122
+ marker?: string;
123
+ /** 最小标记长度,默认 3 */
124
+ minMarkerLength?: number;
125
+ /** 允许的容器名称(如 ['warning', 'info', 'youtube']),undefined 表示允许所有 */
126
+ allowedNames?: string[];
127
+ }
128
+ /**
129
+ * 解析器状态变化事件
130
+ */
131
+ interface ParserState {
132
+ /** 已完成的块 */
133
+ completedBlocks: ParsedBlock[];
134
+ /** 待处理的块 */
135
+ pendingBlocks: ParsedBlock[];
136
+ /** 完整的 Markdown 内容 */
137
+ markdown: string;
138
+ /** 完整的 AST */
139
+ ast: Root;
140
+ definitions: DefinitionMap;
141
+ footnoteDefinitions: FootnoteDefinitionMap;
142
+ }
143
+ /**
144
+ * 解析器配置
145
+ */
146
+ interface ParserOptions {
147
+ /** 启用 GFM 扩展(表格、任务列表等) */
148
+ gfm?: boolean;
149
+ /**
150
+ * 启用数学公式支持($..$ 行内公式和 $$...$$ 块级公式)
151
+ * - false/undefined: 禁用(默认)
152
+ * - true: 启用数学公式解析
153
+ */
154
+ math?: boolean;
155
+ /**
156
+ * 启用 ::: 容器语法支持(用于边界检测)
157
+ * - false: 禁用(默认)
158
+ * - true: 使用默认配置启用
159
+ * - ContainerConfig: 使用自定义配置启用
160
+ */
161
+ containers?: boolean | ContainerConfig;
162
+ /**
163
+ * 启用 HTML 树转换
164
+ * - false/undefined: 禁用(默认),HTML 节点保持原始 type: 'html' 格式
165
+ * - true: 使用默认配置启用,将 HTML 节点转换为结构化的 htmlElement 节点
166
+ * - HtmlTreeExtensionOptions: 使用自定义配置启用(可配置黑名单等)
167
+ */
168
+ htmlTree?: boolean | HtmlTreeExtensionOptions;
169
+ /** 自定义块边界检测函数 */
170
+ blockBoundaryDetector?: (content: string, position: number) => boolean;
171
+ /** 自定义 micromark 扩展(如 directive) */
172
+ extensions?: Extension[];
173
+ /** 自定义 mdast 扩展(如 directiveFromMarkdown) */
174
+ mdastExtensions?: Extension$1[];
175
+ /** 状态变化回调 */
176
+ onChange?: (state: ParserState) => void;
177
+ }
178
+ /**
179
+ * 块上下文
180
+ */
181
+ interface BlockContext {
182
+ /** 当前是否在代码块中 */
183
+ inFencedCode: boolean;
184
+ /** 代码块的 fence 字符(` 或 ~) */
185
+ fenceChar?: string;
186
+ /** 代码块的 fence 长度 */
187
+ fenceLength?: number;
188
+ /** 当前列表嵌套深度 */
189
+ listDepth: number;
190
+ /** 当前引用嵌套深度 */
191
+ blockquoteDepth: number;
192
+ /** 当前是否在容器块中 */
193
+ inContainer: boolean;
194
+ /** 容器的标记长度 */
195
+ containerMarkerLength?: number;
196
+ /** 容器名称 */
197
+ containerName?: string;
198
+ /** 容器嵌套深度(支持嵌套容器) */
199
+ containerDepth: number;
200
+ /** 当前是否在列表中 */
201
+ inList: boolean;
202
+ /** 当前列表是否是有序列表 */
203
+ listOrdered?: boolean;
204
+ /** 当前列表的基础缩进 */
205
+ listIndent?: number;
206
+ /** 遇到空行后,列表可能结束(等待下一行确认) */
207
+ listMayEnd?: boolean;
208
+ /** 当前是否在脚注定义中 */
209
+ inFootnote?: boolean;
210
+ /** 脚注标识符 */
211
+ footnoteIdentifier?: string;
212
+ }
213
+ /**
214
+ * 容器检测结果
215
+ */
216
+ interface ContainerMatch {
217
+ /** 容器名称 */
218
+ name: string;
219
+ /** 标记长度(冒号数量) */
220
+ markerLength: number;
221
+ /** 是否是结束标记 */
222
+ isEnd: boolean;
223
+ }
224
+
225
+ export type { AstNode as A, BlockStatus as B, ContainerConfig as C, DefinitionMap as D, FootnoteDefinitionMap as F, IncrementalUpdate as I, ParserOptions as P, ParsedBlock as a, ParserState as b, BlockContext as c, ContainerMatch as d };
package/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
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 { P as ParserOptions, I as IncrementalUpdate, a as ParsedBlock, D as DefinitionMap, F as FootnoteDefinitionMap, b as ParserState, B as BlockStatus } from './index-CfgnWMWh.js';
2
+ export { A as AstNode } from './index-CfgnWMWh.js';
3
3
  import { Root, RootContent, Text } from 'mdast';
4
4
  export { Root, RootContent } from 'mdast';
5
- export { calculateLineOffset, generateId, joinLines, resetIdCounter, splitLines } from './utils/index.js';
6
5
  import 'micromark-util-types';
7
6
  import 'mdast-util-from-markdown';
8
7
 
@@ -16,33 +15,22 @@ declare class IncremarkParser {
16
15
  private blockIdCounter;
17
16
  private context;
18
17
  private options;
19
- /** 缓存的容器配置,避免重复计算 */
20
- private readonly containerConfig;
21
- /** 缓存的 HTML 树配置,避免重复计算 */
22
- private readonly htmlTreeConfig;
18
+ /** 边界检测器 */
19
+ private readonly boundaryDetector;
20
+ /** AST 构建器 */
21
+ private readonly astBuilder;
22
+ /** Definition 管理器 */
23
+ private readonly definitionManager;
24
+ /** Footnote 管理器 */
25
+ private readonly footnoteManager;
23
26
  /** 上次 append 返回的 pending blocks,用于 getAst 复用 */
24
27
  private lastPendingBlocks;
25
- /** Definition 映射表(用于引用式图片和链接) */
26
- private definitionMap;
27
- /** Footnote Definition 映射表 */
28
- private footnoteDefinitionMap;
29
- /** Footnote Reference 出现顺序(按引用在文档中的顺序) */
30
- private footnoteReferenceOrder;
31
28
  constructor(options?: ParserOptions);
32
29
  private generateBlockId;
33
- private computeContainerConfig;
34
- private computeHtmlTreeConfig;
35
30
  /**
36
- * HTML 节点转换为纯文本
37
- * 递归处理 AST 中所有 html 类型的节点
38
- * - 块级 HTML 节点 → 转换为 paragraph 包含 text
39
- * - 内联 HTML 节点(在段落内部)→ 转换为 text 节点
31
+ * 更新已完成的 blocks 中的 definitions 和 footnote definitions
40
32
  */
41
- private convertHtmlToText;
42
- private parse;
43
33
  private updateDefinitionsFromCompletedBlocks;
44
- private findDefinition;
45
- private findFootnoteDefinition;
46
34
  /**
47
35
  * 收集 AST 中的脚注引用(按出现顺序)
48
36
  * 用于确定脚注的显示顺序
@@ -62,23 +50,6 @@ declare class IncremarkParser {
62
50
  * 返回稳定边界行号和该行对应的上下文(用于后续更新,避免重复计算)
63
51
  */
64
52
  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
53
  /**
83
54
  * 追加新的 chunk 并返回增量更新
84
55
  */