Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 860d56d

Browse files
committed
Add support for fragments
Closes GH-54.
1 parent 780b4cd commit 860d56d

File tree

7 files changed

+49
-20
lines changed

7 files changed

+49
-20
lines changed

index.js

+23-12
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,23 @@ var sanitize = require('hast-util-sanitize')
77
var toH = require('hast-to-hyperscript')
88
var tableCellStyle = require('@mapbox/hast-util-table-cell-style')
99

10+
var globalReact
1011
var globalCreateElement
12+
var globalFragment
1113

14+
/* istanbul ignore next - Hard to test */
1215
try {
13-
globalCreateElement = require('react').createElement
16+
globalReact = require('react')
17+
globalCreateElement = globalReact.createElement
18+
globalFragment = globalReact.Fragment
1419
} catch (error) {}
1520

1621
var own = {}.hasOwnProperty
1722

1823
function react(options) {
1924
var settings = options || {}
2025
var createElement = settings.createElement || globalCreateElement
26+
var Fragment = settings.fragment || globalFragment
2127
var clean = settings.sanitize !== false
2228
var scheme =
2329
clean && typeof settings.sanitize !== 'boolean' ? settings.sanitize : null
@@ -28,24 +34,29 @@ function react(options) {
2834

2935
// Wrapper around `createElement` to pass components in.
3036
function h(name, props, children) {
31-
var component = own.call(components, name) ? components[name] : name
32-
33-
return createElement(component, props, children)
37+
return createElement(
38+
own.call(components, name) ? components[name] : name,
39+
props,
40+
children
41+
)
3442
}
3543

3644
// Compile mdast to React.
3745
function compile(node) {
38-
var hast = {
39-
type: 'element',
40-
tagName: 'div',
41-
properties: {},
42-
children: toHAST(node, toHastOptions).children
43-
}
46+
var tree = toHAST(node, toHastOptions)
47+
var root
4448

4549
if (clean) {
46-
hast = sanitize(hast, scheme)
50+
tree = sanitize(tree, scheme)
51+
}
52+
53+
root = toH(h, tableCellStyle(tree), settings.prefix)
54+
55+
// If this compiled to a `<div>`, but fragment are possible, use those.
56+
if (root.type === 'div' && Fragment) {
57+
root = createElement(Fragment, {}, root.props.children)
4758
}
4859

49-
return toH(h, tableCellStyle(hast), settings.prefix)
60+
return root
5061
}
5162
}

readme.md

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ Transform markdown to React.
7777
How to create elements or components (`Function`).
7878
Default: `require('react').createElement`)
7979

80+
###### `options.fragment`
81+
82+
Create fragments instead of an outer `<div>` if available (`Function`).
83+
Default: `require('react').Fragment`)
84+
8085
###### `options.sanitize`
8186

8287
Sanitation schema to use (`object` or `boolean`, default: `undefined`).

test/index.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ versions.forEach(function(reactVersion) {
7676
})
7777
.processSync('# Foo').contents
7878
),
79-
'<div><h2>Foo</h2></div>',
79+
'<h2>Foo</h2>',
8080
'should use custom components'
8181
)
8282

@@ -90,10 +90,23 @@ versions.forEach(function(reactVersion) {
9090
})
9191
.processSync('```empty\n```').contents
9292
),
93-
'<div><pre><code class="language-empty"></code></pre></div>',
93+
'<pre><code class="language-empty"></code></pre>',
9494
'does not sanitize input when `sanitize` option is set to false'
9595
)
9696

97+
t.equal(
98+
React.renderToStaticMarkup(
99+
remark()
100+
.use(reactRenderer, {
101+
createElement: React.createElement,
102+
fragment: React.Fragment
103+
})
104+
.processSync('# Hello\nWorld').contents
105+
),
106+
'<h1>Hello</h1>\n<p>World</p>',
107+
'should support given fragments'
108+
)
109+
97110
t.equal(
98111
React.renderToStaticMarkup(
99112
remark()
@@ -103,7 +116,7 @@ versions.forEach(function(reactVersion) {
103116
})
104117
.processSync('# Foo').contents
105118
),
106-
'<div><h1>Foo</h1></div>',
119+
'<h1>Foo</h1>',
107120
'passes toHast options to inner toHAST() function'
108121
)
109122

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div><p>Foo bar baz qux.</p></div>
1+
<p>Foo bar baz qux.</p>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div><p>Foo bar baz qux.</p></div>
1+
<p>Foo bar baz qux.</p>

test/react/v16/fixtures/tables/output.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div><table>
1+
<table>
22
<thead>
33
<tr>
44
<th>Alpha</th>
@@ -39,4 +39,4 @@
3939
<td style="text-align:center"></td>
4040
</tr>
4141
</tbody>
42-
</table></div>
42+
</table>
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<div><h1>Hello world</h1></div>
1+
<h1>Hello world</h1>

0 commit comments

Comments
 (0)