Skip to content

Commit fe95c02

Browse files
committed
clean up footnotes, add ast option type
1 parent c758eff commit fe95c02

File tree

3 files changed

+56
-44
lines changed

3 files changed

+56
-44
lines changed

.changeset/ten-turtles-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'markdown-to-jsx': patch
3+
---
4+
5+
Remove unnecessary wrapper when footnotes are present.

index.compiler.spec.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4355,20 +4355,18 @@ describe('footnotes', () => {
43554355

43564356
expect(root.innerHTML).toMatchInlineSnapshot(`
43574357
<div>
4358-
<div>
4359-
<p>
4360-
foo
4361-
<a href="#abc">
4362-
<sup>
4363-
abc
4364-
</sup>
4365-
</a>
4366-
bar
4367-
</p>
4368-
<p>
4369-
After footnotes content
4370-
</p>
4371-
</div>
4358+
<p>
4359+
foo
4360+
<a href="#abc">
4361+
<sup>
4362+
abc
4363+
</sup>
4364+
</a>
4365+
bar
4366+
</p>
4367+
<p>
4368+
After footnotes content
4369+
</p>
43724370
<footer>
43734371
<div id="abc">
43744372
abc: Baz
@@ -5120,7 +5118,7 @@ it('correctly parses YAML front matter inside a code block', () => {
51205118
</code>
51215119
</pre>
51225120
`)
5123-
});
5121+
})
51245122

51255123
it('handles a holistic example', () => {
51265124
const md = fs.readFileSync(__dirname + '/fixture.md', 'utf8')

index.tsx

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,7 +1569,9 @@ export function compiler(
15691569
)
15701570
}
15711571

1572-
function compile(input: string): React.JSX.Element | React.ReactNode[] {
1572+
function compile(
1573+
input: string
1574+
): React.JSX.Element | React.ReactNode[] | MarkdownToJSX.ASTNode[] {
15731575
input = input.replace(FRONT_MATTER_R, '')
15741576

15751577
let inline = false
@@ -1584,16 +1586,20 @@ export function compiler(
15841586
inline = SHOULD_RENDER_AS_BLOCK_R.test(input) === false
15851587
}
15861588

1587-
const arr = emitter(
1588-
parser(
1589-
inline
1590-
? input
1591-
: `${trimEnd(input).replace(TRIM_STARTING_NEWLINES, '')}\n\n`,
1592-
{
1593-
inline,
1594-
}
1595-
)
1596-
) as React.ReactNode[]
1589+
const astNodes = parser(
1590+
inline
1591+
? input
1592+
: `${trimEnd(input).replace(TRIM_STARTING_NEWLINES, '')}\n\n`,
1593+
{
1594+
inline,
1595+
}
1596+
)
1597+
1598+
if (options.ast) {
1599+
return astNodes
1600+
}
1601+
1602+
const arr = emitter(astNodes) as React.ReactNode[]
15971603

15981604
while (
15991605
isString(arr[arr.length - 1]) &&
@@ -1602,6 +1608,21 @@ export function compiler(
16021608
arr.pop()
16031609
}
16041610

1611+
if (footnotes.length) {
1612+
arr.push(
1613+
<footer key="footer">
1614+
{footnotes.map(function createFootnote(def) {
1615+
return (
1616+
<div id={slug(def.identifier, slugify)} key={def.identifier}>
1617+
{def.identifier}
1618+
{emitter(parser(def.footnote, { inline: true }))}
1619+
</div>
1620+
)
1621+
})}
1622+
</footer>
1623+
)
1624+
}
1625+
16051626
if (options.wrapper === null) {
16061627
return arr
16071628
}
@@ -2269,24 +2290,6 @@ export function compiler(
22692290
console.log('Parse invocations:', parseCountsWithNames)
22702291
}
22712292

2272-
if (footnotes.length) {
2273-
return (
2274-
<div>
2275-
{jsx}
2276-
<footer key="footer">
2277-
{footnotes.map(function createFootnote(def) {
2278-
return (
2279-
<div id={slug(def.identifier, slugify)} key={def.identifier}>
2280-
{def.identifier}
2281-
{emitter(parser(def.footnote, { inline: true }))}
2282-
</div>
2283-
)
2284-
})}
2285-
</footer>
2286-
</div>
2287-
)
2288-
}
2289-
22902293
return jsx
22912294
}
22922295

@@ -2627,6 +2630,12 @@ export namespace MarkdownToJSX {
26272630
}
26282631

26292632
export type Options = Partial<{
2633+
/**
2634+
* When true, returns the parsed AST instead of rendered JSX.
2635+
* Footnotes are not automatically appended; the consumer handles them.
2636+
*/
2637+
ast: boolean
2638+
26302639
/**
26312640
* Ultimate control over the output of all rendered JSX.
26322641
*/

0 commit comments

Comments
 (0)