-
Notifications
You must be signed in to change notification settings - Fork 45
feat(parse)!: heal incomplete markdown only while streaming #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
918fc8e
e3046b7
48f7a86
89514b8
18e41d8
9ba38d1
2c71815
1422aa4
c4585f2
141db83
584c1ed
4178713
d0625ad
677ac8f
1c645ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -57,19 +57,44 @@ autoCloseMarkdown('hello *', { dropTrailingOpeners: true }) | |||||||||||||||
| :: | ||||||||||||||||
|
|
||||||||||||||||
| ::tip | ||||||||||||||||
| `autoCloseMarkdown` is also available as a parse option: set `autoClose: true` (default) in `parseMarkdown()` or `createMarkdownParser()` to apply it automatically. | ||||||||||||||||
| `autoCloseMarkdown` is also available as a parse option: `autoClose` in `parseMarkdown()` and `createMarkdownParser()` applies it for you while streaming. | ||||||||||||||||
| :: | ||||||||||||||||
|
|
||||||||||||||||
| ### Parser integration | ||||||||||||||||
|
|
||||||||||||||||
| `autoClose` is enabled by default in `parseMarkdown()` and `createMarkdownParser()`. You can disable it or provide a custom completion function: | ||||||||||||||||
| `autoClose` defaults to `'streaming'`, so healing runs when you parse with `{ streaming: true }` and a plain parse follows CommonMark: | ||||||||||||||||
|
|
||||||||||||||||
| ```typescript | ||||||||||||||||
| import { createMarkdownParser, parseMarkdown } from 'comark' | ||||||||||||||||
|
|
||||||||||||||||
| await parseMarkdown('a _b') | ||||||||||||||||
| // 'a _b' stays literal text | ||||||||||||||||
|
|
||||||||||||||||
| const parse = createMarkdownParser() | ||||||||||||||||
| await parse('a _b', { streaming: true }) | ||||||||||||||||
| // ['em', {}, 'b'] | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| ::warning | ||||||||||||||||
| Before this default, healing ran on every parse. If you parse a stored response that may have been cut off, set `autoClose: true` to keep closing it. | ||||||||||||||||
| :: | ||||||||||||||||
|
|
||||||||||||||||
| You can force it on, turn it off, or provide a custom completion function: | ||||||||||||||||
|
|
||||||||||||||||
| ::code-group | ||||||||||||||||
| ```typescript [Enabled (default)] | ||||||||||||||||
| ```typescript [Streaming (default)] | ||||||||||||||||
| import { parseMarkdown } from 'comark' | ||||||||||||||||
|
|
||||||||||||||||
| const result = await parseMarkdown(content, { | ||||||||||||||||
| autoClose: 'streaming' // default | ||||||||||||||||
| }) | ||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win Pass
Proposed documentation fix const result = await parseMarkdown(content, {
- autoClose: 'streaming' // default
+ autoClose: 'streaming', // default
+ streaming: true,
})📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| ```typescript [Always] | ||||||||||||||||
| import { parseMarkdown } from 'comark' | ||||||||||||||||
|
|
||||||||||||||||
| const result = await parseMarkdown(content, { | ||||||||||||||||
| autoClose: true // default | ||||||||||||||||
| autoClose: true | ||||||||||||||||
| }) | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
|
|
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import React from 'react' | ||
| import { renderToReadableStream } from 'react-dom/server' | ||
| import { Markdown } from '../src/index' | ||
|
|
||
| async function renderAsync(element: React.ReactElement): Promise<string> { | ||
| const stream = await renderToReadableStream(element) | ||
| await stream.allReady | ||
| return new Response(stream).text() | ||
| } | ||
|
|
||
| // The `streaming` prop drives the renderer (caret, stream components) but it also | ||
| // has to reach the parser: auto-close heals only on a streaming parse. | ||
| describe('<Markdown streaming>', () => { | ||
| it('heals incomplete markdown while streaming', async () => { | ||
| const html = await renderAsync( | ||
| <Markdown | ||
| value="Hello **wor" | ||
| streaming | ||
| /> | ||
| ) | ||
| expect(html).toContain('<strong>') | ||
| expect(html).toContain('wor') | ||
| }) | ||
|
|
||
| it('leaves incomplete markdown alone when not streaming', async () => { | ||
| const html = await renderAsync(<Markdown value="Hello **wor" />) | ||
| expect(html).not.toContain('<strong>') | ||
| expect(html).toContain('**wor') | ||
| }) | ||
|
|
||
| it('still heals a non-streaming parse when asked explicitly', async () => { | ||
| const html = await renderAsync( | ||
| <Markdown | ||
| value="Hello **wor" | ||
| options={{ autoClose: true }} | ||
| /> | ||
| ) | ||
| expect(html).toContain('<strong>') | ||
| }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.