pd-markdown 2.0.3 → 2.0.4-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.
Files changed (65) hide show
  1. package/package.json +7 -2
  2. package/packages/parser/dist/index.cjs +1 -1
  3. package/packages/parser/dist/index.mjs +4 -184
  4. package/packages/parser/dist/index.mjs.map +1 -1
  5. package/packages/parser/dist/plugins/transform/heading.mjs +39 -0
  6. package/packages/parser/dist/plugins/transform/heading.mjs.map +1 -0
  7. package/packages/parser/dist/plugins/transform/list.mjs +21 -0
  8. package/packages/parser/dist/plugins/transform/list.mjs.map +1 -0
  9. package/packages/parser/dist/plugins/transform/table.mjs +40 -0
  10. package/packages/parser/dist/plugins/transform/table.mjs.map +1 -0
  11. package/packages/parser/dist/processor.mjs +100 -0
  12. package/packages/parser/dist/processor.mjs.map +1 -0
  13. package/packages/web/dist/MarkdownRenderer-CflS5zy_.js +43 -0
  14. package/packages/web/dist/MarkdownRenderer-CflS5zy_.js.map +1 -0
  15. package/packages/web/dist/NodeRenderer-DTR-8m1G.js +225 -0
  16. package/packages/web/dist/NodeRenderer-DTR-8m1G.js.map +1 -0
  17. package/packages/web/dist/client.cjs +16 -0
  18. package/packages/web/dist/client.cjs.map +1 -0
  19. package/packages/web/dist/client.mjs +5 -0
  20. package/packages/web/dist/client.mjs.map +1 -0
  21. package/packages/web/dist/components/MarkdownRenderer.mjs +41 -0
  22. package/packages/web/dist/components/MarkdownRenderer.mjs.map +1 -0
  23. package/packages/web/dist/components/NodeRenderer.mjs +131 -0
  24. package/packages/web/dist/components/NodeRenderer.mjs.map +1 -0
  25. package/packages/web/dist/components/StreamMarkdownRenderer.mjs +145 -0
  26. package/packages/web/dist/components/StreamMarkdownRenderer.mjs.map +1 -0
  27. package/packages/web/dist/components/context.mjs +17 -0
  28. package/packages/web/dist/components/context.mjs.map +1 -0
  29. package/packages/web/dist/components/defaults/Blockquote.mjs +8 -0
  30. package/packages/web/dist/components/defaults/Blockquote.mjs.map +1 -0
  31. package/packages/web/dist/components/defaults/Code.mjs +12 -0
  32. package/packages/web/dist/components/defaults/Code.mjs.map +1 -0
  33. package/packages/web/dist/components/defaults/Heading.mjs +10 -0
  34. package/packages/web/dist/components/defaults/Heading.mjs.map +1 -0
  35. package/packages/web/dist/components/defaults/Image.mjs +8 -0
  36. package/packages/web/dist/components/defaults/Image.mjs.map +1 -0
  37. package/packages/web/dist/components/defaults/Link.mjs +10 -0
  38. package/packages/web/dist/components/defaults/Link.mjs.map +1 -0
  39. package/packages/web/dist/components/defaults/List.mjs +17 -0
  40. package/packages/web/dist/components/defaults/List.mjs.map +1 -0
  41. package/packages/web/dist/components/defaults/Paragraph.mjs +8 -0
  42. package/packages/web/dist/components/defaults/Paragraph.mjs.map +1 -0
  43. package/packages/web/dist/components/defaults/Table.mjs +21 -0
  44. package/packages/web/dist/components/defaults/Table.mjs.map +1 -0
  45. package/packages/web/dist/components/defaults/index.mjs +29 -0
  46. package/packages/web/dist/components/defaults/index.mjs.map +1 -0
  47. package/packages/web/dist/context-DR5sJXYw.js +86 -0
  48. package/packages/web/dist/context-DR5sJXYw.js.map +1 -0
  49. package/packages/web/dist/hooks/useMarkdown.mjs +31 -0
  50. package/packages/web/dist/hooks/useMarkdown.mjs.map +1 -0
  51. package/packages/web/dist/hooks/useStreamMarkdown.mjs +274 -0
  52. package/packages/web/dist/hooks/useStreamMarkdown.mjs.map +1 -0
  53. package/packages/web/dist/index.cjs +29 -712
  54. package/packages/web/dist/index.cjs.map +1 -1
  55. package/packages/web/dist/index.d.ts +28 -26
  56. package/packages/web/dist/index.mjs +15 -693
  57. package/packages/web/dist/index.mjs.map +1 -1
  58. package/packages/web/dist/server.cjs +25 -0
  59. package/packages/web/dist/server.cjs.map +1 -0
  60. package/packages/web/dist/server.mjs +12 -0
  61. package/packages/web/dist/server.mjs.map +1 -0
  62. package/packages/web/dist/useStreamMarkdown-CXM4Hrzx.js +417 -0
  63. package/packages/web/dist/useStreamMarkdown-CXM4Hrzx.js.map +1 -0
  64. package/packages/web/dist/useStreamMarkdown-DEOfH8Ve.js +459 -0
  65. package/packages/web/dist/useStreamMarkdown-DEOfH8Ve.js.map +1 -0
@@ -1,694 +1,16 @@
1
- import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
- import { createParser } from 'pd-markdown/parser';
3
- import { createContext, useContext, useRef, useState, useEffect, useMemo, useCallback } from 'react';
4
-
5
- /**
6
- * Context for passing configuration down the component tree
7
- */
8
- const MarkdownContext = createContext({
9
- components: {},
10
- });
11
- /**
12
- * Hook to access markdown context
13
- */
14
- function useMarkdownContext() {
15
- return useContext(MarkdownContext);
16
- }
17
-
18
- const Heading = ({ node, children }) => {
19
- const Tag = `h${node.depth}`;
20
- const id = node.data && 'id' in node.data ? node.data.id : undefined;
21
- return jsx(Tag, { id: id, children: children });
22
- };
23
-
24
- const Paragraph = ({ children }) => {
25
- return jsx("p", { children: children });
26
- };
27
-
28
- const List = ({ node, children }) => {
29
- const Tag = node.ordered ? 'ol' : 'ul';
30
- const start = node.ordered && node.start != null && node.start !== 1 ? node.start : undefined;
31
- return jsx(Tag, { start: start, children: children });
32
- };
33
- const ListItem = ({ node, children }) => {
34
- // Handle task list items
35
- if (typeof node.checked === 'boolean') {
36
- return (jsxs("li", { className: "task-list-item", children: [jsx("input", { type: "checkbox", checked: node.checked, readOnly: true }), jsx("span", { children: children })] }));
37
- }
38
- return jsx("li", { children: children });
39
- };
40
-
41
- const Table = ({ children }) => {
42
- return (jsx("table", { children: children }));
43
- };
44
- const TableRow = ({ children, isHeader }) => {
45
- if (isHeader) {
46
- return (jsx("thead", { children: jsx("tr", { children: children }) }));
47
- }
48
- return jsx("tr", { children: children });
49
- };
50
- const TableCell = ({ node, children }) => {
51
- const isHeader = node.data?.isHeader;
52
- const align = node.data?.align;
53
- const Tag = isHeader ? 'th' : 'td';
54
- const style = align ? { textAlign: align } : undefined;
55
- return jsx(Tag, { style: style, children: children });
56
- };
57
-
58
- const Code = ({ node }) => {
59
- const className = node.lang ? `language-${node.lang}` : undefined;
60
- return (jsx("pre", { children: jsx("code", { className: className, children: node.value }) }));
61
- };
62
- const InlineCodeComponent = ({ node }) => {
63
- return jsx("code", { children: node.value });
64
- };
65
-
66
- const Link = ({ node, children }) => {
67
- // Basic security: prevent javascript: URLs
68
- const href = node.url?.startsWith('javascript:') ? '#' : node.url;
69
- return (jsx("a", { href: href, title: node.title || undefined, children: children }));
70
- };
71
-
72
- const Image = ({ node }) => {
73
- return jsx("img", { src: node.url, alt: node.alt || '', title: node.title || undefined });
74
- };
75
-
76
- const Blockquote = ({ children }) => {
77
- return jsx("blockquote", { children: children });
78
- };
79
-
80
- /**
81
- * Default component map
82
- */
83
- const defaultComponents = {
84
- heading: Heading,
85
- paragraph: Paragraph,
86
- list: List,
87
- listItem: ListItem,
88
- table: Table,
89
- tableRow: TableRow,
90
- tableCell: TableCell,
91
- code: Code,
92
- inlineCode: InlineCodeComponent,
93
- link: Link,
94
- image: Image,
95
- blockquote: Blockquote,
96
- };
97
-
98
- /**
99
- * Recursive node renderer that renders AST nodes to React elements
100
- */
101
- const NodeRenderer = ({ node }) => {
102
- const { components } = useMarkdownContext();
103
- // Get the component for this node type
104
- const getComponent = (type) => {
105
- return components[type] || defaultComponents[type];
106
- };
107
- // Render children nodes
108
- const renderChildren = (children) => {
109
- return children.map((child, index) => (jsx(NodeRenderer, { node: child }, index)));
110
- };
111
- // Render phrasing content (inline elements)
112
- const renderPhrasingContent = (children) => {
113
- return children.map((child, index) => {
114
- switch (child.type) {
115
- case 'text':
116
- return child.value;
117
- case 'strong':
118
- return jsx("strong", { children: renderPhrasingContent(child.children) }, index);
119
- case 'emphasis':
120
- return jsx("em", { children: renderPhrasingContent(child.children) }, index);
121
- case 'delete':
122
- return jsx("del", { children: renderPhrasingContent(child.children) }, index);
123
- case 'inlineCode': {
124
- const InlineCode = getComponent('inlineCode');
125
- return InlineCode ? jsx(InlineCode, { node: child }, index) : jsx("code", { children: child.value }, index);
126
- }
127
- case 'link': {
128
- const Link = getComponent('link');
129
- return Link ? (jsx(Link, { node: child, children: renderPhrasingContent(child.children) }, index)) : (jsx("a", { href: child.url, children: renderPhrasingContent(child.children) }, index));
130
- }
131
- case 'image': {
132
- const Image = getComponent('image');
133
- return Image ? jsx(Image, { node: child }, index) : jsx("img", { src: child.url, alt: child.alt || '' }, index);
134
- }
135
- case 'break':
136
- return jsx("br", {}, index);
137
- case 'html':
138
- // For safety, render HTML as text in React
139
- return child.value;
140
- default:
141
- // For unknown inline types, try to render as text if possible
142
- if ('value' in child && typeof child.value === 'string') {
143
- return child.value;
144
- }
145
- if ('children' in child) {
146
- return renderPhrasingContent(child.children);
147
- }
148
- return null;
149
- }
150
- });
151
- };
152
- // Handle different node types
153
- switch (node.type) {
154
- case 'root':
155
- return jsx(Fragment, { children: renderChildren(node.children) });
156
- case 'heading': {
157
- const Heading = getComponent('heading');
158
- return Heading ? (jsx(Heading, { node: node, children: renderPhrasingContent(node.children) })) : null;
159
- }
160
- case 'paragraph': {
161
- const Paragraph = getComponent('paragraph');
162
- return Paragraph ? (jsx(Paragraph, { node: node, children: renderPhrasingContent(node.children) })) : null;
163
- }
164
- case 'list': {
165
- const List = getComponent('list');
166
- return List ? (jsx(List, { node: node, children: renderChildren(node.children) })) : null;
167
- }
168
- case 'listItem': {
169
- const ListItem = getComponent('listItem');
170
- const children = node.children.map((child, index) => {
171
- // Unwrap single paragraph in list item
172
- if (child.type === 'paragraph' && node.children.length === 1) {
173
- return renderPhrasingContent(child.children);
174
- }
175
- return jsx(NodeRenderer, { node: child }, index);
176
- });
177
- return ListItem ? jsx(ListItem, { node: node, children: children }) : null;
178
- }
179
- case 'table': {
180
- const Table = getComponent('table');
181
- const TableRow = getComponent('tableRow');
182
- if (!Table || !TableRow)
183
- return null;
184
- const [headerRow, ...bodyRows] = node.children;
185
- return (jsxs(Table, { node: node, children: [headerRow && (jsx(TableRow, { node: headerRow, isHeader: true, children: headerRow.children.map((cell, index) => (jsx(NodeRenderer, { node: cell }, index))) })), bodyRows.length > 0 && (jsx("tbody", { children: bodyRows.map((row, rowIndex) => (jsx(TableRow, { node: row, children: row.children.map((cell, cellIndex) => (jsx(NodeRenderer, { node: cell }, cellIndex))) }, rowIndex))) }))] }));
186
- }
187
- case 'tableCell': {
188
- const TableCell = getComponent('tableCell');
189
- return TableCell ? (jsx(TableCell, { node: node, children: renderPhrasingContent(node.children) })) : null;
190
- }
191
- case 'code': {
192
- const Code = getComponent('code');
193
- return Code ? jsx(Code, { node: node }) : null;
194
- }
195
- case 'blockquote': {
196
- const Blockquote = getComponent('blockquote');
197
- return Blockquote ? (jsx(Blockquote, { node: node, children: renderChildren(node.children) })) : null;
198
- }
199
- case 'thematicBreak':
200
- return jsx("hr", {});
201
- case 'html':
202
- // For safety, don't render raw HTML by default
203
- return null;
204
- case 'yaml':
205
- // Frontmatter shouldn't be rendered
206
- return null;
207
- default: {
208
- // Try to find a custom component for unknown types
209
- const CustomComponent = getComponent(node.type);
210
- if (CustomComponent) {
211
- const children = 'children' in node
212
- ? renderChildren(node.children)
213
- : undefined;
214
- return jsx(CustomComponent, { node: node, children: children });
215
- }
216
- // Fallback: try to render children if available
217
- if ('children' in node) {
218
- return jsx(Fragment, { children: renderChildren(node.children) });
219
- }
220
- return null;
221
- }
222
- }
223
- };
224
-
225
- // Singleton parser for client-side use
226
- let defaultParser = null;
227
- function getParser$2(options) {
228
- if (options) {
229
- return createParser(options);
230
- }
231
- if (!defaultParser) {
232
- defaultParser = createParser();
233
- }
234
- return defaultParser;
235
- }
236
- /**
237
- * Main markdown renderer component
238
- *
239
- * Supports both client-side and server-side rendering:
240
- * - Pass `source` for automatic parsing
241
- * - Pass `ast` for pre-parsed content (SSR optimization)
242
- */
243
- const MarkdownRenderer = ({ source, ast, components = {}, className, style, parserOptions, }) => {
244
- // Use provided AST or parse source
245
- let tree;
246
- if (ast) {
247
- tree = ast;
248
- }
249
- else if (source) {
250
- const parser = getParser$2(parserOptions);
251
- tree = parser.parse(source);
252
- }
253
- else {
254
- // No content provided
255
- return null;
256
- }
257
- return (jsx(MarkdownContext.Provider, { value: { components }, children: jsx("div", { className: className, style: style, children: jsx(NodeRenderer, { node: tree }) }) }));
258
- };
259
-
260
- // ─── Styles ───────────────────────────────────────────────────────────
261
- const cursorKeyframes = `
262
- @keyframes pd-md-cursor-blink {
263
- 0%, 100% { opacity: 1; }
264
- 50% { opacity: 0; }
265
- }
266
-
267
- @keyframes pd-md-fade-in {
268
- from { opacity: 0; transform: translateY(4px); }
269
- to { opacity: 1; transform: translateY(0); }
270
- }
271
- `;
272
- const cursorStyle = {
273
- display: 'inline-block',
274
- width: '2px',
275
- height: '1.1em',
276
- backgroundColor: 'currentColor',
277
- marginLeft: '2px',
278
- verticalAlign: 'text-bottom',
279
- animation: 'pd-md-cursor-blink 1s step-end infinite',
280
- };
281
- // ─── Internal Parsed AST Hook ─────────────────────────────────────────
282
- function useParsedAst(source, options) {
283
- const parserRef = useRef(null);
284
- const optionsRef = useRef(options);
285
- if (!parserRef.current ||
286
- JSON.stringify(optionsRef.current) !== JSON.stringify(options)) {
287
- parserRef.current = createParser(options);
288
- optionsRef.current = options;
289
- }
290
- return useMemo(() => {
291
- try {
292
- return parserRef.current.parse(source);
293
- }
294
- catch {
295
- return { type: 'root', children: [] };
296
- }
297
- }, [source]);
298
- }
299
- // ─── Cursor Component ─────────────────────────────────────────────────
300
- const StreamCursor = ({ customElement }) => {
301
- if (customElement) {
302
- return jsx(Fragment, { children: customElement });
303
- }
304
- return jsx("span", { style: cursorStyle, "aria-hidden": "true", "data-streaming-cursor": true });
305
- };
306
- // ─── Main Component ──────────────────────────────────────────────────
307
- /**
308
- * StreamMarkdownRenderer — A streaming-aware markdown renderer.
309
- *
310
- * This component is designed for rendering AI-generated streaming markdown.
311
- * It shows a blinking cursor at the end while content is being streamed,
312
- * and optionally animates new content blocks as they appear.
313
- *
314
- * Usage patterns:
315
- *
316
- * 1. **With `useStreamMarkdown` hook** (recommended):
317
- * ```tsx
318
- * const stream = useStreamMarkdown()
319
- * // ... consume stream
320
- * <StreamMarkdownRenderer
321
- * source={stream.source}
322
- * ast={stream.ast}
323
- * isStreaming={stream.isStreaming}
324
- * />
325
- * ```
326
- *
327
- * 2. **Standalone** (pass source, let it parse internally):
328
- * ```tsx
329
- * <StreamMarkdownRenderer
330
- * source={accumulatedText}
331
- * isStreaming={isLoading}
332
- * />
333
- * ```
334
- */
335
- const StreamMarkdownRenderer = ({ source, isStreaming = false, ast: externalAst, components = {}, className, style, parserOptions, showCursor = true, cursorElement, animationClassName, enableAnimation = true, }) => {
336
- // Use external AST if provided, otherwise parse internally
337
- const internalAst = useParsedAst(externalAst ? '' : source, parserOptions);
338
- const ast = externalAst || internalAst;
339
- // Track previous child count for animation
340
- const prevChildCountRef = useRef(0);
341
- const [animatingIndices, setAnimatingIndices] = useState(new Set());
342
- // Detect new blocks for animation
343
- useEffect(() => {
344
- const currentCount = ast.children.length;
345
- const prevCount = prevChildCountRef.current;
346
- if (enableAnimation &&
347
- isStreaming &&
348
- currentCount > prevCount &&
349
- prevCount > 0) {
350
- const newIndices = new Set();
351
- for (let i = prevCount; i < currentCount; i++) {
352
- newIndices.add(i);
353
- }
354
- setAnimatingIndices(newIndices);
355
- // Clear animation flags after animation completes
356
- const timer = setTimeout(() => {
357
- setAnimatingIndices(new Set());
358
- }, 300);
359
- prevChildCountRef.current = currentCount;
360
- return () => clearTimeout(timer);
361
- }
362
- prevChildCountRef.current = currentCount;
363
- }, [ast.children.length, enableAnimation, isStreaming]);
364
- // Auto-scroll ref
365
- const containerRef = useRef(null);
366
- // Inject keyframe styles
367
- useEffect(() => {
368
- const styleId = 'pd-md-stream-styles';
369
- if (!document.getElementById(styleId)) {
370
- const styleEl = document.createElement('style');
371
- styleEl.id = styleId;
372
- styleEl.textContent = cursorKeyframes;
373
- document.head.appendChild(styleEl);
374
- }
375
- }, []);
376
- if (!source && !externalAst) {
377
- return null;
378
- }
379
- const wrapperStyle = {
380
- ...style,
381
- position: 'relative',
382
- };
383
- return (jsx(MarkdownContext.Provider, { value: { components }, children: jsxs("div", { ref: containerRef, className: className, style: wrapperStyle, children: [ast.children.map((child, index) => {
384
- const isNewBlock = animatingIndices.has(index);
385
- const isLastBlock = index === ast.children.length - 1;
386
- const blockStyle = enableAnimation && isNewBlock
387
- ? {
388
- animation: 'pd-md-fade-in 0.3s ease-out forwards',
389
- }
390
- : {};
391
- const blockClassName = isNewBlock
392
- ? animationClassName || undefined
393
- : undefined;
394
- return (jsxs("div", { style: blockStyle, className: blockClassName, "data-stream-block": isLastBlock && isStreaming ? 'active' : undefined, children: [jsx(NodeRenderer, { node: child }), showCursor && isStreaming && isLastBlock && (jsx(StreamCursor, { customElement: cursorElement }))] }, index));
395
- }), showCursor && isStreaming && ast.children.length === 0 && (jsx(StreamCursor, { customElement: cursorElement }))] }) }));
396
- };
397
-
398
- // Cached parser instance
399
- let cachedParser$1 = null;
400
- function getParser$1(options) {
401
- if (options) {
402
- return createParser(options);
403
- }
404
- if (!cachedParser$1) {
405
- cachedParser$1 = createParser();
406
- }
407
- return cachedParser$1;
408
- }
409
- /**
410
- * Hook for parsing markdown on the client side
411
- *
412
- * @param source - Markdown source string
413
- * @param options - Parser options
414
- * @returns Parsed AST
415
- */
416
- function useMarkdown(source, options) {
417
- const ast = useMemo(() => {
418
- const parser = getParser$1(options);
419
- return parser.parse(source);
420
- }, [source, options]);
421
- return ast;
422
- }
423
-
424
- // Singleton parser cache
425
- let cachedParser = null;
426
- let cachedParserOptions;
427
- function getParser(options) {
428
- if (options !== cachedParserOptions ||
429
- (options && JSON.stringify(options) !== JSON.stringify(cachedParserOptions))) {
430
- cachedParser = createParser(options);
431
- cachedParserOptions = options;
432
- }
433
- if (!cachedParser) {
434
- cachedParser = createParser(options);
435
- cachedParserOptions = options;
436
- }
437
- return cachedParser;
438
- }
439
- const EMPTY_AST = { type: 'root', children: [] };
440
- /**
441
- * Hook for streaming markdown rendering.
442
- *
443
- * Provides a simple API to progressively append markdown text,
444
- * automatically re-parsing into an AST that can be rendered
445
- * by the MarkdownRenderer.
446
- *
447
- * Supports multiple consumption methods:
448
- * - Manual `append()` + `done()` calls
449
- * - `consume(readableStream)` for ReadableStream<string>
450
- * - `consumeIterator(asyncIterable)` for async iterators
451
- * - `consumeResponse(response)` for SSE/fetch responses
452
- */
453
- function useStreamMarkdown(options = {}) {
454
- const { parserOptions, onStart, onChunk, onDone, onError, parseDebounceMs = 50, } = options;
455
- const [state, setState] = useState({
456
- source: '',
457
- ast: EMPTY_AST,
458
- isStreaming: false,
459
- isDone: false,
460
- error: null,
461
- });
462
- const sourceRef = useRef('');
463
- const isStreamingRef = useRef(false);
464
- const parseTimerRef = useRef(null);
465
- const abortControllerRef = useRef(null);
466
- // Parse the current source and update AST
467
- const parseSource = useCallback((source) => {
468
- try {
469
- const parser = getParser(parserOptions);
470
- const ast = parser.parse(source);
471
- setState((prev) => ({ ...prev, source, ast }));
472
- }
473
- catch {
474
- // If parsing fails (e.g., incomplete markdown), keep previous AST
475
- setState((prev) => ({ ...prev, source }));
476
- }
477
- }, [parserOptions]);
478
- // Debounced parse
479
- const debouncedParse = useCallback((source) => {
480
- if (parseTimerRef.current) {
481
- clearTimeout(parseTimerRef.current);
482
- }
483
- parseTimerRef.current = setTimeout(() => {
484
- parseSource(source);
485
- }, parseDebounceMs);
486
- }, [parseSource, parseDebounceMs]);
487
- // Append a chunk
488
- const append = useCallback((chunk) => {
489
- if (!isStreamingRef.current) {
490
- isStreamingRef.current = true;
491
- setState((prev) => ({
492
- ...prev,
493
- isStreaming: true,
494
- isDone: false,
495
- error: null,
496
- }));
497
- onStart?.();
498
- }
499
- sourceRef.current += chunk;
500
- const currentSource = sourceRef.current;
501
- onChunk?.(chunk, currentSource);
502
- debouncedParse(currentSource);
503
- }, [debouncedParse, onStart, onChunk]);
504
- // Signal completion
505
- const done = useCallback(() => {
506
- if (parseTimerRef.current) {
507
- clearTimeout(parseTimerRef.current);
508
- }
509
- // Final parse with complete source
510
- const finalSource = sourceRef.current;
511
- parseSource(finalSource);
512
- isStreamingRef.current = false;
513
- setState((prev) => ({
514
- ...prev,
515
- isStreaming: false,
516
- isDone: true,
517
- }));
518
- onDone?.(finalSource);
519
- }, [parseSource, onDone]);
520
- // Reset state
521
- const reset = useCallback(() => {
522
- if (parseTimerRef.current) {
523
- clearTimeout(parseTimerRef.current);
524
- }
525
- if (abortControllerRef.current) {
526
- abortControllerRef.current.abort();
527
- }
528
- sourceRef.current = '';
529
- isStreamingRef.current = false;
530
- setState({
531
- source: '',
532
- ast: EMPTY_AST,
533
- isStreaming: false,
534
- isDone: false,
535
- error: null,
536
- });
537
- }, []);
538
- // Consume a ReadableStream<string>
539
- const consume = useCallback(async (stream) => {
540
- reset();
541
- const reader = stream.getReader();
542
- const controller = new AbortController();
543
- abortControllerRef.current = controller;
544
- try {
545
- while (true) {
546
- if (controller.signal.aborted)
547
- break;
548
- const { done: readerDone, value } = await reader.read();
549
- if (readerDone)
550
- break;
551
- if (value)
552
- append(value);
553
- }
554
- if (!controller.signal.aborted)
555
- done();
556
- }
557
- catch (err) {
558
- const error = err instanceof Error ? err : new Error(String(err));
559
- setState((prev) => ({ ...prev, error, isStreaming: false }));
560
- onError?.(error);
561
- }
562
- finally {
563
- reader.releaseLock();
564
- }
565
- }, [reset, append, done, onError]);
566
- // Consume an async iterator
567
- const consumeIterator = useCallback(async (iterator) => {
568
- reset();
569
- const controller = new AbortController();
570
- abortControllerRef.current = controller;
571
- try {
572
- for await (const chunk of iterator) {
573
- if (controller.signal.aborted)
574
- break;
575
- append(chunk);
576
- }
577
- if (!controller.signal.aborted)
578
- done();
579
- }
580
- catch (err) {
581
- const error = err instanceof Error ? err : new Error(String(err));
582
- setState((prev) => ({ ...prev, error, isStreaming: false }));
583
- onError?.(error);
584
- }
585
- }, [reset, append, done, onError]);
586
- // Consume a fetch Response (SSE format)
587
- const consumeResponse = useCallback(async (response, opts) => {
588
- if (!response.body) {
589
- throw new Error('Response has no body');
590
- }
591
- if (!response.ok) {
592
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
593
- }
594
- reset();
595
- const controller = new AbortController();
596
- abortControllerRef.current = controller;
597
- const reader = response.body.getReader();
598
- const decoder = new TextDecoder();
599
- const extractContent = opts?.extractContent ??
600
- ((data) => {
601
- // Default: try to extract from SSE "data: ..." lines
602
- // or OpenAI-style streaming content
603
- try {
604
- const parsed = JSON.parse(data);
605
- // OpenAI format
606
- if (parsed.choices?.[0]?.delta?.content !== undefined) {
607
- return parsed.choices[0].delta.content;
608
- }
609
- // Generic content field
610
- if (typeof parsed.content === 'string') {
611
- return parsed.content;
612
- }
613
- if (typeof parsed.text === 'string') {
614
- return parsed.text;
615
- }
616
- }
617
- catch {
618
- // Not JSON, return as-is
619
- }
620
- return data;
621
- });
622
- try {
623
- let buffer = '';
624
- while (true) {
625
- if (controller.signal.aborted)
626
- break;
627
- const { done: readerDone, value } = await reader.read();
628
- if (readerDone)
629
- break;
630
- buffer += decoder.decode(value, { stream: true });
631
- const lines = buffer.split('\n');
632
- buffer = lines.pop() || ''; // Keep incomplete line in buffer
633
- for (const line of lines) {
634
- const trimmed = line.trim();
635
- if (!trimmed)
636
- continue;
637
- if (trimmed === 'data: [DONE]')
638
- continue;
639
- let data = trimmed;
640
- if (trimmed.startsWith('data: ')) {
641
- data = trimmed.slice(6);
642
- }
643
- const content = extractContent(data);
644
- if (content !== null && content !== '') {
645
- append(content);
646
- }
647
- }
648
- }
649
- // Process remaining buffer
650
- if (buffer.trim()) {
651
- const data = buffer.trim().startsWith('data: ')
652
- ? buffer.trim().slice(6)
653
- : buffer.trim();
654
- const content = extractContent(data);
655
- if (content !== null && content !== '') {
656
- append(content);
657
- }
658
- }
659
- if (!controller.signal.aborted)
660
- done();
661
- }
662
- catch (err) {
663
- const error = err instanceof Error ? err : new Error(String(err));
664
- setState((prev) => ({ ...prev, error, isStreaming: false }));
665
- onError?.(error);
666
- }
667
- finally {
668
- reader.releaseLock();
669
- }
670
- }, [reset, append, done, onError]);
671
- // Cleanup on unmount
672
- useEffect(() => {
673
- return () => {
674
- if (parseTimerRef.current) {
675
- clearTimeout(parseTimerRef.current);
676
- }
677
- if (abortControllerRef.current) {
678
- abortControllerRef.current.abort();
679
- }
680
- };
681
- }, []);
682
- return {
683
- ...state,
684
- append,
685
- done,
686
- reset,
687
- consume,
688
- consumeIterator,
689
- consumeResponse,
690
- };
691
- }
692
-
693
- export { Blockquote, Code, Heading, Image, InlineCodeComponent, Link, List, ListItem, MarkdownContext, MarkdownRenderer, NodeRenderer, Paragraph, StreamMarkdownRenderer, Table, TableCell, TableRow, defaultComponents, useMarkdown, useMarkdownContext, useStreamMarkdown };
1
+ export { Blockquote } from './components/defaults/Blockquote.mjs';
2
+ export { Code, InlineCodeComponent } from './components/defaults/Code.mjs';
3
+ export { Heading } from './components/defaults/Heading.mjs';
4
+ export { Image } from './components/defaults/Image.mjs';
5
+ export { Link } from './components/defaults/Link.mjs';
6
+ export { List, ListItem } from './components/defaults/List.mjs';
7
+ export { MarkdownContext, useMarkdownContext } from './components/context.mjs';
8
+ export { MarkdownRenderer } from './components/MarkdownRenderer.mjs';
9
+ export { NodeRenderer } from './components/NodeRenderer.mjs';
10
+ export { Paragraph } from './components/defaults/Paragraph.mjs';
11
+ export { StreamMarkdownRenderer } from './components/StreamMarkdownRenderer.mjs';
12
+ export { Table, TableCell, TableRow } from './components/defaults/Table.mjs';
13
+ export { defaultComponents } from './components/defaults/index.mjs';
14
+ export { useMarkdown } from './hooks/useMarkdown.mjs';
15
+ export { useStreamMarkdown } from './hooks/useStreamMarkdown.mjs';
694
16
  //# sourceMappingURL=index.mjs.map