diff --git a/index.compiler.spec.tsx b/index.compiler.spec.tsx index 1687af52..53f54506 100644 --- a/index.compiler.spec.tsx +++ b/index.compiler.spec.tsx @@ -747,6 +747,20 @@ describe('images', () => {
`) }) + + it('should handle an image reference with a missing reference', () => { + render( + compiler(theredoc` + ![test][1] + `) + ) + + expect(root.innerHTML).toMatchInlineSnapshot(` + + ![test][1] + + `) + }) }) describe('links', () => { @@ -848,6 +862,16 @@ describe('links', () => { `) }) + it('should handle a link reference with a missing reference', () => { + render(compiler('[foo][1]')) + + expect(root.innerHTML).toMatchInlineSnapshot(` + + [foo][1] + + `) + }) + it('list item should break paragraph', () => { render(compiler('foo\n- item')) diff --git a/index.tsx b/index.tsx index 2aed1747..4e5ffeba 100644 --- a/index.tsx +++ b/index.tsx @@ -1660,23 +1660,33 @@ export function compiler( refImage: { _match: simpleInlineRegex(REFERENCE_IMAGE_R), _order: Priority.MAX, - _parse(capture) { + _parse(capture, parse, state) { return { _alt: capture[1] || undefined, + _fallbackContent: parse( + capture[0].replace(SQUARE_BRACKETS_R, '\\$1'), + state + ), _ref: capture[2], } }, _react(node, output, state) { - return ( + return refs[node._ref] ? ( + ) : ( + { output(node._fallbackContent, state) } ) }, - } as MarkdownToJSX.Rule<{ _alt?: string; _ref: string }>, + } as MarkdownToJSX.Rule<{ + _alt?: string + _fallbackContent: MarkdownToJSX.ParserResult + _ref: string + }>, refLink: { _match: inlineRegex(REFERENCE_LINK_R),