Skip to content

Commit

Permalink
fix: gracefully handle missing image references
Browse files Browse the repository at this point in the history
Closes #524
  • Loading branch information
quantizor committed Mar 21, 2024
1 parent c0a0ac1 commit 1404696
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 @@ -782,6 +782,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 @@ -917,6 +931,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 @@ -1693,14 +1693,14 @@ export function compiler(
}
},
render(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 1404696

Please sign in to comment.