Skip to content

Commit

Permalink
Fix unexpected sibling deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
demsking committed Sep 16, 2017
2 parents f574a84 + 35d1762 commit 5c381ff
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = (section, target, source) => {
for (let node of target.children) {
if (sectionLevel) {
if (!isClean) {
if (isNaN(node.depth) || node.depth >= sectionLevel) {
if (isNaN(node.depth) || node.depth > sectionLevel) {
continue
}
isClean = true
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ describe('transform', () => {
assert.deepEqual(doc, expected)
})

it('should successfully inject with a source from middle', () => {
const section = 'methods'
const target = require('./tree.source.middle')
const source = { children: [ newHeaderNode, newParagraphNode ] }
const expected = require('./tree.source.middle.expected')
const doc = inject(section, target, source)

assert.deepEqual(doc, expected)
})

it('should successfully inject with an existing section', () => {
const section = 'methods'
const target = require('./tree.multiple.section')
Expand Down
80 changes: 80 additions & 0 deletions test/tree.source.middle.expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"type": "Document",
"children": [
{
"type": "Header",
"depth": 2,
"children": [
{
"type": "Str",
"value": "textarea",
"raw": "textarea"
}
],
"raw": "## textarea "
},
{
"type": "Paragraph",
"children": [
{
"type": "Str",
"value": "A simple textarea component ",
"raw": "A simple textarea component "
}
],
"raw": "A simple textarea component "
},
{
"type": "Header",
"depth": 3,
"children": [
{
"type": "Str",
"value": "methods",
"raw": "methods"
}
],
"raw": "### methods "
},
{
"type": "Header",
"depth": 3,
"children": [
{
"type": "Str",
"value": "NEW_HEADER",
"raw": "NEW_HEADER"
}
],
"raw": "### NEW_HEADER "
},
{
"type": "Str",
"value": "NEW_PARAGRAPTH",
"raw": "NEW_PARAGRAPTH"
},
{
"type": "Header",
"depth": 3,
"children": [
{
"type": "Str",
"value": "last header",
"raw": "last header"
}
],
"raw": "## last header "
},
{
"type": "Paragraph",
"children": [
{
"type": "Str",
"value": "Last Paragraph",
"raw": "Last Paragraph"
}
],
"raw": "Last Paragraph"
}
]
}
96 changes: 96 additions & 0 deletions test/tree.source.middle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"type": "Document",
"children": [
{
"type": "Header",
"depth": 2,
"children": [
{
"type": "Str",
"value": "textarea",
"raw": "textarea"
}
],
"raw": "## textarea "
},
{
"type": "Paragraph",
"children": [
{
"type": "Str",
"value": "A simple textarea component ",
"raw": "A simple textarea component "
}
],
"raw": "A simple textarea component "
},
{
"type": "Header",
"depth": 3,
"children": [
{
"type": "Str",
"value": "methods",
"raw": "methods"
}
],
"raw": "### methods "
},
{
"type": "List",
"ordered": false,
"start": null,
"loose": false,
"children": [
{
"type": "ListItem",
"loose": false,
"checked": null,
"children": [
{
"type": "Paragraph",
"children": [
{
"type": "Code",
"value": "enable()",
"raw": "`enable()`"
},
{
"type": "Str",
"value": " \nEnable the textarea ",
"raw": " \nEnable the textarea "
}
],
"raw": "`enable()` \nEnable the textarea "
}
],
"raw": "- `enable()` \nEnable the textarea "
}
],
"raw": "- `enable()` \nEnable the textarea "
},
{
"type": "Header",
"depth": 3,
"children": [
{
"type": "Str",
"value": "last header",
"raw": "last header"
}
],
"raw": "## last header "
},
{
"type": "Paragraph",
"children": [
{
"type": "Str",
"value": "Last Paragraph",
"raw": "Last Paragraph"
}
],
"raw": "Last Paragraph"
}
]
}

0 comments on commit 5c381ff

Please sign in to comment.