Skip to content

Commit 3fc44e2

Browse files
authored
Markdown: parse partial links as they are streaming (#256)
1 parent 42ab29a commit 3fc44e2

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/components/Markdown/Markdown.test.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ describe('Markdown', () => {
8080
expect(getByRole('link').getAttribute('href')).toBe('https://hyperparam.app')
8181
})
8282

83+
it('renders a partial link', () => {
84+
const text = 'Check out [Hyp](https://hyper'
85+
const { getByText, queryByText } = render(<Markdown text={text} />)
86+
expect(getByText('Hyp')).toBeDefined()
87+
expect(queryByText('hyper')).toBeNull()
88+
expect(getByText('Hyp').tagName).toBe('A')
89+
})
90+
8391
it('renders multiple links in one line', () => {
8492
const text = 'Check out [Hyp](https://hyperparam.app) on [GitHub](https://github.com/hyparam).'
8593
const { getAllByRole, getByText } = render(<Markdown text={text} />)

src/components/Markdown/Markdown.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ function parseInlineRecursive(text: string, stop?: string): [Token[], number] {
302302
i++ // skip '('
303303
const endParen = text.indexOf(')', i)
304304
if (endParen === -1) {
305-
tokens.push({ type: 'text', content: text.slice(start, i) })
305+
// No closing ")": assume in-flight href
306+
const href = text.slice(i).trim()
307+
i = text.length // consume to EOI so loop terminates
308+
tokens.push({ type: 'link', href, children: linkTextTokens })
306309
continue
307310
}
308311
const href = text.slice(i, endParen).trim()

0 commit comments

Comments
 (0)