Skip to content

Commit 66e2567

Browse files
csantos1113Cesar Santos
andauthored
[FIX] - Footnote references (#304)
* Change footnote regular expressions * Change regex and use options.slugify Co-authored-by: Cesar Santos <[email protected]>
1 parent b653c7a commit 66e2567

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

index.compiler.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,6 +2972,67 @@ describe('footnotes', () => {
29722972
</footer>
29732973
</div>
29742974
2975+
`);
2976+
});
2977+
2978+
it('should handle complex references', () => {
2979+
render(compiler(['foo[^referencé heré 123] bar', '', '[^referencé heré 123]: Baz baz'].join('\n')));
2980+
2981+
expect(root.innerHTML).toMatchInlineSnapshot(`
2982+
2983+
<div data-reactroot>
2984+
<p>
2985+
foo
2986+
<a href="#reference-here-123">
2987+
<sup>
2988+
referencé heré 123
2989+
</sup>
2990+
</a>
2991+
bar
2992+
</p>
2993+
<footer>
2994+
<div id="reference-here-123">
2995+
referencé heré 123
2996+
: Baz baz
2997+
</div>
2998+
</footer>
2999+
</div>
3000+
3001+
`);
3002+
});
3003+
3004+
it('should handle conversion of multiple references into links', () => {
3005+
render(compiler(['foo[^abc] bar. baz[^def]', '', '[^abc]: Baz baz', '[^def]: Def'].join('\n')));
3006+
3007+
expect(root.innerHTML).toMatchInlineSnapshot(`
3008+
3009+
<div data-reactroot>
3010+
<p>
3011+
foo
3012+
<a href="#abc">
3013+
<sup>
3014+
abc
3015+
</sup>
3016+
</a>
3017+
bar. baz
3018+
<a href="#def">
3019+
<sup>
3020+
def
3021+
</sup>
3022+
</a>
3023+
</p>
3024+
<footer>
3025+
<div id="abc">
3026+
abc
3027+
: Baz baz
3028+
</div>
3029+
<div id="def">
3030+
def
3031+
: Def
3032+
</div>
3033+
</footer>
3034+
</div>
3035+
29753036
`);
29763037
});
29773038

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ const CODE_BLOCK_R = /^(?: {4}[^\n]+\n*)+(?:\n *)+\n?/;
111111
const CODE_INLINE_R = /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/;
112112
const CONSECUTIVE_NEWLINE_R = /^(?:\n *)*\n/;
113113
const CR_NEWLINE_R = /\r\n?/g;
114-
const FOOTNOTE_R = /^\[\^(.*)\](:.*)\n/;
115-
const FOOTNOTE_REFERENCE_R = /^\[\^(.*)\]/;
114+
const FOOTNOTE_R = /^\[\^([^\]]+)](:.*)\n/;
115+
const FOOTNOTE_REFERENCE_R = /^\[\^([^\]]+)]/;
116116
const FORMFEED_R = /\f/g;
117117
const GFM_TASK_R = /^\s*?\[(x|\s)\]/;
118118
const HEADING_R = /^ *(#{1,6}) *([^\n]+)\n{0,2}/;
@@ -966,7 +966,7 @@ export function compiler(markdown, options) {
966966
parse(capture /*, parse*/) {
967967
return {
968968
content: capture[1],
969-
target: `#${capture[1]}`,
969+
target: `#${options.slugify(capture[1])}`,
970970
};
971971
},
972972
react(node, output, state) {
@@ -1565,7 +1565,7 @@ export function compiler(markdown, options) {
15651565
<footer key="footer">
15661566
{footnotes.map(function createFootnote(def) {
15671567
return (
1568-
<div id={def.identifier} key={def.identifier}>
1568+
<div id={options.slugify(def.identifier)} key={def.identifier}>
15691569
{def.identifier}
15701570
{emitter(parser(def.footnote, { inline: true }))}
15711571
</div>

0 commit comments

Comments
 (0)