stream-markdown-parser 0.0.1 → 0.0.3

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/README.md CHANGED
@@ -1,97 +1,331 @@
1
-
2
1
  # stream-markdown-parser
3
2
 
4
- Lightweight, framework-agnostic Markdown parser implemented in TypeScript. It focuses on producing a typed, structured representation of Markdown content so you can render it in Vue, React, or any other environment.
5
-
6
3
  [![NPM version](https://img.shields.io/npm/v/stream-markdown-parser?color=a1b858&label=)](https://www.npmjs.com/package/stream-markdown-parser)
7
4
  [![中文版](https://img.shields.io/badge/docs-中文文档-blue)](README.zh-CN.md)
8
5
  [![NPM downloads](https://img.shields.io/npm/dm/stream-markdown-parser)](https://www.npmjs.com/package/stream-markdown-parser)
9
6
  [![Bundle size](https://img.shields.io/bundlephobia/minzip/stream-markdown-parser)](https://bundlephobia.com/package/stream-markdown-parser)
10
7
  [![License](https://img.shields.io/npm/l/stream-markdown-parser)](./LICENSE)
11
8
 
12
- ## Highlights
9
+ Pure markdown parser and renderer utilities with streaming support - framework agnostic.
10
+
11
+ This package contains the core markdown parsing logic extracted from `stream-markdown-parser`, making it usable in any JavaScript/TypeScript project without Vue dependencies.
13
12
 
14
- - Framework-agnostic: pure TypeScript, no runtime framework dependency
15
- - Typed output: full TypeScript definitions for the produced node tree
16
- - Extensible: built on top of markdown-it and supports plugins (math, containers, etc.)
17
- - Optimized for streaming / large documents
13
+ ## Features
18
14
 
19
- ## Install
15
+ - 🚀 **Pure JavaScript** - No framework dependencies
16
+ - 📦 **Lightweight** - Minimal bundle size
17
+ - 🔧 **Extensible** - Plugin-based architecture
18
+ - 🎯 **Type-safe** - Full TypeScript support
19
+ - ⚡ **Fast** - Optimized for performance
20
+ - 🌊 **Streaming-friendly** - Progressive parsing support
21
+
22
+ ## Installation
20
23
 
21
24
  ```bash
25
+ pnpm add stream-markdown-parser
26
+ # or
22
27
  npm install stream-markdown-parser
23
28
  # or
24
- pnpm add stream-markdown-parser
29
+ yarn add stream-markdown-parser
25
30
  ```
26
31
 
27
- ## Quickstart
32
+ ## Usage
33
+
34
+ ### Basic Example
28
35
 
29
- ```ts
36
+ ```typescript
30
37
  import { getMarkdown, parseMarkdownToStructure } from 'stream-markdown-parser'
31
38
 
39
+ // Create a markdown-it instance with default plugins
32
40
  const md = getMarkdown()
41
+
42
+ // Parse markdown to HTML
43
+ const html = md.render('# Hello World\n\nThis is **bold**.')
44
+
45
+ // Or parse to AST structure
33
46
  const nodes = parseMarkdownToStructure('# Hello World', md)
34
47
  console.log(nodes)
48
+ // [{ type: 'heading', level: 1, children: [...] }]
35
49
  ```
36
50
 
37
- ## Examples
51
+ ### With Math Options
52
+
53
+ ```typescript
54
+ import { getMarkdown, setDefaultMathOptions } from 'stream-markdown-parser'
38
55
 
39
- Basic parsing, math and custom container usage:
56
+ // Set global math options
57
+ setDefaultMathOptions({
58
+ commands: ['infty', 'perp', 'alpha'],
59
+ escapeExclamation: true
60
+ })
40
61
 
41
- ```ts
42
- // basic
43
62
  const md = getMarkdown()
44
- parseMarkdownToStructure('# Title', md)
63
+ ```
64
+
65
+ ### With Custom i18n
66
+
67
+ ```typescript
68
+ import { getMarkdown } from 'stream-markdown-parser'
45
69
 
46
- // enable math
47
- const mdWithMath = getMarkdown({ enableMath: true })
48
- parseMarkdownToStructure('Inline $x^2$ and block math:\n$$x^2$$', mdWithMath)
70
+ // Using translation map
71
+ const md = getMarkdown('editor-1', {
72
+ i18n: {
73
+ 'common.copy': '复制',
74
+ }
75
+ })
49
76
 
50
- // enable custom containers
51
- const mdWithContainers = getMarkdown({ enableContainers: true })
52
- parseMarkdownToStructure('::: tip\nHi\n:::', mdWithContainers)
77
+ // Or using a translation function
78
+ const md = getMarkdown('editor-1', {
79
+ i18n: (key: string) => translateFunction(key)
80
+ })
53
81
  ```
54
82
 
55
- You can also process tokens directly:
83
+ ### With Plugins
56
84
 
57
- ```ts
58
- import { processTokens } from 'stream-markdown-parser'
59
- import MarkdownIt from 'markdown-it'
85
+ ```typescript
86
+ import customPlugin from 'markdown-it-custom-plugin'
87
+ import { getMarkdown } from 'stream-markdown-parser'
60
88
 
61
- const md = new MarkdownIt()
62
- const tokens = md.parse('# Hello', {})
63
- const nodes = processTokens(tokens)
89
+ const md = getMarkdown('editor-1', {
90
+ plugin: [
91
+ [customPlugin, { /* options */ }]
92
+ ]
93
+ })
64
94
  ```
65
95
 
66
- ## API (overview)
96
+ ### Advanced: Custom Rules
67
97
 
68
- - getMarkdown(options?): returns a configured markdown-it instance
69
- - parseMarkdownToStructure(markdown, md, options?): parse a markdown string into typed nodes
70
- - processTokens(tokens): convert markdown-it tokens into typed nodes
71
- - parseInlineTokens(tokens): parse inline tokens
98
+ ```typescript
99
+ import { getMarkdown } from 'stream-markdown-parser'
72
100
 
73
- See `src/types/index.ts` for the full `ParsedNode` definitions.
101
+ const md = getMarkdown('editor-1', {
102
+ apply: [
103
+ (md) => {
104
+ // Add custom inline rule
105
+ md.inline.ruler.before('emphasis', 'custom', (state, silent) => {
106
+ // Your custom logic
107
+ return false
108
+ })
109
+ }
110
+ ]
111
+ })
112
+ ```
74
113
 
75
- ## Integration
114
+ ## API
76
115
 
77
- - React / Vue / Vanilla: parse to nodes, then render with your components
116
+ ### Main Functions
78
117
 
79
- ## Build & Dev
118
+ #### `getMarkdown(msgId?, options?)`
80
119
 
81
- This package is built using Vite and `vite-plugin-dts` for types generation. In this monorepo the package is under `packages/parser`.
120
+ Creates a configured markdown-it instance.
82
121
 
83
- Build locally:
122
+ **Parameters:**
123
+ - `msgId` (string, optional): Unique identifier for this instance. Default: `editor-${Date.now()}`
124
+ - `options` (GetMarkdownOptions, optional): Configuration options
84
125
 
85
- ```bash
86
- pnpm --filter ./packages/parser build
126
+ **Options:**
127
+ ```typescript
128
+ interface GetMarkdownOptions {
129
+ // Array of markdown-it plugins to use
130
+ plugin?: Array<Plugin | [Plugin, any]>
131
+
132
+ // Array of functions to mutate the md instance
133
+ apply?: Array<(md: MarkdownIt) => void>
134
+
135
+ // Translation function or translation map
136
+ i18n?: ((key: string) => string) | Record<string, string>
137
+ }
87
138
  ```
88
139
 
89
- Typecheck:
140
+ #### `parseMarkdownToStructure(content, md?, options?)`
90
141
 
91
- ```bash
92
- pnpm --filter ./packages/parser -w -C . typecheck
142
+ Parses markdown content into a structured node tree.
143
+
144
+ **Parameters:**
145
+ - `content` (string): The markdown content to parse
146
+ - `md` (MarkdownIt, optional): A markdown-it instance. If not provided, creates one using `getMarkdown()`
147
+ - `options` (ParseOptions, optional): Parsing options with hooks
148
+
149
+ **Returns:** `ParsedNode[]` - An array of parsed nodes
150
+
151
+ #### `processTokens(tokens)`
152
+
153
+ Processes raw markdown-it tokens into a flat array.
154
+
155
+ #### `parseInlineTokens(tokens, md)`
156
+
157
+ Parses inline markdown-it tokens.
158
+
159
+ ### Configuration Functions
160
+
161
+ #### `setDefaultMathOptions(options)`
162
+
163
+ Set global math rendering options.
164
+
165
+ **Parameters:**
166
+ - `options` (MathOptions): Math configuration options
167
+
168
+ ```typescript
169
+ interface MathOptions {
170
+ commands?: readonly string[] // LaTeX commands to escape
171
+ escapeExclamation?: boolean // Escape standalone '!' (default: true)
172
+ }
93
173
  ```
94
174
 
175
+ ### Utility Functions
176
+
177
+ #### `isMathLike(content)`
178
+
179
+ Heuristic function to detect if content looks like mathematical notation.
180
+
181
+ **Parameters:**
182
+ - `content` (string): Content to check
183
+
184
+ **Returns:** `boolean`
185
+
186
+ #### `findMatchingClose(src, startIdx, open, close)`
187
+
188
+ Find the matching closing delimiter in a string, handling nested pairs.
189
+
190
+ **Parameters:**
191
+ - `src` (string): Source string
192
+ - `startIdx` (number): Start index to search from
193
+ - `open` (string): Opening delimiter
194
+ - `close` (string): Closing delimiter
195
+
196
+ **Returns:** `number` - Index of matching close, or -1 if not found
197
+
198
+ #### `parseFenceToken(token)`
199
+
200
+ Parse a code fence token into a CodeBlockNode.
201
+
202
+ **Parameters:**
203
+ - `token` (MarkdownToken): markdown-it token
204
+
205
+ **Returns:** `CodeBlockNode`
206
+
207
+ #### `normalizeStandaloneBackslashT(content, options?)`
208
+
209
+ Normalize backslash-t sequences in math content.
210
+
211
+ **Parameters:**
212
+ - `content` (string): Content to normalize
213
+ - `options` (MathOptions, optional): Math options
214
+
215
+ **Returns:** `string`
216
+
217
+ ### Plugin Functions
218
+
219
+ #### `applyMath(md, options?)`
220
+
221
+ Apply math plugin to markdown-it instance.
222
+
223
+ **Parameters:**
224
+ - `md` (MarkdownIt): markdown-it instance
225
+ - `options` (MathOptions, optional): Math rendering options
226
+
227
+ #### `applyContainers(md)`
228
+
229
+ Apply container plugins to markdown-it instance.
230
+
231
+ **Parameters:**
232
+ - `md` (MarkdownIt): markdown-it instance
233
+
234
+ ### Constants
235
+
236
+ #### `KATEX_COMMANDS`
237
+
238
+ Array of common KaTeX commands for escaping.
239
+
240
+ #### `TEX_BRACE_COMMANDS`
241
+
242
+ Array of TeX commands that use braces.
243
+
244
+ #### `ESCAPED_TEX_BRACE_COMMANDS`
245
+
246
+ Escaped version of TEX_BRACE_COMMANDS for regex use.
247
+
248
+ ## Types
249
+
250
+ All TypeScript types are exported:
251
+
252
+ ```typescript
253
+ import type {
254
+ // Node types
255
+ CodeBlockNode,
256
+ GetMarkdownOptions,
257
+ HeadingNode,
258
+ ListItemNode,
259
+ ListNode,
260
+ MathOptions,
261
+ ParagraphNode,
262
+ ParsedNode,
263
+ ParseOptions,
264
+ // ... and more
265
+ } from 'stream-markdown-parser'
266
+ ```
267
+
268
+ ### Node Types
269
+
270
+ The parser exports various node types representing different markdown elements:
271
+
272
+ - `TextNode`, `HeadingNode`, `ParagraphNode`
273
+ - `ListNode`, `ListItemNode`
274
+ - `CodeBlockNode`, `InlineCodeNode`
275
+ - `LinkNode`, `ImageNode`
276
+ - `BlockquoteNode`, `TableNode`
277
+ - `MathBlockNode`, `MathInlineNode`
278
+ - And many more...
279
+
280
+ ## Default Plugins
281
+
282
+ This package comes with the following markdown-it plugins pre-configured:
283
+
284
+ - `markdown-it-sub` - Subscript support (`H~2~O`)
285
+ - `markdown-it-sup` - Superscript support (`x^2^`)
286
+ - `markdown-it-mark` - Highlight/mark support (`==highlighted==`)
287
+ - `markdown-it-emoji` - Emoji support (`:smile:` → 😄)
288
+ - `markdown-it-task-checkbox` - Task list support (`- [ ] Todo`)
289
+ - `markdown-it-ins` - Insert tag support (`++inserted++`)
290
+ - `markdown-it-footnote` - Footnote support
291
+ - `markdown-it-container` - Custom container support (`::: warning`, `::: tip`, etc.)
292
+ - Math support - LaTeX math rendering with `$...$` and `$$...$$`
293
+
294
+ ## Framework Integration
295
+
296
+ While this package is framework-agnostic, it's designed to work seamlessly with:
297
+
298
+ - ✅ **Node.js** - Server-side rendering
299
+ - ✅ **Vue 3** - Use with `stream-markdown-parser`
300
+ - ✅ **React** - Use parsed nodes for custom rendering
301
+ - ✅ **Vanilla JS** - Direct HTML rendering
302
+ - ✅ **Any framework** - Parse to AST and render as needed
303
+
304
+ ## Migration from `stream-markdown-parser`
305
+
306
+ If you're migrating from using the markdown utils in `stream-markdown-parser`:
307
+
308
+ ```typescript
309
+ import { getMarkdown } from 'stream-markdown-parser'
310
+ ```
311
+
312
+ All APIs remain the same. See [migration guide](../../docs/monorepo-migration.md) for details.
313
+
314
+ ## Performance
315
+
316
+ - **Lightweight**: ~65KB minified (13KB gzipped)
317
+ - **Fast**: Optimized for real-time parsing
318
+ - **Tree-shakeable**: Only import what you need
319
+ - **Zero dependencies**: Except for markdown-it and its plugins
320
+
321
+ ## Contributing
322
+
323
+ Issues and PRs welcome! Please read the [contribution guidelines](../../AGENTS.md).
324
+
95
325
  ## License
96
326
 
97
- MIT
327
+ MIT © Simon He
328
+
329
+ ## Related
330
+
331
+ - [stream-markdown-parser](https://github.com/Simon-He95/vue-markdown-render) - Full-featured Vue 3 Markdown renderer