Skip to content

Commit d8c151b

Browse files
committed
Change to match CommonMark, new remark
* remark is now complies to CommonMark, this means that we can adhere too, and merge adjacent block quotes * remark no longer has separate text nodes for character references (`&`) and character escapes (`\&`), so we can now merge adjacent text nodes always
1 parent fda4f93 commit d8c151b

File tree

3 files changed

+14
-72
lines changed

3 files changed

+14
-72
lines changed

index.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var visit = require('unist-util-visit')
55
module.exports = compact
66

77
// Make an mdast tree compact by merging adjacent text nodes.
8-
function compact(tree, commonmark) {
8+
function compact(tree) {
99
visit(tree, visitor)
1010

1111
return tree
@@ -17,8 +17,7 @@ function compact(tree, commonmark) {
1717
if (
1818
previous &&
1919
child.type === previous.type &&
20-
mergeable(previous, commonmark) &&
21-
mergeable(child, commonmark)
20+
(child.type === 'text' || child.type === 'blockquote')
2221
) {
2322
if (child.value) {
2423
previous.value += child.value
@@ -38,24 +37,3 @@ function compact(tree, commonmark) {
3837
}
3938
}
4039
}
41-
42-
function mergeable(node, commonmark) {
43-
var start
44-
var end
45-
46-
if (node.type === 'text') {
47-
if (!node.position) {
48-
return true
49-
}
50-
51-
start = node.position.start
52-
end = node.position.end
53-
54-
// Only merge nodes which occupy the same size as their `value`.
55-
return (
56-
start.line !== end.line || end.column - start.column === node.value.length
57-
)
58-
}
59-
60-
return commonmark && node.type === 'blockquote'
61-
}

readme.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
[![Backers][backers-badge]][collective]
99
[![Chat][chat-badge]][chat]
1010

11-
[**mdast**][mdast] utility to make trees compact: collapse text nodes (when
12-
possible) and blockquotes (in commonmark mode).
11+
[**mdast**][mdast] utility to make trees compact: collapse adjacent text nodes
12+
and blockquotes.
1313

1414
## Install
1515

@@ -41,11 +41,10 @@ Yields:
4141

4242
## API
4343

44-
### `compact(tree[, commonmark])`
44+
### `compact(tree)`
4545

4646
Walk the [tree][] and collapse nodes.
47-
Combines adjacent [text][]s (but not when they represent entities or escapes).
48-
If `commonmark` is `true`, collapses [blockquote][]s.
47+
Combines adjacent [text][]s and collapses [blockquote][]s.
4948

5049
Handles [positional information][position-information] properly.
5150

test.js

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,25 @@ test('compact()', function (t) {
1818
u('paragraph', [
1919
u(
2020
'text',
21-
{
22-
position: {start: {line: 1, column: 1}, end: {line: 1, column: 6}}
23-
},
21+
{position: {start: {line: 1, column: 1}, end: {line: 1, column: 6}}},
2422
'alpha'
2523
),
2624
u(
2725
'text',
28-
{
29-
position: {start: {line: 1, column: 6}, end: {line: 1, column: 7}}
30-
},
26+
{position: {start: {line: 1, column: 6}, end: {line: 1, column: 7}}},
3127
' '
3228
),
3329
u(
3430
'text',
35-
{
36-
position: {start: {line: 1, column: 7}, end: {line: 1, column: 12}}
37-
},
31+
{position: {start: {line: 1, column: 7}, end: {line: 1, column: 12}}},
3832
'bravo'
3933
)
4034
])
4135
),
4236
u('paragraph', [
4337
u(
4438
'text',
45-
{
46-
position: {start: {line: 1, column: 1}, end: {line: 1, column: 12}}
47-
},
39+
{position: {start: {line: 1, column: 1}, end: {line: 1, column: 12}}},
4840
'alpha bravo'
4941
)
5042
]),
@@ -57,26 +49,14 @@ test('compact()', function (t) {
5749
u('text', 'at'),
5850
u(
5951
'text',
60-
{
61-
position: {start: {line: 1, column: 3}, end: {line: 1, column: 8}}
62-
},
52+
{position: {start: {line: 1, column: 3}, end: {line: 1, column: 8}}},
6353
'&'
6454
),
6555
u('text', 't')
6656
])
6757
),
68-
u('paragraph', [
69-
u('text', 'at'),
70-
u(
71-
'text',
72-
{
73-
position: {start: {line: 1, column: 3}, end: {line: 1, column: 8}}
74-
},
75-
'&'
76-
),
77-
u('text', 't')
78-
]),
79-
'should not compact texts with incompatible positions'
58+
u('paragraph', [u('text', 'at&t')]),
59+
'should compact texts with incompatible positions'
8060
)
8161

8262
t.same(
@@ -86,28 +66,13 @@ test('compact()', function (t) {
8666
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
8767
])
8868
),
89-
u('root', [
90-
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
91-
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
92-
]),
93-
'should not compact blockquotes'
94-
)
95-
96-
t.same(
97-
compact(
98-
u('root', [
99-
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
100-
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
101-
]),
102-
true
103-
),
10469
u('root', [
10570
u('blockquote', [
10671
u('paragraph', [u('text', 'Alpha.')]),
10772
u('paragraph', [u('text', 'Bravo.')])
10873
])
10974
]),
110-
'should compact blockquotes in commonmark mode'
75+
'should compact blockquotes'
11176
)
11277

11378
t.end()

0 commit comments

Comments
 (0)