Skip to content

Commit 2d39e28

Browse files
jeffalwooorm
authored andcommitted
Add support for newlines in options
Closes GH-37. Related to GH-35.
1 parent 3e6218f commit 2d39e28

File tree

9 files changed

+99
-3
lines changed

9 files changed

+99
-3
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
module.exports = toMDAST
44

5-
var minify = require('rehype-minify-whitespace')()
5+
var minify = require('rehype-minify-whitespace')
66
var xtend = require('xtend')
77
var one = require('./lib/one')
88
var handlers = require('./lib/handlers')
99

1010
function toMDAST(tree, options) {
1111
var settings = options || {}
12+
var opts = {newlines: settings.newlines === true}
1213

1314
h.baseFound = false
1415
h.frozenBaseURL = null
@@ -17,7 +18,7 @@ function toMDAST(tree, options) {
1718
h.augment = augment
1819
h.document = settings.document
1920

20-
return one(h, minify(tree), null)
21+
return one(h, minify(opts)(tree), null)
2122

2223
function h(node, type, props, children) {
2324
var result

readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ Whether the given tree is a complete document. If `document: true`,
6969
implicit paragraphs are added in the `root` node around inline MDAST nodes.
7070
Otherwise, inline MDAST nodes are wrapped when needed.
7171

72+
###### `options.newlines`
73+
74+
Whether to collapse to a newline (`\n`) instead of a single space (default) if
75+
a streak of white-space in a text node contains a newline.
76+
7277
##### Returns
7378

7479
[`MDASTNode`][mdast].

test/fixtures/newlines-off/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello
2+
World.

test/fixtures/newlines-off/index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fragment": true,
3+
"newlines": false
4+
}

test/fixtures/newlines-off/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World.

test/fixtures/newlines-on/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello
2+
World.

test/fixtures/newlines-on/index.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fragment": true,
3+
"newlines": true
4+
}

test/fixtures/newlines-on/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello
2+
World.

test/index.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ test('handlers option', function(t) {
207207
t.end()
208208
})
209209

210-
test('document', function(t) {
210+
test('document option', function(t) {
211211
var tree = u('root', [
212212
h('b', 'Importance'),
213213
u('text', ' and '),
@@ -241,3 +241,78 @@ test('document', function(t) {
241241

242242
t.end()
243243
})
244+
245+
test('newlines option', function(t) {
246+
t.deepEqual(
247+
toMDAST({
248+
type: 'root',
249+
children: [
250+
{
251+
type: 'element',
252+
tagName: 'p',
253+
properties: {},
254+
children: [
255+
{
256+
type: 'text',
257+
value: 'Alpha\nBeta'
258+
}
259+
]
260+
}
261+
]
262+
}),
263+
{
264+
type: 'root',
265+
children: [
266+
{
267+
type: 'paragraph',
268+
children: [
269+
{
270+
type: 'text',
271+
value: 'Alpha Beta'
272+
}
273+
]
274+
}
275+
]
276+
},
277+
'should collapse newline to a single space'
278+
)
279+
280+
t.deepEqual(
281+
toMDAST(
282+
{
283+
type: 'root',
284+
children: [
285+
{
286+
type: 'element',
287+
tagName: 'p',
288+
properties: {},
289+
children: [
290+
{
291+
type: 'text',
292+
value: 'Alpha\nBeta'
293+
}
294+
]
295+
}
296+
]
297+
},
298+
{newlines: true}
299+
),
300+
{
301+
type: 'root',
302+
children: [
303+
{
304+
type: 'paragraph',
305+
children: [
306+
{
307+
type: 'text',
308+
value: 'Alpha\nBeta'
309+
}
310+
]
311+
}
312+
]
313+
},
314+
'should contain newlines'
315+
)
316+
317+
t.end()
318+
})

0 commit comments

Comments
 (0)