Skip to content

Commit

Permalink
fix: gracefully handle missing image references (quantizor#554)
Browse files Browse the repository at this point in the history
Closes quantizor#524

Signed-off-by: Innei <i@innei.in>
  • Loading branch information
quantizor authored and Innei committed Mar 28, 2024
1 parent c4220da commit e2cc34c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions index.compiler.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,20 @@ describe('images', () => {
`)
})

it('should gracefully handle an empty image reference', () => {
render(
compiler(theredoc`
![][1]
[2]: /xyz.png
`)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<p>
</p>
`)
})

it('should handle an image reference with alt text', () => {
render(
compiler(theredoc`
Expand Down Expand Up @@ -890,6 +904,23 @@ describe('links', () => {
`)
})

it('should gracefully handle an empty link reference', () => {
render(
compiler(theredoc`
[][1]
[2]: foo
`)
)

expect(root.innerHTML).toMatchInlineSnapshot(`
<p>
<span>
[][1]
</span>
</p>
`)
})

it('list item should break paragraph', () => {
render(compiler('foo\n- item'))

Expand Down
4 changes: 2 additions & 2 deletions index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1799,14 +1799,14 @@ export function compiler(
}
},
react(node, output, state) {
return (
return refs[node.ref] ? (
<img
key={state.key}
alt={node.alt}
src={sanitizeUrl(refs[node.ref].target)!}
title={refs[node.ref].title}
/>
)
) : null
},
} as MarkdownToJSX.Rule<{ alt?: string; ref: string }>,

Expand Down

0 comments on commit e2cc34c

Please sign in to comment.