Skip to content

Commit

Permalink
[FIX] - Footnote references (#304)
Browse files Browse the repository at this point in the history
* Change footnote regular expressions

* Change regex and use options.slugify

Co-authored-by: Cesar Santos <csantos@helloelephant.com>
  • Loading branch information
csantos1113 and Cesar Santos committed May 19, 2020
1 parent b653c7a commit 66e2567
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
61 changes: 61 additions & 0 deletions index.compiler.spec.js
Expand Up @@ -2972,6 +2972,67 @@ describe('footnotes', () => {
</footer>
</div>
`);
});

it('should handle complex references', () => {
render(compiler(['foo[^referenc茅 her茅 123] bar', '', '[^referenc茅 her茅 123]: Baz baz'].join('\n')));

expect(root.innerHTML).toMatchInlineSnapshot(`
<div data-reactroot>
<p>
foo
<a href="#reference-here-123">
<sup>
referenc茅 her茅 123
</sup>
</a>
bar
</p>
<footer>
<div id="reference-here-123">
referenc茅 her茅 123
: Baz baz
</div>
</footer>
</div>
`);
});

it('should handle conversion of multiple references into links', () => {
render(compiler(['foo[^abc] bar. baz[^def]', '', '[^abc]: Baz baz', '[^def]: Def'].join('\n')));

expect(root.innerHTML).toMatchInlineSnapshot(`
<div data-reactroot>
<p>
foo
<a href="#abc">
<sup>
abc
</sup>
</a>
bar. baz
<a href="#def">
<sup>
def
</sup>
</a>
</p>
<footer>
<div id="abc">
abc
: Baz baz
</div>
<div id="def">
def
: Def
</div>
</footer>
</div>
`);
});

Expand Down
8 changes: 4 additions & 4 deletions index.js
Expand Up @@ -111,8 +111,8 @@ const CODE_BLOCK_R = /^(?: {4}[^\n]+\n*)+(?:\n *)+\n?/;
const CODE_INLINE_R = /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/;
const CONSECUTIVE_NEWLINE_R = /^(?:\n *)*\n/;
const CR_NEWLINE_R = /\r\n?/g;
const FOOTNOTE_R = /^\[\^(.*)\](:.*)\n/;
const FOOTNOTE_REFERENCE_R = /^\[\^(.*)\]/;
const FOOTNOTE_R = /^\[\^([^\]]+)](:.*)\n/;
const FOOTNOTE_REFERENCE_R = /^\[\^([^\]]+)]/;
const FORMFEED_R = /\f/g;
const GFM_TASK_R = /^\s*?\[(x|\s)\]/;
const HEADING_R = /^ *(#{1,6}) *([^\n]+)\n{0,2}/;
Expand Down Expand Up @@ -966,7 +966,7 @@ export function compiler(markdown, options) {
parse(capture /*, parse*/) {
return {
content: capture[1],
target: `#${capture[1]}`,
target: `#${options.slugify(capture[1])}`,
};
},
react(node, output, state) {
Expand Down Expand Up @@ -1565,7 +1565,7 @@ export function compiler(markdown, options) {
<footer key="footer">
{footnotes.map(function createFootnote(def) {
return (
<div id={def.identifier} key={def.identifier}>
<div id={options.slugify(def.identifier)} key={def.identifier}>
{def.identifier}
{emitter(parser(def.footnote, { inline: true }))}
</div>
Expand Down

0 comments on commit 66e2567

Please sign in to comment.