Skip to content

Commit 9303e58

Browse files
committed
fix(newlines): merged sibling newlines after badge removal
1 parent d4f7af7 commit 9303e58

File tree

7 files changed

+119
-4
lines changed

7 files changed

+119
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
},
8282
"dependencies": {
8383
"mdast-util-definitions": "^2.0.1",
84-
"unist-util-remove": "^2.0.0"
84+
"unist-util-remove": "^2.0.0",
85+
"unist-util-visit": "^2.0.2"
8586
}
8687
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import any from '@travi/any';
2+
import {assert} from 'chai';
3+
import mergeNewlines from './merge-newlines-in-paragraph';
4+
5+
suite('merge newlines in a paragraph', () => {
6+
const originalChildren = any.listOf(any.simpleObject);
7+
const index = any.integer({max: originalChildren.length - 1});
8+
9+
test('that a newline node is removed if its direct sibling is also a newline', () => {
10+
const node = {...any.simpleObject(), type: 'text', value: '\n'};
11+
const parent = {
12+
...any.simpleObject(),
13+
type: 'paragraph',
14+
children: [
15+
...originalChildren.slice(0, index),
16+
{type: 'text', value: '\n'},
17+
{type: 'text', value: '\n'},
18+
...originalChildren.slice(index)
19+
]
20+
};
21+
22+
mergeNewlines(node, index, parent);
23+
24+
assert.deepEqual(
25+
parent.children,
26+
[
27+
...originalChildren.slice(0, index),
28+
{type: 'text', value: '\n'},
29+
...originalChildren.slice(index)
30+
]
31+
);
32+
});
33+
34+
test('that a newline node is removed if node is the last child of the parent', () => {
35+
const node = {...any.simpleObject(), type: 'text', value: '\n'};
36+
const parent = {
37+
...any.simpleObject(),
38+
type: 'paragraph',
39+
children: [
40+
...originalChildren,
41+
{type: 'text', value: '\n'}
42+
]
43+
};
44+
45+
mergeNewlines(node, originalChildren.length, parent);
46+
47+
assert.deepEqual(parent.children, originalChildren);
48+
});
49+
50+
test('that the children of the parent are unchanged if the parent is not a `paragraph`', () => {
51+
const node = {...any.simpleObject(), type: 'text', value: '\n'};
52+
const parent = {...any.simpleObject(), children: [...originalChildren]};
53+
54+
mergeNewlines(node, index, parent);
55+
56+
assert.deepEqual(parent.children, originalChildren);
57+
});
58+
59+
test('that the children of the parent are unchanged if the node is not a `text` node', () => {
60+
const node = {...any.simpleObject(), type: any.word(), value: '\n'};
61+
const parent = {...any.simpleObject(), children: [...originalChildren], type: 'paragraph'};
62+
63+
mergeNewlines(node, index, parent);
64+
65+
assert.deepEqual(parent.children, originalChildren);
66+
});
67+
68+
test('that the children of the parent are unchanged if the node is not a newline', () => {
69+
const node = {...any.simpleObject(), type: 'text', value: any.word()};
70+
const parent = {...any.simpleObject(), children: [...originalChildren], type: 'paragraph'};
71+
72+
mergeNewlines(node, index, parent);
73+
74+
assert.deepEqual(parent.children, originalChildren);
75+
});
76+
});

src/merge-newlines-in-paragraph.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function nodeIsLastChild(index, parent) {
2+
return index === parent.children.length - 1;
3+
}
4+
5+
function nextSiblingIsNewline(nextSibling) {
6+
return nextSibling && 'text' === nextSibling.type && '\n' === nextSibling.value;
7+
}
8+
9+
export default function (node, index, parent) {
10+
if ('paragraph' === parent.type) {
11+
const nextSibling = parent.children[index + 1];
12+
13+
if (nodeIsLastChild(index, parent) || nextSiblingIsNewline(nextSibling)) {
14+
parent.children.splice(index, 1);
15+
}
16+
}
17+
}

src/plugin-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import {assert} from 'chai';
22
import any from '@travi/any';
33
import sinon from 'sinon';
44
import * as remove from '../thirdparty-wrappers/unist-util-remove';
5+
import * as visit from '../thirdparty-wrappers/unist-util-visit';
56
import * as referencedBadgePredicateFactory from './referenced-badge-predicate';
7+
import mergeNewlines from './merge-newlines-in-paragraph';
68
import {GREENKEEPER_URL} from './constants';
79
import plugin from './plugin';
810

@@ -14,6 +16,7 @@ suite('plugin', () => {
1416

1517
sandbox.stub(remove, 'default');
1618
sandbox.stub(referencedBadgePredicateFactory, 'default');
19+
sandbox.stub(visit, 'default');
1720
});
1821

1922
teardown(() => sandbox.restore());
@@ -29,5 +32,6 @@ suite('plugin', () => {
2932
assert.calledWith(remove.default, tree, {type: 'definition', identifier: 'greenkeeper-badge'});
3033
assert.calledWith(remove.default, tree, {type: 'definition', identifier: 'greenkeeper-link'});
3134
assert.calledWith(remove.default, tree, referencedBadgePredicate);
35+
assert.calledWith(visit.default, tree, {type: 'text', value: '\n'}, mergeNewlines);
3236
});
3337
});

src/plugin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import remove from '../thirdparty-wrappers/unist-util-remove';
2+
import visit from '../thirdparty-wrappers/unist-util-visit';
23
import createReferencedBadgePredicate from './referenced-badge-predicate';
4+
import mergeNewlines from './merge-newlines-in-paragraph';
35
import {GREENKEEPER_URL} from './constants';
46

57
export default function () {
@@ -8,5 +10,6 @@ export default function () {
810
remove(tree, createReferencedBadgePredicate(tree));
911
remove(tree, {type: 'definition', identifier: 'greenkeeper-badge'});
1012
remove(tree, {type: 'definition', identifier: 'greenkeeper-link'});
13+
visit(tree, {type: 'text', value: '\n'}, mergeNewlines);
1114
};
1215
}

test/integration/features/step_definitions/badge-steps.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ const inlineGreenkeeperBadge = `[![Greenkeeper badge](https://badges.greenkeeper
1414
}.svg)](https://greenkeeper.io/)`;
1515
const greenkeeperBadgeWithReferencedDefinitions = '[![Greenkeeper][greenkeeper-badge]][greenkeeper-link]';
1616
const otherInlineBadge = `[![${any.word()}](${any.url()})](${any.url()})`;
17+
const anotherInlineBadge = `[![${any.word()}](${any.url()})](${any.url()})`;
1718
const otherBadgeWithReferencedDefinitions = `[![${any.word()}][${imageReference}]][${linkReference}]`;
19+
const anotherBadgeWithReferencedDefinitions = `[![${any.word()}][${imageReference}]][${linkReference}]`;
1820
const otherBadgeDefinitions = `[${imageReference}]: ${any.url()}
1921
22+
[${linkReference}]: ${any.url()}`;
23+
const anotherSetOfBadgeDefinitions = `[${imageReference}]: ${any.url()}
24+
2025
[${linkReference}]: ${any.url()}`;
2126

2227
Before(function () {
@@ -42,26 +47,32 @@ Given('a greenkeeper badge exists with referenced definitions', async function (
4247
});
4348

4449
Given('other inline badges exist', async function () {
50+
this.badgeGroup.unshift(anotherInlineBadge);
4551
this.badgeGroup.push(otherInlineBadge);
52+
otherBadges.push(anotherInlineBadge);
4653
otherBadges.push(otherInlineBadge);
4754
});
4855

4956
Given('other badges exist with referenced definitions', async function () {
57+
this.badgeGroup.unshift(anotherBadgeWithReferencedDefinitions);
5058
this.badgeGroup.push(otherBadgeWithReferencedDefinitions);
59+
otherBadges.push(anotherBadgeWithReferencedDefinitions);
5160
otherBadges.push(otherBadgeWithReferencedDefinitions);
5261

5362
this.badgeDefinitions.push(otherBadgeDefinitions);
63+
this.badgeDefinitions.push(anotherSetOfBadgeDefinitions);
5464
otherDefinitions.push(otherBadgeDefinitions);
65+
otherDefinitions.push(anotherSetOfBadgeDefinitions);
5566
});
5667

5768
Then('the greenkeeper badge is removed from the README', async function () {
5869
assert.equal(
5970
this.resultingContent,
60-
`# some-project
61-
${otherBadges.length ? `
71+
`# some-project${otherBadges.length ? `
6272
6373
${otherBadges.join('\n')}
64-
` : ''}
74+
` : `
75+
`}
6576
${normalLink}${otherDefinitions.length ? `
6677
6778
${otherDefinitions.join('\n\n')}` : ''}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import visit from 'unist-util-visit';
2+
3+
export default visit;

0 commit comments

Comments
 (0)