From 8705b55b295c5d8091504f97e395ab0130de0cd7 Mon Sep 17 00:00:00 2001 From: Evan Jacobs Date: Sun, 18 Nov 2018 23:57:53 -0600 Subject: [PATCH] graceful fallback for missing footnotes (#227) * add failing test for #226 * graceful fallback for missing footnotes fixes #226 * raise the size threshold slightly --- index.compiler.spec.js | 11 +++++++++++ index.js | 6 ++++-- package.json | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/index.compiler.spec.js b/index.compiler.spec.js index ee732ef2..efad811e 100644 --- a/index.compiler.spec.js +++ b/index.compiler.spec.js @@ -2673,6 +2673,17 @@ describe('footnotes', () => { +`); + }); + + it('should not blow up if footnote syntax is seen but no matching footnote was found', () => { + expect(() => render(compiler('[one] [two]'))).not.toThrow(); + expect(root.innerHTML).toMatchInlineSnapshot(` + + + [one] [two] + + `); }); }); diff --git a/index.js b/index.js index a0243d5f..88f2d6c5 100644 --- a/index.js +++ b/index.js @@ -164,6 +164,7 @@ const PARAGRAPH_R = /^((?:[^\n]|\n(?! *\n))+)(?:\n *)+\n/; const REFERENCE_IMAGE_OR_LINK = /^\[([^\]]*)\]:\s*(\S+)\s*("([^"]*)")?/; const REFERENCE_IMAGE_R = /^!\[([^\]]*)\] ?\[([^\]]*)\]/; const REFERENCE_LINK_R = /^\[([^\]]*)\] ?\[([^\]]*)\]/; +const SQUARE_BRACKETS_R = /(\[|\])/g; const SHOULD_RENDER_AS_BLOCK_R = /(\n|^[-*]\s|^#|^ {2,}|^-{2,}|^>\s)/; const TAB_R = /\t/g; const TABLE_TRIM_PIPES = /(^ *\||\| *$)/g; @@ -1373,11 +1374,12 @@ export function compiler(markdown, options) { parse(capture, parse, state) { return { content: parse(capture[1], state), + fallbackContent: parse(capture[0].replace(SQUARE_BRACKETS_R, '\\$1'), state), ref: capture[2], }; }, react(node, output, state) { - return ( + return refs[node.ref] ? ( {output(node.content, state)} - ); + ) : {output(node.fallbackContent, state)}; }, }, diff --git a/package.json b/package.json index 1dea5934..e8cf6644 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "size-limit": [ { "path": "dist/cjs.js", - "limit": "5 kB" + "limit": "5.15 kB" } ], "jest": {