Skip to content

Commit

Permalink
Change to match CommonMark, new remark
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
wooorm committed Oct 3, 2020
1 parent fda4f93 commit d8c151b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 72 deletions.
26 changes: 2 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var visit = require('unist-util-visit')
module.exports = compact

// Make an mdast tree compact by merging adjacent text nodes.
function compact(tree, commonmark) {
function compact(tree) {
visit(tree, visitor)

return tree
Expand All @@ -17,8 +17,7 @@ function compact(tree, commonmark) {
if (
previous &&
child.type === previous.type &&
mergeable(previous, commonmark) &&
mergeable(child, commonmark)
(child.type === 'text' || child.type === 'blockquote')
) {
if (child.value) {
previous.value += child.value
Expand All @@ -38,24 +37,3 @@ function compact(tree, commonmark) {
}
}
}

function mergeable(node, commonmark) {
var start
var end

if (node.type === 'text') {
if (!node.position) {
return true
}

start = node.position.start
end = node.position.end

// Only merge nodes which occupy the same size as their `value`.
return (
start.line !== end.line || end.column - start.column === node.value.length
)
}

return commonmark && node.type === 'blockquote'
}
9 changes: 4 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
[![Backers][backers-badge]][collective]
[![Chat][chat-badge]][chat]

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

## Install

Expand Down Expand Up @@ -41,11 +41,10 @@ Yields:

## API

### `compact(tree[, commonmark])`
### `compact(tree)`

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

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

Expand Down
51 changes: 8 additions & 43 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,25 @@ test('compact()', function (t) {
u('paragraph', [
u(
'text',
{
position: {start: {line: 1, column: 1}, end: {line: 1, column: 6}}
},
{position: {start: {line: 1, column: 1}, end: {line: 1, column: 6}}},
'alpha'
),
u(
'text',
{
position: {start: {line: 1, column: 6}, end: {line: 1, column: 7}}
},
{position: {start: {line: 1, column: 6}, end: {line: 1, column: 7}}},
' '
),
u(
'text',
{
position: {start: {line: 1, column: 7}, end: {line: 1, column: 12}}
},
{position: {start: {line: 1, column: 7}, end: {line: 1, column: 12}}},
'bravo'
)
])
),
u('paragraph', [
u(
'text',
{
position: {start: {line: 1, column: 1}, end: {line: 1, column: 12}}
},
{position: {start: {line: 1, column: 1}, end: {line: 1, column: 12}}},
'alpha bravo'
)
]),
Expand All @@ -57,26 +49,14 @@ test('compact()', function (t) {
u('text', 'at'),
u(
'text',
{
position: {start: {line: 1, column: 3}, end: {line: 1, column: 8}}
},
{position: {start: {line: 1, column: 3}, end: {line: 1, column: 8}}},
'&'
),
u('text', 't')
])
),
u('paragraph', [
u('text', 'at'),
u(
'text',
{
position: {start: {line: 1, column: 3}, end: {line: 1, column: 8}}
},
'&'
),
u('text', 't')
]),
'should not compact texts with incompatible positions'
u('paragraph', [u('text', 'at&t')]),
'should compact texts with incompatible positions'
)

t.same(
Expand All @@ -86,28 +66,13 @@ test('compact()', function (t) {
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
])
),
u('root', [
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
]),
'should not compact blockquotes'
)

t.same(
compact(
u('root', [
u('blockquote', [u('paragraph', [u('text', 'Alpha.')])]),
u('blockquote', [u('paragraph', [u('text', 'Bravo.')])])
]),
true
),
u('root', [
u('blockquote', [
u('paragraph', [u('text', 'Alpha.')]),
u('paragraph', [u('text', 'Bravo.')])
])
]),
'should compact blockquotes in commonmark mode'
'should compact blockquotes'
)

t.end()
Expand Down

0 comments on commit d8c151b

Please sign in to comment.