@incremark/react 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.en.md +140 -0
- package/README.md +3 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +13 -0
- package/package.json +9 -3
package/README.en.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# @incremark/react
|
|
2
|
+
|
|
3
|
+
React 18+ integration for Incremark.
|
|
4
|
+
|
|
5
|
+
**[🇨🇳 中文](./README.md)** | 🇺🇸 English
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- 📦 **Out of the Box** - Provides `useIncremark` hook and `<Incremark>` component
|
|
10
|
+
- 🎨 **Customizable** - Support for custom render components
|
|
11
|
+
- ⚡ **High Performance** - Leverages React's reconciliation mechanism
|
|
12
|
+
- 🔧 **DevTools** - Built-in developer tools
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @incremark/core @incremark/react
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```tsx
|
|
23
|
+
import { useIncremark, Incremark } from '@incremark/react'
|
|
24
|
+
|
|
25
|
+
function App() {
|
|
26
|
+
const { blocks, append, finalize, reset } = useIncremark({ gfm: true })
|
|
27
|
+
|
|
28
|
+
async function handleStream(stream: ReadableStream) {
|
|
29
|
+
reset()
|
|
30
|
+
const reader = stream.getReader()
|
|
31
|
+
const decoder = new TextDecoder()
|
|
32
|
+
|
|
33
|
+
while (true) {
|
|
34
|
+
const { done, value } = await reader.read()
|
|
35
|
+
if (done) break
|
|
36
|
+
append(decoder.decode(value))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
finalize()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<>
|
|
44
|
+
<button onClick={() => handleStream(stream)}>Start</button>
|
|
45
|
+
<Incremark blocks={blocks} />
|
|
46
|
+
</>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## API
|
|
52
|
+
|
|
53
|
+
### useIncremark(options)
|
|
54
|
+
|
|
55
|
+
Core hook.
|
|
56
|
+
|
|
57
|
+
**Returns:**
|
|
58
|
+
|
|
59
|
+
| Property | Type | Description |
|
|
60
|
+
|----------|------|-------------|
|
|
61
|
+
| `markdown` | `string` | Complete Markdown |
|
|
62
|
+
| `blocks` | `Block[]` | All blocks |
|
|
63
|
+
| `completedBlocks` | `Block[]` | Completed blocks |
|
|
64
|
+
| `pendingBlocks` | `Block[]` | Pending blocks |
|
|
65
|
+
| `append` | `Function` | Append content |
|
|
66
|
+
| `finalize` | `Function` | Complete parsing |
|
|
67
|
+
| `reset` | `Function` | Reset state |
|
|
68
|
+
| `render` | `Function` | Render once (reset + append + finalize) |
|
|
69
|
+
|
|
70
|
+
### useDevTools(incremark)
|
|
71
|
+
|
|
72
|
+
Enable DevTools.
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
const incremark = useIncremark()
|
|
76
|
+
useDevTools(incremark)
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### \<Incremark\>
|
|
80
|
+
|
|
81
|
+
Render component.
|
|
82
|
+
|
|
83
|
+
```tsx
|
|
84
|
+
<Incremark
|
|
85
|
+
blocks={blocks}
|
|
86
|
+
components={{ heading: MyHeading }}
|
|
87
|
+
/>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Custom Components
|
|
91
|
+
|
|
92
|
+
```tsx
|
|
93
|
+
import { useIncremark, Incremark } from '@incremark/react'
|
|
94
|
+
import MyCode from './MyCode'
|
|
95
|
+
|
|
96
|
+
function App() {
|
|
97
|
+
const { blocks } = useIncremark()
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<Incremark
|
|
101
|
+
blocks={blocks}
|
|
102
|
+
components={{ code: MyCode }}
|
|
103
|
+
/>
|
|
104
|
+
)
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Integration with React Query
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
import { useQuery } from '@tanstack/react-query'
|
|
112
|
+
import { useIncremark, Incremark } from '@incremark/react'
|
|
113
|
+
|
|
114
|
+
function StreamingContent() {
|
|
115
|
+
const { blocks, append, finalize, reset } = useIncremark()
|
|
116
|
+
|
|
117
|
+
const { refetch } = useQuery({
|
|
118
|
+
queryKey: ['chat'],
|
|
119
|
+
queryFn: async () => {
|
|
120
|
+
reset()
|
|
121
|
+
// ... streaming handling
|
|
122
|
+
finalize()
|
|
123
|
+
return null
|
|
124
|
+
},
|
|
125
|
+
enabled: false
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
return (
|
|
129
|
+
<>
|
|
130
|
+
<button onClick={() => refetch()}>Start</button>
|
|
131
|
+
<Incremark blocks={blocks} />
|
|
132
|
+
</>
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## License
|
|
138
|
+
|
|
139
|
+
MIT
|
|
140
|
+
|
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Incremark 的 React 18+ 集成库。
|
|
4
4
|
|
|
5
|
+
🇨🇳 中文 | **[🇺🇸 English](./README.en.md)**
|
|
6
|
+
|
|
5
7
|
## 特性
|
|
6
8
|
|
|
7
9
|
- 📦 **开箱即用** - 提供 `useIncremark` hook 和 `<Incremark>` 组件
|
|
@@ -63,6 +65,7 @@ function App() {
|
|
|
63
65
|
| `append` | `Function` | 追加内容 |
|
|
64
66
|
| `finalize` | `Function` | 完成解析 |
|
|
65
67
|
| `reset` | `Function` | 重置状态 |
|
|
68
|
+
| `render` | `Function` | 一次性渲染(reset + append + finalize) |
|
|
66
69
|
|
|
67
70
|
### useDevTools(incremark)
|
|
68
71
|
|
package/dist/index.d.ts
CHANGED
|
@@ -51,6 +51,8 @@ declare function useIncremark(options?: UseIncremarkOptions): {
|
|
|
51
51
|
abort: () => IncrementalUpdate;
|
|
52
52
|
/** 重置解析器 */
|
|
53
53
|
reset: () => void;
|
|
54
|
+
/** 一次性渲染(reset + append + finalize) */
|
|
55
|
+
render: (content: string) => IncrementalUpdate;
|
|
54
56
|
/** 解析器实例 */
|
|
55
57
|
parser: IncremarkParser;
|
|
56
58
|
};
|
package/dist/index.js
CHANGED
|
@@ -66,6 +66,17 @@ function useIncremark(options = {}) {
|
|
|
66
66
|
setMarkdown("");
|
|
67
67
|
setIsLoading(false);
|
|
68
68
|
}, [parser]);
|
|
69
|
+
const render = useCallback(
|
|
70
|
+
(content) => {
|
|
71
|
+
const update = parser.render(content);
|
|
72
|
+
setMarkdown(parser.getBuffer());
|
|
73
|
+
setCompletedBlocks(parser.getCompletedBlocks());
|
|
74
|
+
setPendingBlocks([]);
|
|
75
|
+
setIsLoading(false);
|
|
76
|
+
return update;
|
|
77
|
+
},
|
|
78
|
+
[parser]
|
|
79
|
+
);
|
|
69
80
|
return {
|
|
70
81
|
/** 已收集的完整 Markdown 字符串 */
|
|
71
82
|
markdown,
|
|
@@ -87,6 +98,8 @@ function useIncremark(options = {}) {
|
|
|
87
98
|
abort,
|
|
88
99
|
/** 重置解析器 */
|
|
89
100
|
reset,
|
|
101
|
+
/** 一次性渲染(reset + append + finalize) */
|
|
102
|
+
render,
|
|
90
103
|
/** 解析器实例 */
|
|
91
104
|
parser
|
|
92
105
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@incremark/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Incremark React integration - Incremental Markdown parser for AI streaming",
|
|
6
6
|
"type": "module",
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
],
|
|
19
19
|
"peerDependencies": {
|
|
20
20
|
"react": ">=18.0.0",
|
|
21
|
-
"@incremark/core": "0.0.
|
|
21
|
+
"@incremark/core": "0.0.3"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@incremark/devtools": "0.0.
|
|
24
|
+
"@incremark/devtools": "0.0.3"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@types/react": "^18.2.0",
|
|
@@ -29,6 +29,12 @@
|
|
|
29
29
|
"tsup": "^8.0.0",
|
|
30
30
|
"typescript": "^5.3.0"
|
|
31
31
|
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/kingshuaishuai/incremark.git",
|
|
35
|
+
"directory": "packages/react"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://incremark-docs.vercel.app/",
|
|
32
38
|
"scripts": {
|
|
33
39
|
"build": "tsup",
|
|
34
40
|
"dev": "tsup --watch"
|