@minjun0219/mdwire 0.1.9 → 0.1.11
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 +58 -35
- package/bundler/mdwire.d.ts +42 -41
- package/bundler/mdwire_bg.js +51 -55
- package/bundler/mdwire_bg.wasm +0 -0
- package/js/events.d.ts +5 -5
- package/js/events.js +3 -3
- package/js/react.d.ts +10 -13
- package/js/react.js +26 -21
- package/node/mdwire.d.ts +42 -41
- package/node/mdwire.js +51 -55
- package/node/mdwire_bg.wasm +0 -0
- package/package.json +3 -3
package/bundler/mdwire_bg.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* `renderWithReport
|
|
2
|
+
* The result of `renderWithReport`.
|
|
3
3
|
*/
|
|
4
4
|
export class Rendered {
|
|
5
5
|
static __wrap(ptr) {
|
|
@@ -19,7 +19,7 @@ export class Rendered {
|
|
|
19
19
|
wasm.__wbg_rendered_free(ptr, 0);
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* The parts. Just one if the text fit within the limit.
|
|
23
23
|
* @returns {string[]}
|
|
24
24
|
*/
|
|
25
25
|
get parts() {
|
|
@@ -36,7 +36,7 @@ export class Rendered {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
39
|
+
* What normalization repaired.
|
|
40
40
|
* @returns {Repairs}
|
|
41
41
|
*/
|
|
42
42
|
get repairs() {
|
|
@@ -47,7 +47,7 @@ export class Rendered {
|
|
|
47
47
|
if (Symbol.dispose) Rendered.prototype[Symbol.dispose] = Rendered.prototype.free;
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* Counts of what normalization repaired and what was changed to fit the channel.
|
|
51
51
|
*/
|
|
52
52
|
export class Repairs {
|
|
53
53
|
static __wrap(ptr) {
|
|
@@ -67,7 +67,7 @@ export class Repairs {
|
|
|
67
67
|
wasm.__wbg_repairs_free(ptr, 0);
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
|
-
*
|
|
70
|
+
* Emphasis left open at the end of a block, closed for you.
|
|
71
71
|
* @returns {number}
|
|
72
72
|
*/
|
|
73
73
|
get closedEmphasis() {
|
|
@@ -75,7 +75,7 @@ export class Repairs {
|
|
|
75
75
|
return ret >>> 0;
|
|
76
76
|
}
|
|
77
77
|
/**
|
|
78
|
-
*
|
|
78
|
+
* Code fences left open at the end of the document, closed for you.
|
|
79
79
|
* @returns {number}
|
|
80
80
|
*/
|
|
81
81
|
get closedFence() {
|
|
@@ -83,7 +83,7 @@ export class Repairs {
|
|
|
83
83
|
return ret >>> 0;
|
|
84
84
|
}
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
86
|
+
* Emphasis markers and `<url|text>` links rewritten in a different notation.
|
|
87
87
|
* @returns {number}
|
|
88
88
|
*/
|
|
89
89
|
get convertedMarker() {
|
|
@@ -91,7 +91,7 @@ export class Repairs {
|
|
|
91
91
|
return ret >>> 0;
|
|
92
92
|
}
|
|
93
93
|
/**
|
|
94
|
-
*
|
|
94
|
+
* Unmatched `**` markers that were dropped.
|
|
95
95
|
* @returns {number}
|
|
96
96
|
*/
|
|
97
97
|
get droppedMarker() {
|
|
@@ -99,7 +99,7 @@ export class Repairs {
|
|
|
99
99
|
return ret >>> 0;
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
102
|
+
* Characters escaped because the channel would read them as syntax (GitHub's `\~`, `\<`, `\*`).
|
|
103
103
|
* @returns {number}
|
|
104
104
|
*/
|
|
105
105
|
get escapedChar() {
|
|
@@ -107,7 +107,7 @@ export class Repairs {
|
|
|
107
107
|
return ret >>> 0;
|
|
108
108
|
}
|
|
109
109
|
/**
|
|
110
|
-
*
|
|
110
|
+
* Unmatched backtick runs, turned back into literal text.
|
|
111
111
|
* @returns {number}
|
|
112
112
|
*/
|
|
113
113
|
get revertedCodeSpan() {
|
|
@@ -115,7 +115,7 @@ export class Repairs {
|
|
|
115
115
|
return ret >>> 0;
|
|
116
116
|
}
|
|
117
117
|
/**
|
|
118
|
-
*
|
|
118
|
+
* List bullets rewritten with a different symbol.
|
|
119
119
|
* @returns {number}
|
|
120
120
|
*/
|
|
121
121
|
get rewrittenBullet() {
|
|
@@ -123,7 +123,7 @@ export class Repairs {
|
|
|
123
123
|
return ret >>> 0;
|
|
124
124
|
}
|
|
125
125
|
/**
|
|
126
|
-
*
|
|
126
|
+
* Tables rewritten in a different shape from the source.
|
|
127
127
|
* @returns {number}
|
|
128
128
|
*/
|
|
129
129
|
get rewrittenTable() {
|
|
@@ -131,7 +131,7 @@ export class Repairs {
|
|
|
131
131
|
return ret >>> 0;
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
|
-
*
|
|
134
|
+
* Raw HTML stripped from the source (tags, comments, and `<br>` turned into line breaks).
|
|
135
135
|
* @returns {number}
|
|
136
136
|
*/
|
|
137
137
|
get strippedHtml() {
|
|
@@ -139,7 +139,7 @@ export class Repairs {
|
|
|
139
139
|
return ret >>> 0;
|
|
140
140
|
}
|
|
141
141
|
/**
|
|
142
|
-
*
|
|
142
|
+
* Emphasis emitted differently because the channel would not read the marker in that position (GitHub's `<strong>`, Slack's U+2060).
|
|
143
143
|
* @returns {number}
|
|
144
144
|
*/
|
|
145
145
|
get tagEmphasis() {
|
|
@@ -147,70 +147,70 @@ export class Repairs {
|
|
|
147
147
|
return ret >>> 0;
|
|
148
148
|
}
|
|
149
149
|
/**
|
|
150
|
-
*
|
|
150
|
+
* Emphasis left open at the end of a block, closed for you.
|
|
151
151
|
* @param {number} arg0
|
|
152
152
|
*/
|
|
153
153
|
set closedEmphasis(arg0) {
|
|
154
154
|
wasm.__wbg_set_repairs_closedEmphasis(this.__wbg_ptr, arg0);
|
|
155
155
|
}
|
|
156
156
|
/**
|
|
157
|
-
*
|
|
157
|
+
* Code fences left open at the end of the document, closed for you.
|
|
158
158
|
* @param {number} arg0
|
|
159
159
|
*/
|
|
160
160
|
set closedFence(arg0) {
|
|
161
161
|
wasm.__wbg_set_repairs_closedFence(this.__wbg_ptr, arg0);
|
|
162
162
|
}
|
|
163
163
|
/**
|
|
164
|
-
*
|
|
164
|
+
* Emphasis markers and `<url|text>` links rewritten in a different notation.
|
|
165
165
|
* @param {number} arg0
|
|
166
166
|
*/
|
|
167
167
|
set convertedMarker(arg0) {
|
|
168
168
|
wasm.__wbg_set_repairs_convertedMarker(this.__wbg_ptr, arg0);
|
|
169
169
|
}
|
|
170
170
|
/**
|
|
171
|
-
*
|
|
171
|
+
* Unmatched `**` markers that were dropped.
|
|
172
172
|
* @param {number} arg0
|
|
173
173
|
*/
|
|
174
174
|
set droppedMarker(arg0) {
|
|
175
175
|
wasm.__wbg_set_repairs_droppedMarker(this.__wbg_ptr, arg0);
|
|
176
176
|
}
|
|
177
177
|
/**
|
|
178
|
-
*
|
|
178
|
+
* Characters escaped because the channel would read them as syntax (GitHub's `\~`, `\<`, `\*`).
|
|
179
179
|
* @param {number} arg0
|
|
180
180
|
*/
|
|
181
181
|
set escapedChar(arg0) {
|
|
182
182
|
wasm.__wbg_set_repairs_escapedChar(this.__wbg_ptr, arg0);
|
|
183
183
|
}
|
|
184
184
|
/**
|
|
185
|
-
*
|
|
185
|
+
* Unmatched backtick runs, turned back into literal text.
|
|
186
186
|
* @param {number} arg0
|
|
187
187
|
*/
|
|
188
188
|
set revertedCodeSpan(arg0) {
|
|
189
189
|
wasm.__wbg_set_repairs_revertedCodeSpan(this.__wbg_ptr, arg0);
|
|
190
190
|
}
|
|
191
191
|
/**
|
|
192
|
-
*
|
|
192
|
+
* List bullets rewritten with a different symbol.
|
|
193
193
|
* @param {number} arg0
|
|
194
194
|
*/
|
|
195
195
|
set rewrittenBullet(arg0) {
|
|
196
196
|
wasm.__wbg_set_repairs_rewrittenBullet(this.__wbg_ptr, arg0);
|
|
197
197
|
}
|
|
198
198
|
/**
|
|
199
|
-
*
|
|
199
|
+
* Tables rewritten in a different shape from the source.
|
|
200
200
|
* @param {number} arg0
|
|
201
201
|
*/
|
|
202
202
|
set rewrittenTable(arg0) {
|
|
203
203
|
wasm.__wbg_set_repairs_rewrittenTable(this.__wbg_ptr, arg0);
|
|
204
204
|
}
|
|
205
205
|
/**
|
|
206
|
-
*
|
|
206
|
+
* Raw HTML stripped from the source (tags, comments, and `<br>` turned into line breaks).
|
|
207
207
|
* @param {number} arg0
|
|
208
208
|
*/
|
|
209
209
|
set strippedHtml(arg0) {
|
|
210
210
|
wasm.__wbg_set_repairs_strippedHtml(this.__wbg_ptr, arg0);
|
|
211
211
|
}
|
|
212
212
|
/**
|
|
213
|
-
*
|
|
213
|
+
* Emphasis emitted differently because the channel would not read the marker in that position (GitHub's `<strong>`, Slack's U+2060).
|
|
214
214
|
* @param {number} arg0
|
|
215
215
|
*/
|
|
216
216
|
set tagEmphasis(arg0) {
|
|
@@ -220,10 +220,11 @@ export class Repairs {
|
|
|
220
220
|
if (Symbol.dispose) Repairs.prototype[Symbol.dispose] = Repairs.prototype.free;
|
|
221
221
|
|
|
222
222
|
/**
|
|
223
|
-
*
|
|
223
|
+
* Streaming converter.
|
|
224
224
|
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
225
|
+
* Push a chunk and it returns only what is safe to emit now. Markup cut off at the
|
|
226
|
+
* chunk boundary stays inside. This is what you want when appending tokens to the
|
|
227
|
+
* screen as they arrive.
|
|
227
228
|
*/
|
|
228
229
|
export class Streamer {
|
|
229
230
|
__destroy_into_raw() {
|
|
@@ -237,14 +238,15 @@ export class Streamer {
|
|
|
237
238
|
wasm.__wbg_streamer_free(ptr, 0);
|
|
238
239
|
}
|
|
239
240
|
/**
|
|
240
|
-
*
|
|
241
|
+
* **To send what you have received so far as is, append this after it.**
|
|
241
242
|
*
|
|
242
|
-
*
|
|
243
|
-
*
|
|
243
|
+
* It does not touch the state, so streaming continues afterwards. Do not add it
|
|
244
|
+
* to the accumulated output itself; append it only right before sending. Use it
|
|
245
|
+
* when editing a message as tokens arrive.
|
|
244
246
|
*
|
|
245
247
|
* ```js
|
|
246
248
|
* acc += s.push(chunk);
|
|
247
|
-
* await edit(acc + s.closeOpen()); //
|
|
249
|
+
* await edit(acc + s.closeOpen()); // leave acc unchanged
|
|
248
250
|
* ```
|
|
249
251
|
* @returns {string}
|
|
250
252
|
*/
|
|
@@ -265,7 +267,7 @@ export class Streamer {
|
|
|
265
267
|
}
|
|
266
268
|
}
|
|
267
269
|
/**
|
|
268
|
-
*
|
|
270
|
+
* Input has ended. Emits what is left and closes open markup.
|
|
269
271
|
* @returns {string}
|
|
270
272
|
*/
|
|
271
273
|
finish() {
|
|
@@ -308,15 +310,16 @@ export class Streamer {
|
|
|
308
310
|
}
|
|
309
311
|
}
|
|
310
312
|
/**
|
|
311
|
-
*
|
|
312
|
-
*
|
|
313
|
-
*
|
|
313
|
+
* **The tail that would follow the final output if input ended now.** It goes in the
|
|
314
|
+
* same place as `closeOpen`, but also draws what is being held back (open emphasis,
|
|
315
|
+
* table rows, code spans). This is the default when you redraw the whole message.
|
|
316
|
+
* It is a guess, so later chunks may change the shape. Check `revised()` after the end.
|
|
314
317
|
*
|
|
315
318
|
* ```js
|
|
316
319
|
* acc += s.push(chunk);
|
|
317
|
-
* await edit(acc + s.preview()); //
|
|
320
|
+
* await edit(acc + s.preview()); // call only when drawing (costs as much as the open block)
|
|
318
321
|
* acc += s.finish();
|
|
319
|
-
* if (s.revised()) await edit(acc); //
|
|
322
|
+
* if (s.revised()) await edit(acc); // skip if the last screen already is the final output
|
|
320
323
|
* ```
|
|
321
324
|
* @returns {string}
|
|
322
325
|
*/
|
|
@@ -359,7 +362,7 @@ export class Streamer {
|
|
|
359
362
|
}
|
|
360
363
|
}
|
|
361
364
|
/**
|
|
362
|
-
*
|
|
365
|
+
* What normalization has repaired so far. After `finish`, this covers the whole document.
|
|
363
366
|
* @returns {Repairs}
|
|
364
367
|
*/
|
|
365
368
|
repairs() {
|
|
@@ -367,7 +370,7 @@ export class Streamer {
|
|
|
367
370
|
return Repairs.__wrap(ret);
|
|
368
371
|
}
|
|
369
372
|
/**
|
|
370
|
-
*
|
|
373
|
+
* Whether the final output differs from the last `preview()`. Check it after `finish`.
|
|
371
374
|
* @returns {boolean}
|
|
372
375
|
*/
|
|
373
376
|
revised() {
|
|
@@ -378,7 +381,7 @@ export class Streamer {
|
|
|
378
381
|
if (Symbol.dispose) Streamer.prototype[Symbol.dispose] = Streamer.prototype.free;
|
|
379
382
|
|
|
380
383
|
/**
|
|
381
|
-
*
|
|
384
|
+
* The channel's length limit (in characters). Exposed for callers that handle parts themselves.
|
|
382
385
|
* @param {string} channel
|
|
383
386
|
* @returns {number}
|
|
384
387
|
*/
|
|
@@ -401,7 +404,7 @@ export function limit(channel) {
|
|
|
401
404
|
}
|
|
402
405
|
|
|
403
406
|
/**
|
|
404
|
-
*
|
|
407
|
+
* Converts a complete document. Returns an array of parts; it has more than one when the text exceeds the limit.
|
|
405
408
|
* @param {string} input
|
|
406
409
|
* @param {string} channel
|
|
407
410
|
* @param {RenderOptions | null} [options]
|
|
@@ -431,8 +434,8 @@ export function render(input, channel, options) {
|
|
|
431
434
|
}
|
|
432
435
|
|
|
433
436
|
/**
|
|
434
|
-
* [`render`]
|
|
435
|
-
*
|
|
437
|
+
* Like [`render`], but also returns **what normalization repaired**. Use it to log
|
|
438
|
+
* how often the model breaks formatting.
|
|
436
439
|
* @param {string} input
|
|
437
440
|
* @param {string} channel
|
|
438
441
|
* @param {RenderOptions | null} [options]
|
|
@@ -472,37 +475,30 @@ export function __wbg___wbindgen_string_get_92ab86bb19cbc12f(arg0, arg1) {
|
|
|
472
475
|
export function __wbg___wbindgen_throw_5d9e815e6fdf150f(arg0, arg1) {
|
|
473
476
|
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
474
477
|
}
|
|
475
|
-
export function
|
|
476
|
-
const ret = getObject(arg1).from;
|
|
477
|
-
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
478
|
-
var len1 = WASM_VECTOR_LEN;
|
|
479
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
480
|
-
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
481
|
-
}
|
|
482
|
-
export function __wbg_html_a628fe022a5daeaf(arg0) {
|
|
478
|
+
export function __wbg_html_89b49615556b2850(arg0) {
|
|
483
479
|
const ret = getObject(arg0).html;
|
|
484
480
|
return isLikeNone(ret) ? 0 : addHeapObject(ret);
|
|
485
481
|
}
|
|
486
|
-
export function
|
|
482
|
+
export function __wbg_images_fc88426d1f55bdfc(arg0, arg1) {
|
|
487
483
|
const ret = getObject(arg1).images;
|
|
488
484
|
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
489
485
|
var len1 = WASM_VECTOR_LEN;
|
|
490
486
|
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
491
487
|
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
492
488
|
}
|
|
493
|
-
export function
|
|
489
|
+
export function __wbg_limit_d1453e73fbf788db(arg0, arg1) {
|
|
494
490
|
const ret = getObject(arg1).limit;
|
|
495
491
|
getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
|
|
496
492
|
getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
|
|
497
493
|
}
|
|
498
|
-
export function
|
|
494
|
+
export function __wbg_lineBreaks_8b98689e9716a2c8(arg0, arg1) {
|
|
499
495
|
const ret = getObject(arg1).lineBreaks;
|
|
500
496
|
var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
|
|
501
497
|
var len1 = WASM_VECTOR_LEN;
|
|
502
498
|
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
503
499
|
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
504
500
|
}
|
|
505
|
-
export function
|
|
501
|
+
export function __wbg_schemes_8e39175723a07726(arg0, arg1) {
|
|
506
502
|
const ret = getObject(arg1).schemes;
|
|
507
503
|
var ptr1 = isLikeNone(ret) ? 0 : passArrayJsValueToWasm0(ret, wasm.__wbindgen_export);
|
|
508
504
|
var len1 = WASM_VECTOR_LEN;
|
package/bundler/mdwire_bg.wasm
CHANGED
|
Binary file
|
package/js/events.d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
/** mdwire html
|
|
1
|
+
/** Tags that appear in mdwire's html output. */
|
|
2
2
|
export type MdTag =
|
|
3
3
|
| "p" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "ul" | "ol" | "li" | "blockquote" | "pre" | "code"
|
|
4
4
|
| "strong" | "em" | "del" | "a" | "table" | "thead" | "tbody" | "tr" | "th" | "td" | "hr" | "br"
|
|
5
5
|
| "sub" | "sup" | "b" | "i" | "u" | "s" | "strike" | "span" | "small" | "mark" | "kbd" | "ins" | "img";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
* `
|
|
8
|
+
* A single event. Attributes hold only what the core emits:
|
|
9
|
+
* `href` on `a`, `class` on `code` (`language-…`), `style` on `th` and `td` (`text-align:…`), `start` on `ol`.
|
|
10
10
|
*/
|
|
11
11
|
export type MdEvent =
|
|
12
12
|
| { type: "open"; tag: MdTag; attrs: Record<string, string> }
|
|
13
13
|
| { type: "close"; tag: MdTag }
|
|
14
14
|
| { type: "void"; tag: "br" | "hr" }
|
|
15
|
-
/**
|
|
15
|
+
/** Images appear only with `html.images: "load"`. Holds `src` and `alt`. */
|
|
16
16
|
| { type: "void"; tag: "img"; attrs: Record<string, string> }
|
|
17
17
|
| { type: "text"; text: string };
|
|
18
18
|
|
|
19
|
-
/** html
|
|
19
|
+
/** Parses html channel output (or the accumulated stream output + `closeOpen()`) into an array of events. */
|
|
20
20
|
export function toEvents(html: string): MdEvent[];
|
package/js/events.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// mdwire 의 html 채널 출력을 이벤트 열로 푼다.
|
|
2
2
|
//
|
|
3
|
-
// **mdwire 가 낸 HTML 만 읽는다.** 코어가 글자를 전부
|
|
3
|
+
// **mdwire 가 낸 HTML 만 읽는다.** 코어가 글자를 전부 이스케이프하고 태그를 고정된 집합으로만
|
|
4
4
|
// 내므로(SPEC 4절) 여기서 필요한 것은 작은 토크나이저 하나다. 남이 쓴 HTML 을 넣으면 모르는
|
|
5
5
|
// 태그는 글자로 돌려준다 — 이 모듈은 새니타이저가 아니다. 안전 판단은 코어 한 곳에 있다.
|
|
6
6
|
|
|
@@ -18,8 +18,8 @@ const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"' };
|
|
|
18
18
|
const decode = (s) => s.replace(/&(amp|lt|gt|quot);/g, (_, n) => ENTITIES[n]);
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* html
|
|
22
|
-
* @param {string} html `render(text, "html")
|
|
21
|
+
* Converts html channel output into an array of events.
|
|
22
|
+
* @param {string} html The result of `render(text, "html")`, or the accumulated stream output + `closeOpen()`.
|
|
23
23
|
* @returns {import("./events.d.ts").MdEvent[]}
|
|
24
24
|
*/
|
|
25
25
|
export function toEvents(html) {
|
package/js/react.d.ts
CHANGED
|
@@ -2,35 +2,32 @@ import type { ElementType, ReactElement, ReactNode } from "react";
|
|
|
2
2
|
import type { MdTag } from "./events.js";
|
|
3
3
|
import type { RenderOptions } from "@minjun0219/mdwire";
|
|
4
4
|
|
|
5
|
-
/**
|
|
5
|
+
/** Components that replace individual tags, e.g. `{ a: MyLink, code: CodeBlock }`. Each receives that tag's props. */
|
|
6
6
|
export type MdComponents = Partial<Record<MdTag, ElementType>>;
|
|
7
7
|
|
|
8
8
|
export interface MarkdownProps {
|
|
9
|
-
/**
|
|
9
|
+
/** Markdown written by the agent (complete text). For streaming, use `Streamer` + `toElements`; passing the accumulated output each time reconverts it from scratch. */
|
|
10
10
|
text: string;
|
|
11
|
-
/** 입력 표기. 슬랙 레거시 mrkdwn 이면 "slack-mrkdwn". */
|
|
12
|
-
from?: "markdown" | "slack-mrkdwn";
|
|
13
11
|
components?: MdComponents;
|
|
14
|
-
/**
|
|
12
|
+
/** Options passed to the core. The `html` policy (line breaks, images, schemes) goes here. */
|
|
15
13
|
options?: RenderOptions;
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
/**
|
|
16
|
+
/** Renders markdown as React elements. Does not use innerHTML. */
|
|
19
17
|
export function Markdown(props: MarkdownProps): ReactElement;
|
|
20
18
|
|
|
21
19
|
export interface MarkdownStreamOptions {
|
|
22
|
-
from?: "markdown" | "slack-mrkdwn";
|
|
23
20
|
components?: MdComponents;
|
|
24
21
|
options?: RenderOptions;
|
|
25
|
-
/**
|
|
22
|
+
/** Also draw what is held back (open emphasis, table rows, code spans) ahead of time. Default `true`. With `false`, only final output is shown. */
|
|
26
23
|
eager?: boolean;
|
|
27
|
-
/**
|
|
24
|
+
/** Called when done. `revised` tells whether the final output differs from the last screen; if false, the hook does not redraw. */
|
|
28
25
|
onSettled?: (html: string, revised: boolean) => void;
|
|
29
26
|
}
|
|
30
27
|
|
|
31
28
|
/**
|
|
32
|
-
*
|
|
33
|
-
* `onSettled`
|
|
29
|
+
* Hook for streaming: call `push` for each token and `finish` at the end. By default it also draws what is held back (`eager`).
|
|
30
|
+
* Options other than `onSettled` are read only once, on first render.
|
|
34
31
|
*/
|
|
35
32
|
export function useMarkdownStream(opts?: MarkdownStreamOptions): {
|
|
36
33
|
elements: ReactElement;
|
|
@@ -38,6 +35,6 @@ export function useMarkdownStream(opts?: MarkdownStreamOptions): {
|
|
|
38
35
|
finish(): void;
|
|
39
36
|
};
|
|
40
37
|
|
|
41
|
-
/** html
|
|
42
|
-
/** `schemes`
|
|
38
|
+
/** Converts html channel output (when streaming, the accumulated output + `preview()` or `closeOpen()`) into React nodes. */
|
|
39
|
+
/** Pass the same `schemes` you gave the core as `options.html.schemes`. Default `["http", "https", "mailto"]`. */
|
|
43
40
|
export function toElements(html: string, components?: MdComponents, schemes?: string[]): ReactNode[];
|
package/js/react.js
CHANGED
|
@@ -40,11 +40,11 @@ function propsOf(tag, attrs, key, schemes) {
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
/**
|
|
43
|
-
* html
|
|
44
|
-
* `toElements(acc + streamer.closeOpen())`.
|
|
43
|
+
* Converts html channel output into an array of React nodes. Use it when you handle the
|
|
44
|
+
* accumulated stream output yourself: `toElements(acc + streamer.closeOpen())`.
|
|
45
45
|
* @param {string} html
|
|
46
|
-
* @param {Partial<Record<string, import("react").ElementType>>} [components]
|
|
47
|
-
* @param {string[]} [schemes]
|
|
46
|
+
* @param {Partial<Record<string, import("react").ElementType>>} [components] Components that replace individual tags.
|
|
47
|
+
* @param {string[]} [schemes] Schemes allowed for links and images. Same as the `options.html.schemes` given to the core.
|
|
48
48
|
*/
|
|
49
49
|
export function toElements(html, components = {}, schemes = DEFAULT_SCHEMES) {
|
|
50
50
|
const root = { tag: null, children: [] };
|
|
@@ -84,36 +84,41 @@ export function toElements(html, components = {}, schemes = DEFAULT_SCHEMES) {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
87
|
+
* Renders agent markdown, for complete text. If you pass the accumulated stream on every token,
|
|
88
|
+
* it reconverts from scratch each time, so total cost grows quadratically, and half-written markers
|
|
89
|
+
* (`**bo`, an opening backtick, `##`) briefly show up as text and then vanish. For streaming, use [`useMarkdownStream`].
|
|
90
90
|
* @param {import("./react.d.ts").MarkdownProps} props
|
|
91
91
|
*/
|
|
92
|
-
export function Markdown({ text,
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
return createElement(Fragment, null, ...toElements(html, components, opts.html?.schemes ?? DEFAULT_SCHEMES));
|
|
92
|
+
export function Markdown({ text, components, options }) {
|
|
93
|
+
const html = render(text, "html", options).join("");
|
|
94
|
+
return createElement(Fragment, null, ...toElements(html, components, options?.html?.schemes ?? DEFAULT_SCHEMES));
|
|
96
95
|
}
|
|
97
96
|
|
|
98
97
|
/**
|
|
99
|
-
*
|
|
98
|
+
* Hook for streaming. Call `push(chunk)` as tokens arrive and `finish()` at the end.
|
|
100
99
|
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
100
|
+
* By default (`eager: true`) it **also draws what is held back**: open emphasis closed, tables with
|
|
101
|
+
* the rows received so far, code spans closed (`Streamer.preview()`). This is a guess, so later tokens
|
|
102
|
+
* may change the shape, but the final output matches a one-shot conversion. With `eager: false`, only
|
|
103
|
+
* final output is shown (append-only, SPEC 8.2): unmatched emphasis and prefixes not yet decided
|
|
104
|
+
* appear once they become final.
|
|
105
105
|
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
106
|
+
* At the end it calls `onSettled(html, revised)`. `revised` tells whether the final output differs
|
|
107
|
+
* from what was last **drawn on screen**. If they match, the hook does not redraw.
|
|
108
108
|
* @param {import("./react.d.ts").MarkdownStreamOptions} [opts]
|
|
109
109
|
*/
|
|
110
|
-
export function useMarkdownStream({
|
|
110
|
+
export function useMarkdownStream({ components, options, eager = true, onSettled } = {}) {
|
|
111
111
|
const ref = useRef(null);
|
|
112
112
|
const init = useRef(null);
|
|
113
113
|
const [, rerender] = useReducer((n) => n + 1, 0);
|
|
114
114
|
if (init.current === null) {
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
init.current = () => ({
|
|
116
|
+
streamer: new Streamer("html", options),
|
|
117
|
+
acc: "",
|
|
118
|
+
done: false,
|
|
119
|
+
eager,
|
|
120
|
+
schemes: options?.html?.schemes ?? DEFAULT_SCHEMES,
|
|
121
|
+
});
|
|
117
122
|
}
|
|
118
123
|
// 해제됐으면 다시 만든다 — StrictMode 개발 모드는 이펙트를 붙였다 떼었다 다시 붙여서, 정리
|
|
119
124
|
// 함수가 스트리머를 먼저 해제한다(토큰이 오기 전이라 잃는 것은 없다).
|