Skip to content

Commit

Permalink
graceful fallback for missing footnotes (#227)
Browse files Browse the repository at this point in the history
* add failing test for #226

* graceful fallback for missing footnotes

fixes #226

* raise the size threshold slightly
  • Loading branch information
quantizor authored Nov 19, 2018
1 parent da07d9a commit 8705b55
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
11 changes: 11 additions & 0 deletions index.compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2673,6 +2673,17 @@ describe('footnotes', () => {
</footer>
</div>
`);
});

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(`
<span data-reactroot>
[one] [two]
</span>
`);
});
});
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1373,19 +1374,20 @@ 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] ? (
<a
key={state.key}
href={sanitizeUrl(refs[node.ref].target)}
title={refs[node.ref].title}
>
{output(node.content, state)}
</a>
);
) : <span>{output(node.fallbackContent, state)}</span>;
},
},

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"size-limit": [
{
"path": "dist/cjs.js",
"limit": "5 kB"
"limit": "5.15 kB"
}
],
"jest": {
Expand Down

0 comments on commit 8705b55

Please sign in to comment.