Skip to content

Commit 850a2fe

Browse files
committed
Add support for ignoring collapsable white-space
HTML often collapsed multiple white-space into one, or ignores it completely (such as between block-level elements). Which means HTML documents are written with that in mind. So I think that superfluous white-space should also be stripped when transforming to markdown.
1 parent 72f513f commit 850a2fe

File tree

9 files changed

+49
-18
lines changed

9 files changed

+49
-18
lines changed

handlers/table.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ function cellsBefore(parent, node) {
5050
break;
5151
}
5252

53+
/* istanbul ignore else - When proper HTML, should always be a cell */
5354
if (cell(child)) {
5455
pos++;
5556
}

handlers/text.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
module.exports = text;
44

5-
var u = require('unist-builder');
6-
var trimLines = require('trim-lines');
7-
85
function text(h, node) {
9-
if (node.value.match(/(\n+)/g)) {
10-
return null;
11-
}
12-
13-
return h.augment(node, u('text', trimLines(node.value)));
6+
return h.augment(node, {type: 'text', value: node.value});
147
}

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
module.exports = toMDAST;
44

5+
var minify = require('rehype-minify-whitespace')();
56
var xtend = require('xtend');
67
var one = require('./one');
78

89
h.augment = augment;
910

1011
function toMDAST(tree) {
11-
return one(h, tree);
12+
return one(h, minify(tree));
1213
}
1314

1415
function h(node, type, props, children) {
1516
var result;
17+
1618
if (!children && ((typeof props === 'object' && 'length' in props) || typeof props === 'string')) {
1719
children = props;
1820
props = {};

one.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
module.exports = one;
44

5-
var u = require('unist-builder');
65
var has = require('has');
76
var all = require('./all');
87
var handlers = require('./handlers');
@@ -21,7 +20,7 @@ function one(h, node, parent) {
2120

2221
function unknown(h, node) {
2322
if (node.value) {
24-
return h.augment(node, u('text', node.value));
23+
return h.augment(node, {type: 'text', value: node.value});
2524
}
2625

2726
return all(h, node);

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
"hast-util-has-property": "^1.0.0",
3131
"hast-util-is-element": "^1.0.0",
3232
"hast-util-to-string": "^1.0.0",
33-
"trim-lines": "^1.1.0",
34-
"unist-builder": "^1.0.2",
33+
"rehype-minify-whitespace": "^1.1.0",
3534
"unist-util-visit": "^1.1.1",
3635
"xtend": "^4.0.1"
3736
},

tests/fixtures/whitespace/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Alpha</title>
5+
<meta charset="utf8">
6+
</head>
7+
<body>
8+
<section>
9+
<p>
10+
Bravo Charlie
11+
</p>
12+
<p>
13+
</p>
14+
<p>Delta
15+
</p>
16+
<p>
17+
Echo</p>
18+
<p>
19+
<strong>Foxtrot</strong> <em>and</em> <code>Golf</code>
20+
</p>
21+
</section>
22+
</body>
23+
</html>

tests/fixtures/whitespace/index.json

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

tests/fixtures/whitespace/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Bravo Charlie
2+
3+
4+
5+
Delta
6+
7+
Echo
8+
9+
**Foxtrot** _and_ `Golf`

tests/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,13 @@ test('fixtures', function (t) {
137137
st.deepEqual(remark.stringify(tree), output || '\n', 'should produce the same documents');
138138
}
139139

140-
st.deepEqual(
141-
tree,
142-
remove(remark.run(remark.parse(output)), true),
143-
'should produce the same tree as remark'
144-
);
140+
if (!config || config.tree !== false) {
141+
st.deepEqual(
142+
tree,
143+
remove(remark.run(remark.parse(output)), true),
144+
'should produce the same tree as remark'
145+
);
146+
}
145147

146148
st.end();
147149
});

0 commit comments

Comments
 (0)