Skip to content

Commit 950be99

Browse files
authored
✨ feat: add remend option to Streamdown (#5)
Pass RemendOptions to the tail completion pass in both the renderer and the smoothing hook. With default options, remend treats a comparison such as p<q as an unfinished HTML tag and drops everything after it until the stream ends; callers that do not render raw HTML can now pass { htmlTags: false }.
1 parent da46ebc commit 950be99

6 files changed

Lines changed: 39 additions & 11 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ Style the output yourself. The rendered tree is plain markdown HTML plus fade an
121121
| `granularity` | `'char' \| 'word'` | `'char'` | Fade animation unit |
122122
| `latexGuard` | `boolean` | `false` | Hold the last frame while a trailing formula is incomplete |
123123
| `preprocess` | `(text: string) => string` | — | Transform content before rendering |
124+
| `remend` | `RemendOptions` | — | Options for the remend pass that completes the open tail block, e.g. `{ htmlTags: false }` |
124125
| `components` / `remarkPlugins` / `rehypePlugins` | — | — | Passed through to `react-markdown` |
125126

126127
<div align="right">

‎src/Streamdown.test.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,11 @@ describe('buffered complete blocks', () => {
8989
expect(container.querySelector('p.stream-block')?.textContent).toBe('New complete paragraph.');
9090
});
9191
});
92+
93+
describe('remend options', () => {
94+
it('keeps the text after a comparison when HTML-tag completion is off', () => {
95+
const content = 'Inline notation p<q stays.\n\n## Later\n\nThe rest.';
96+
const { container } = render(<Streamdown content={content} remend={{ htmlTags: false }} />);
97+
expect(container.textContent).toContain('The rest.');
98+
});
99+
});

‎src/Streamdown.tsx‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import {
99
useId,
1010
useMemo,
1111
useRef,
12-
useState,
1312
} from 'react';
1413
import { type Options } from 'react-markdown';
14+
import { type RemendOptions } from 'remend';
1515
import type { Pluggable, PluggableList } from 'unified';
1616

1717
import { createBlockLexer } from './blockLexer';
@@ -224,17 +224,18 @@ interface StreamdownBlocksProps {
224224
content: string;
225225
granularity: StreamAnimationGranularity;
226226
markdownOptions: Omit<Options, 'children'>;
227+
remendOptions?: RemendOptions;
227228
tailUnitsRef: { current: number };
228229
}
229230

230231
const StreamdownBlocks = memo<StreamdownBlocksProps>(
231-
({ content: smoothedContent, granularity, markdownOptions, tailUnitsRef }) => {
232+
({ content: smoothedContent, granularity, markdownOptions, remendOptions, tailUnitsRef }) => {
232233
const profiler = useStreamdownProfiler();
233234
const { components: baseComponents, ...rest } = markdownOptions;
234235
const baseRehypePlugins = useStablePlugins(markdownOptions.rehypePlugins ?? EMPTY_PLUGINS);
235236
const remarkPlugins = useStablePlugins(markdownOptions.remarkPlugins ?? EMPTY_PLUGINS);
236237
const generatedId = useId();
237-
const [lexBlocks] = useState(createBlockLexer);
238+
const lexBlocks = useMemo(() => createBlockLexer(remendOptions), [remendOptions]);
238239

239240
const components = useMemo<Options['components']>(
240241
() => ({ ...baseComponents, [STREAM_TAIL_TAG]: StreamTail }) as Options['components'],
@@ -444,6 +445,11 @@ export interface StreamdownProps extends Omit<Options, 'children'> {
444445
*/
445446
latexGuard?: boolean;
446447
preprocess?: (text: string) => string;
448+
/**
449+
* Options for the remend pass that completes the open tail block, e.g.
450+
* `{ htmlTags: false }` when `<` is prose or math rather than HTML.
451+
*/
452+
remend?: RemendOptions;
447453
smoothing?: StreamSmoothingPreset;
448454
}
449455

@@ -452,15 +458,17 @@ export interface StreamdownProps extends Omit<Options, 'children'> {
452458
// a memoized child keyed on the smoother's output — so the expensive block
453459
// pipeline runs per reveal commit, not per chunk AND per commit.
454460
export const Streamdown = memo<StreamdownProps>(
455-
({ content, granularity = 'char', latexGuard = false, preprocess, smoothing, ...rest }) => {
461+
({ content, granularity = 'char', latexGuard = false, preprocess, remend, smoothing, ...rest }) => {
456462
const preprocessed = useMemo(
457463
() => (preprocess ? preprocess(content) : content),
458464
[content, preprocess],
459465
);
460466
const guardedContent = useLatexGuard(preprocessed, latexGuard);
461467
const tailUnitsRef = useRef(0);
468+
const remendOptions = useStableValue(remend);
462469
const smoothedContent = useSmoothStreamContent(guardedContent, {
463470
preset: smoothing ?? 'balanced',
471+
remend: remendOptions,
464472
tailUnitsRef,
465473
});
466474
const markdownOptions = useStableValue(rest);
@@ -470,6 +478,7 @@ export const Streamdown = memo<StreamdownProps>(
470478
content={smoothedContent}
471479
granularity={granularity}
472480
markdownOptions={markdownOptions}
481+
remendOptions={remendOptions}
473482
tailUnitsRef={tailUnitsRef}
474483
/>
475484
);

‎src/blockLexer.test.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ describe('createBlockLexer', () => {
5454
expect(lex(`${head}$$y`).processed).toBe(`${head}$$y$$`);
5555
});
5656

57+
it('passes remend options through, so a comparison is not mended as an HTML tag', () => {
58+
const source = 'Inline notation p<q stays.\n\n## Later\n\nThe rest.';
59+
expect(createBlockLexer()(source).processed).not.toContain('The rest.');
60+
expect(createBlockLexer({ htmlTags: false })(source).processed).toBe(source);
61+
});
62+
5763
it('re-lexes from scratch when content is not an append', () => {
5864
const lex = createBlockLexer();
5965
lex('para one\n\npara two\n\n- item');

‎src/blockLexer.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { marked, type Token } from 'marked';
2-
import remend from 'remend';
2+
import remend, { type RemendOptions } from 'remend';
33

44
import { type BlockInfo } from './useStreamQueue';
55

@@ -31,7 +31,7 @@ const toBlocks = (tokens: Token[], startOffset: number): BlockInfo[] => {
3131
});
3232
};
3333

34-
export const createBlockLexer = () => {
34+
export const createBlockLexer = (remendOptions?: RemendOptions) => {
3535
let frozenRaw = '';
3636
let frozenBlocks: BlockInfo[] = [];
3737

@@ -71,7 +71,7 @@ export const createBlockLexer = () => {
7171
}
7272

7373
const rawTail = content.slice(frozenRaw.length);
74-
const processedTail = remend(rawTail);
74+
const processedTail = remend(rawTail, remendOptions);
7575
const tailTokens = marked.lexer(processedTail);
7676
const tailBlocks = toBlocks(tailTokens, frozenRaw.length);
7777
const result = {

‎src/useSmoothStreamContent.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { useCallback, useEffect, useRef, useState } from 'react';
1+
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
2+
import { type RemendOptions } from 'remend';
23

34
import { createBlockLexer } from './blockLexer';
45
import { findOpenFenceLanguage } from './fenceState';
5-
import { getNow } from './internal';
6+
import { getNow, useStableValue } from './internal';
67
import { useStreamdownProfiler } from './profiler';
78
import { type StreamSmoothingPreset } from './types';
89

@@ -120,17 +121,20 @@ export const countChars = (text: string): number => {
120121
interface UseSmoothStreamContentOptions {
121122
enabled?: boolean;
122123
preset?: StreamSmoothingPreset;
124+
/** Options for the remend pass that completes the open tail block. */
125+
remend?: RemendOptions;
123126
tailUnitsRef?: { current: number };
124127
}
125128

126129
export const useSmoothStreamContent = (
127130
content: string,
128-
{ enabled = true, preset = 'balanced', tailUnitsRef }: UseSmoothStreamContentOptions = {},
131+
{ enabled = true, preset = 'balanced', remend, tailUnitsRef }: UseSmoothStreamContentOptions = {},
129132
): string => {
130133
const config = PRESET_CONFIG[preset];
131134
const profiler = useStreamdownProfiler();
132135
const [displayedContent, setDisplayedContent] = useState(content);
133-
const [lexBufferedContent] = useState(createBlockLexer);
136+
const remendOptions = useStableValue(remend);
137+
const lexBufferedContent = useMemo(() => createBlockLexer(remendOptions), [remendOptions]);
134138
const completePrefixRef = useRef({ content: '', count: 0 });
135139

136140
const displayedContentRef = useRef(content);

0 commit comments

Comments
 (0)