@incremark/core 0.2.2 → 0.2.4

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,183 +0,0 @@
1
- import type { Root, RootContent } from 'mdast'
2
- import type { Extension as MicromarkExtension } from 'micromark-util-types'
3
- import type { Extension as MdastExtension } from 'mdast-util-from-markdown'
4
- import type { HtmlTreeExtensionOptions } from '../extensions/html-extension'
5
- import type { Definition, FootnoteDefinition } from 'mdast'
6
-
7
- /**
8
- * Definition 映射类型
9
- */
10
- export interface DefinitionMap {
11
- [identifier: string]: Definition
12
- }
13
-
14
- export interface FootnoteDefinitionMap {
15
- [identifier: string]: FootnoteDefinition
16
- }
17
-
18
- /**
19
- * 解析块的状态
20
- */
21
- export type BlockStatus =
22
- | 'pending' // 正在接收中,可能不完整
23
- | 'stable' // 可能完整,但下一个 chunk 可能会改变它
24
- | 'completed' // 确认完成,不会再改变
25
-
26
- /**
27
- * AST 节点的通用接口(用于遍历)
28
- * 统一定义,避免各模块重复声明
29
- */
30
- export interface AstNode {
31
- type: string
32
- value?: string
33
- children?: AstNode[]
34
- [key: string]: unknown
35
- }
36
-
37
- /**
38
- * 解析出的块
39
- */
40
- export interface ParsedBlock {
41
- /** 块的唯一 ID */
42
- id: string
43
- /** 块状态 */
44
- status: BlockStatus
45
- /** AST 节点 */
46
- node: RootContent
47
- /** 原始文本起始位置(相对于完整文档) */
48
- startOffset: number
49
- /** 原始文本结束位置 */
50
- endOffset: number
51
- /** 原始文本内容 */
52
- rawText: string
53
- }
54
-
55
- /**
56
- * 增量更新事件
57
- */
58
- export interface IncrementalUpdate {
59
- /** 新完成的块 */
60
- completed: ParsedBlock[]
61
- /** 更新的块(内容变化) */
62
- updated: ParsedBlock[]
63
- /** 当前正在解析中的块(可能不完整) */
64
- pending: ParsedBlock[]
65
- /** 完整的 AST(包含所有已解析的内容) */
66
- ast: Root
67
- /** Definition 映射表(用于引用式图片和链接) */
68
- definitions: DefinitionMap
69
- /** Footnote Definition 映射表 */
70
- footnoteDefinitions: FootnoteDefinitionMap
71
- /** 脚注引用的出现顺序(用于渲染时排序) */
72
- footnoteReferenceOrder: string[]
73
- }
74
-
75
- /**
76
- * 容器语法配置
77
- */
78
- export interface ContainerConfig {
79
- /** 容器标记字符,默认 ':' */
80
- marker?: string
81
- /** 最小标记长度,默认 3 */
82
- minMarkerLength?: number
83
- /** 允许的容器名称(如 ['warning', 'info', 'youtube']),undefined 表示允许所有 */
84
- allowedNames?: string[]
85
- }
86
-
87
- /**
88
- * 解析器状态变化事件
89
- */
90
- export interface ParserState {
91
- /** 已完成的块 */
92
- completedBlocks: ParsedBlock[]
93
- /** 待处理的块 */
94
- pendingBlocks: ParsedBlock[]
95
- /** 完整的 Markdown 内容 */
96
- markdown: string
97
- /** 完整的 AST */
98
- ast: Root,
99
- definitions: DefinitionMap,
100
- footnoteDefinitions: FootnoteDefinitionMap
101
- }
102
-
103
- /**
104
- * 解析器配置
105
- */
106
- export interface ParserOptions {
107
- /** 启用 GFM 扩展(表格、任务列表等) */
108
- gfm?: boolean
109
- /**
110
- * 启用 ::: 容器语法支持(用于边界检测)
111
- * - false: 禁用(默认)
112
- * - true: 使用默认配置启用
113
- * - ContainerConfig: 使用自定义配置启用
114
- */
115
- containers?: boolean | ContainerConfig
116
- /**
117
- * 启用 HTML 树转换
118
- * - false/undefined: 禁用(默认),HTML 节点保持原始 type: 'html' 格式
119
- * - true: 使用默认配置启用,将 HTML 节点转换为结构化的 htmlElement 节点
120
- * - HtmlTreeExtensionOptions: 使用自定义配置启用(可配置黑名单等)
121
- */
122
- htmlTree?: boolean | HtmlTreeExtensionOptions
123
- /** 自定义块边界检测函数 */
124
- blockBoundaryDetector?: (content: string, position: number) => boolean
125
- /** 自定义 micromark 扩展(如 directive) */
126
- extensions?: MicromarkExtension[]
127
- /** 自定义 mdast 扩展(如 directiveFromMarkdown) */
128
- mdastExtensions?: MdastExtension[]
129
- /** 状态变化回调 */
130
- onChange?: (state: ParserState) => void
131
- }
132
-
133
- /**
134
- * 块上下文
135
- */
136
- export interface BlockContext {
137
- /** 当前是否在代码块中 */
138
- inFencedCode: boolean
139
- /** 代码块的 fence 字符(` 或 ~) */
140
- fenceChar?: string
141
- /** 代码块的 fence 长度 */
142
- fenceLength?: number
143
- /** 当前列表嵌套深度 */
144
- listDepth: number
145
- /** 当前引用嵌套深度 */
146
- blockquoteDepth: number
147
- /** 当前是否在容器块中 */
148
- inContainer: boolean
149
- /** 容器的标记长度 */
150
- containerMarkerLength?: number
151
- /** 容器名称 */
152
- containerName?: string
153
- /** 容器嵌套深度(支持嵌套容器) */
154
- containerDepth: number
155
- }
156
-
157
- /**
158
- * 容器检测结果
159
- */
160
- export interface ContainerMatch {
161
- /** 容器名称 */
162
- name: string
163
- /** 标记长度(冒号数量) */
164
- markerLength: number
165
- /** 是否是结束标记 */
166
- isEnd: boolean
167
- }
168
-
169
- /**
170
- * 块类型检测结果
171
- */
172
- export interface BlockTypeInfo {
173
- type: string
174
- /** 是否是容器节点(可以包含其他块) */
175
- isContainer: boolean
176
- /** 是否需要显式关闭(如代码块) */
177
- requiresClosing: boolean
178
- /** 关闭模式 */
179
- closingPattern?: RegExp
180
- }
181
-
182
- export type { Root, RootContent }
183
-
@@ -1,53 +0,0 @@
1
- /**
2
- * 工具函数
3
- */
4
-
5
- import type { Definition, FootnoteDefinition, RootContent } from "mdast"
6
-
7
- /**
8
- * 生成唯一 ID
9
- */
10
- let idCounter = 0
11
- export function generateId(prefix = 'block'): string {
12
- return `${prefix}-${++idCounter}`
13
- }
14
-
15
- /**
16
- * 重置 ID 计数器(用于测试)
17
- */
18
- export function resetIdCounter(): void {
19
- idCounter = 0
20
- }
21
-
22
- /**
23
- * 计算行的偏移量
24
- */
25
- export function calculateLineOffset(lines: string[], lineIndex: number): number {
26
- let offset = 0
27
- for (let i = 0; i < lineIndex && i < lines.length; i++) {
28
- offset += lines[i].length + 1 // +1 for newline
29
- }
30
- return offset
31
- }
32
-
33
- /**
34
- * 将文本按行分割
35
- */
36
- export function splitLines(text: string): string[] {
37
- return text.split('\n')
38
- }
39
-
40
- /**
41
- * 合并行为文本
42
- */
43
- export function joinLines(lines: string[], start: number, end: number): string {
44
- return lines.slice(start, end + 1).join('\n')
45
- }
46
-
47
- export function isDefinitionNode(node: RootContent): node is Definition {
48
- return node.type === 'definition'
49
- }
50
-
51
- export function isFootnoteDefinitionNode(node: RootContent): node is FootnoteDefinition {
52
- return node.type === 'footnoteDefinition'
53
- }