diff options
author | Nicolas Gaborit <soreine.plume@gmail.com> | 2017-01-13 18:56:32 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2017-01-13 18:56:32 +0100 |
commit | 7128db842a9dd1b68ba08d5e527546518c8618a8 (patch) | |
tree | c120d40cb6dd1cbd3737665fc868b58db28263fa | |
parent | 0342a11da0835abc9317101784191b49ebef1b65 (diff) | |
download | gitbook-7128db842a9dd1b68ba08d5e527546518c8618a8.zip gitbook-7128db842a9dd1b68ba08d5e527546518c8618a8.tar.gz gitbook-7128db842a9dd1b68ba08d5e527546518c8618a8.tar.bz2 |
Test and fix conversion of empty parts (#1675)
* Test and fix conversion of empty parts
* Cover case of last part empty
6 files changed, 212 insertions, 8 deletions
diff --git a/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-part.yaml b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-part.yaml new file mode 100644 index 0000000..de11664 --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-part.yaml @@ -0,0 +1,58 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Part 1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: 'Article 1' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Empty part 1' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Empty part 2' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Part 2' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: 'Article 2' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Empty part 3' diff --git a/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js b/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js index 93e8124..0252103 100644 --- a/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js +++ b/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js @@ -106,6 +106,39 @@ describe('summaryToDocument', () => { ]); }); + it('should convert empty parts', () => { + expectDocument('empty-part.yaml', [ + { + title: 'Part 1', + articles: [ + { + title: 'Article 1' + } + ] + }, + { + title: 'Empty part 1', + articles: [] + }, + { + title: 'Empty part 2', + articles: [] + }, + { + title: 'Part 2', + articles: [ + { + title: 'Article 2' + } + ] + }, + { + title: 'Empty part 3', + articles: [] + } + ]); + }); + it('should convert a deep summary', () => { expectDocument('deep.yaml', [ { diff --git a/packages/gitbook/src/modifiers/summary/toDocument.js b/packages/gitbook/src/modifiers/summary/toDocument.js index 7cd459a..cecc208 100644 --- a/packages/gitbook/src/modifiers/summary/toDocument.js +++ b/packages/gitbook/src/modifiers/summary/toDocument.js @@ -85,7 +85,9 @@ function summaryToDocument(summary) { })); } - nodes.push(articlesToBlock(articles)); + if (!articles.isEmpty()) { + nodes.push(articlesToBlock(articles)); + } }); return Document.create({ nodes }); diff --git a/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml new file mode 100644 index 0000000..de11664 --- /dev/null +++ b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml @@ -0,0 +1,58 @@ +nodes: + - kind: block + type: header_one + nodes: + - kind: text + text: Summary + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Part 1' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: 'Article 1' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Empty part 1' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Empty part 2' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Part 2' + - kind: block + type: unordered_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: unstyled + nodes: + - kind: text + text: 'Article 2' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Empty part 3' diff --git a/packages/gitbook/src/parse/__tests__/summaryFromDocument.js b/packages/gitbook/src/parse/__tests__/summaryFromDocument.js index 5ec964d..1714f26 100644 --- a/packages/gitbook/src/parse/__tests__/summaryFromDocument.js +++ b/packages/gitbook/src/parse/__tests__/summaryFromDocument.js @@ -112,6 +112,41 @@ describe('summaryFromDocument', () => { ]); }); + it('should parse empty parts', () => { + const summary = readSummary('summary/empty-part.yaml'); + + expectParts(summary, [ + { + title: 'Part 1', + articles: [ + { + title: 'Article 1' + } + ] + }, + { + title: 'Empty part 1', + articles: [] + }, + { + title: 'Empty part 2', + articles: [] + }, + { + title: 'Part 2', + articles: [ + { + title: 'Article 2' + } + ] + }, + { + title: 'Empty part 3', + articles: [] + } + ]); + }); + it('should parse an deep summary', () => { const summary = readSummary('summary/deep.yaml'); diff --git a/packages/gitbook/src/parse/summaryFromDocument.js b/packages/gitbook/src/parse/summaryFromDocument.js index 8b35897..1ce14b8 100644 --- a/packages/gitbook/src/parse/summaryFromDocument.js +++ b/packages/gitbook/src/parse/summaryFromDocument.js @@ -46,7 +46,9 @@ function listArticles(list) { function listParts(document) { const { nodes } = document; const parts = []; - let title = ''; + + // Keep a reference to a part, waiting for its articles + let pendingPart; nodes.forEach((node) => { const isHeading = ( @@ -55,20 +57,36 @@ function listParts(document) { ); if (isHeading) { - title = node.text; + if (pendingPart) { + // The previous was empty + parts.push(pendingPart); + } + pendingPart = { + title: node.text + }; } if (isList(node)) { const articles = listArticles(node); - parts.push({ - title, - articles - }); - title = ''; + if (pendingPart) { + pendingPart.articles = articles; + parts.push(pendingPart); + pendingPart = undefined; + } else { + parts.push({ + title: '', + articles + }); + } } }); + if (pendingPart) { + // The last one was empty + parts.push(pendingPart); + } + return List(parts); } |