Skip to content

Commit f350a40

Browse files
committed
Add support for enforcing documents with document: true
Related to 475366c. Related to GH-27. Closes remarkjs/remark#273.
1 parent 77e9b3e commit f350a40

File tree

7 files changed

+54
-7
lines changed

7 files changed

+54
-7
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ Transform the given [HAST][] tree to an [MDAST][] tree.
4343
Object mapping tag-names to functions handling those elements.
4444
Take a look at [`handlers/`][handlers] for examples.
4545

46+
###### `options.document`
47+
48+
Whether the given tree is a complete document. If `document: true`,
49+
implicit paragraphs are added in the `root` node around inline MDAST nodes.
50+
Otherwise, inline MDAST nodes are wrapped when needed.
51+
4652
## Related
4753

4854
* [`mdast-util-to-hast`][mdast-util-to-hast]

handlers/root.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ var all = require('../all');
66
var wrap = require('../wrap');
77

88
function root(h, node) {
9-
return h(node, 'root', wrap.optional(all(h, node)));
9+
var children = all(h, node);
10+
11+
if (h.document || wrap.needed(children)) {
12+
children = wrap(children);
13+
}
14+
15+
return h(node, 'root', children);
1016
}

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function toMDAST(tree, options) {
1212

1313
h.handlers = xtend(handlers, settings.handlers || {});
1414
h.augment = augment;
15+
h.document = settings.document;
1516

1617
return one(h, minify(tree), null);
1718

tests/fixtures/implicit-paragraphs/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ <h1>An <em>explicit</em> paragraph</h1>
1111
<h1>Heading</h1>
1212
Another <em>implicit</em> paragraph.
1313
</blockquote>
14+
15+
<em>Emphasis</em>, <strong>importance</strong>, and <s>strikethrough</s>.

tests/fixtures/implicit-paragraphs/index.md

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ An **implicit** paragraph.
77
> # Heading
88
>
99
> Another _implicit_ paragraph.
10+
11+
_Emphasis_, **importance**, and ~~strikethrough~~.

tests/index.js

+35
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,38 @@ test('handlers option', function (t) {
189189

190190
t.end();
191191
});
192+
193+
test('document', function (t) {
194+
var tree = u('root', [
195+
h('b', 'Importance'),
196+
u('text', ' and '),
197+
h('i', 'emphasis'),
198+
u('text', '.')
199+
]);
200+
201+
t.deepEqual(
202+
toMDAST(tree),
203+
u('root', [
204+
u('strong', [u('text', 'Importance')]),
205+
u('text', ' and '),
206+
u('emphasis', [u('text', 'emphasis')]),
207+
u('text', '.')
208+
]),
209+
'should infer document if not needed'
210+
);
211+
212+
t.deepEqual(
213+
toMDAST(tree, {document: true}),
214+
u('root', [
215+
u('paragraph', [
216+
u('strong', [u('text', 'Importance')]),
217+
u('text', ' and '),
218+
u('emphasis', [u('text', 'emphasis')]),
219+
u('text', '.')
220+
])
221+
]),
222+
'should support an explitic `document: true`'
223+
);
224+
225+
t.end();
226+
});

wrap.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = wrap;
44

5-
wrap.optional = optional;
5+
wrap.needed = needed;
66

77
var is = require('unist-util-is');
88

@@ -56,11 +56,6 @@ function wrap(nodes) {
5656
return result;
5757
}
5858

59-
/* Optionally wrap `nodes`, if `needed`. */
60-
function optional(nodes) {
61-
return needed(nodes) ? wrap(nodes) : nodes;
62-
}
63-
6459
/* Check if there are non-inline MDAST nodes returned.
6560
* This is needed if a fragment is given, which could just be
6661
* a sentence, and doesn’t need a wrapper paragraph. */

0 commit comments

Comments
 (0)