From 35d1762728d7787214416efd30154da37b8f08cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Demanou?= Date: Sat, 16 Sep 2017 20:18:28 +0200 Subject: [PATCH] Fix unexpected sibling deletion --- index.js | 2 +- test/test.js | 10 +++ test/tree.source.middle.expected.json | 80 ++++++++++++++++++++++ test/tree.source.middle.json | 96 +++++++++++++++++++++++++++ 4 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 test/tree.source.middle.expected.json create mode 100644 test/tree.source.middle.json diff --git a/index.js b/index.js index 32c2914..58d7d47 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/test.js b/test/test.js index 4107497..37691f2 100644 --- a/test/test.js +++ b/test/test.js @@ -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') diff --git a/test/tree.source.middle.expected.json b/test/tree.source.middle.expected.json new file mode 100644 index 0000000..b443d4c --- /dev/null +++ b/test/tree.source.middle.expected.json @@ -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" + } + ] +} diff --git a/test/tree.source.middle.json b/test/tree.source.middle.json new file mode 100644 index 0000000..89ad5c5 --- /dev/null +++ b/test/tree.source.middle.json @@ -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" + } + ] +}