Skip to content

Commit e2cc34c

Browse files
quantizorInnei
authored andcommitted
fix: gracefully handle missing image references (quantizor#554)
Closes quantizor#524 Signed-off-by: Innei <[email protected]>
1 parent c4220da commit e2cc34c

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

index.compiler.spec.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,20 @@ describe('images', () => {
755755
`)
756756
})
757757

758+
it('should gracefully handle an empty image reference', () => {
759+
render(
760+
compiler(theredoc`
761+
![][1]
762+
[2]: /xyz.png
763+
`)
764+
)
765+
766+
expect(root.innerHTML).toMatchInlineSnapshot(`
767+
<p>
768+
</p>
769+
`)
770+
})
771+
758772
it('should handle an image reference with alt text', () => {
759773
render(
760774
compiler(theredoc`
@@ -890,6 +904,23 @@ describe('links', () => {
890904
`)
891905
})
892906

907+
it('should gracefully handle an empty link reference', () => {
908+
render(
909+
compiler(theredoc`
910+
[][1]
911+
[2]: foo
912+
`)
913+
)
914+
915+
expect(root.innerHTML).toMatchInlineSnapshot(`
916+
<p>
917+
<span>
918+
[][1]
919+
</span>
920+
</p>
921+
`)
922+
})
923+
893924
it('list item should break paragraph', () => {
894925
render(compiler('foo\n- item'))
895926

index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,14 +1799,14 @@ export function compiler(
17991799
}
18001800
},
18011801
react(node, output, state) {
1802-
return (
1802+
return refs[node.ref] ? (
18031803
<img
18041804
key={state.key}
18051805
alt={node.alt}
18061806
src={sanitizeUrl(refs[node.ref].target)!}
18071807
title={refs[node.ref].title}
18081808
/>
1809-
)
1809+
) : null
18101810
},
18111811
} as MarkdownToJSX.Rule<{ alt?: string; ref: string }>,
18121812

0 commit comments

Comments
 (0)