Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/components/Markdown/Markdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ describe('Markdown', () => {
const { getByText } = render(<Markdown text={text} />)
expect(getByText('This is a blockquote.')).toBeDefined()
})

it('renders a horizontal rule', () => {
const text = 'First paragraph\n---\nSecond paragraph'
const { container } = render(<Markdown text={text} />)

const paragraphs = container.querySelectorAll('p')
expect(paragraphs.length).toBe(2)
expect(paragraphs[0]?.textContent).toBe('First paragraph')
expect(paragraphs[1]?.textContent).toBe('Second paragraph')

const hr = container.querySelector('hr')
expect(hr).toBeDefined()
})
})

describe('Markdown code blocks', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/components/Markdown/Markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Token =
| { type: 'list', ordered: boolean, items: Token[][] }
| { type: 'blockquote', children: Token[] }
| { type: 'codeblock', language?: string, content: string }
| { type: 'hr' }

function parseMarkdown(text: string): Token[] {
const tokens: Token[] = []
Expand Down Expand Up @@ -50,6 +51,13 @@ function parseMarkdown(text: string): Token[] {
continue
}

// Horizontal rule
if (/^---+$/.test(line.trim())) {
tokens.push({ type: 'hr' })
i++
continue
}

// Heading
const headingMatch = /^(#{1,6})\s+(.*)/.exec(line)
if (headingMatch !== null) {
Expand Down Expand Up @@ -110,6 +118,7 @@ function parseMarkdown(text: string): Token[] {
if (ln.startsWith('>')) break // blockquote
if (/^(#{1,6})\s+/.test(ln)) break // heading
if (/^(\s*)([-*+–•‣◦○⚬]|\d+\.)\s+/.test(ln)) break // list item
if (/^---+$/.test(ln.trim())) break // horizontal rule

paraLines.push(ln)
i++
Expand Down Expand Up @@ -499,6 +508,8 @@ function renderTokens(tokens: Token[], keyPrefix = ''): ReactNode[] {
{ key },
createElement('code', null, token.content)
)
case 'hr':
return createElement('hr', { key })
default:
return null
}
Expand Down