From e2cc34cae21ac255d54e2b84807faefe66bbc1ba Mon Sep 17 00:00:00 2001 From: Evan Jacobs <570070+quantizor@users.noreply.github.com> Date: Wed, 20 Mar 2024 22:45:06 -0400 Subject: [PATCH] fix: gracefully handle missing image references (#554) Closes #524 Signed-off-by: Innei --- index.compiler.spec.tsx | 31 +++++++++++++++++++++++++++++++ index.tsx | 4 ++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/index.compiler.spec.tsx b/index.compiler.spec.tsx index f0b9f539..6b3769d8 100644 --- a/index.compiler.spec.tsx +++ b/index.compiler.spec.tsx @@ -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(` +
+
+ `) + }) + it('should handle an image reference with alt text', () => { render( compiler(theredoc` @@ -890,6 +904,23 @@ describe('links', () => { `) }) + it('should gracefully handle an empty link reference', () => { + render( + compiler(theredoc` + [][1] + [2]: foo + `) + ) + + expect(root.innerHTML).toMatchInlineSnapshot(` ++ + [][1] + +
+ `) + }) + it('list item should break paragraph', () => { render(compiler('foo\n- item')) diff --git a/index.tsx b/index.tsx index 1004049a..36898ba0 100644 --- a/index.tsx +++ b/index.tsx @@ -1799,14 +1799,14 @@ export function compiler( } }, react(node, output, state) { - return ( + return refs[node.ref] ? ( - ) + ) : null }, } as MarkdownToJSX.Rule<{ alt?: string; ref: string }>,