From ed879a5242f52d99f2842419da8e0a2edb374a36 Mon Sep 17 00:00:00 2001 From: Soreine Date: Fri, 13 Jan 2017 18:38:35 +0100 Subject: [PATCH 1/2] Test and fix conversion of empty parts --- .../__tests__/fixtures/empty-part.yaml | 47 +++++++++++++++++++ .../modifiers/summary/__tests__/toDocument.js | 25 ++++++++++ .../src/modifiers/summary/toDocument.js | 4 +- .../fixtures/summary/empty-part.yaml | 47 +++++++++++++++++++ .../parse/__tests__/summaryFromDocument.js | 27 +++++++++++ .../gitbook/src/parse/summaryFromDocument.js | 27 ++++++++--- 6 files changed, 169 insertions(+), 8 deletions(-) create mode 100644 packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-part.yaml create mode 100644 packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml 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 0000000000..406a7feed0 --- /dev/null +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-part.yaml @@ -0,0 +1,47 @@ +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' + + - 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' + diff --git a/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js b/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js index 93e81246d9..ee9770635a 100644 --- a/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js +++ b/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js @@ -106,6 +106,31 @@ describe('summaryToDocument', () => { ]); }); + it('should convert empty parts', () => { + expectDocument('empty-part.yaml', [ + { + title: 'Part 1', + articles: [ + { + title: 'Article 1' + } + ] + }, + { + title: 'Empty part', + articles: [] + }, + { + title: 'Part 2', + articles: [ + { + title: 'Article 2' + } + ] + } + ]); + }); + 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 7cd459a675..cecc208aa6 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 0000000000..406a7feed0 --- /dev/null +++ b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml @@ -0,0 +1,47 @@ +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' + + - 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' + diff --git a/packages/gitbook/src/parse/__tests__/summaryFromDocument.js b/packages/gitbook/src/parse/__tests__/summaryFromDocument.js index 5ec964d819..85117faa55 100644 --- a/packages/gitbook/src/parse/__tests__/summaryFromDocument.js +++ b/packages/gitbook/src/parse/__tests__/summaryFromDocument.js @@ -112,6 +112,33 @@ 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', + articles: [] + }, + { + title: 'Part 2', + articles: [ + { + title: 'Article 2' + } + ] + } + ]); + }); + 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 8b35897f0b..c915cb3601 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,17 +57,28 @@ 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 + }); + } } }); From 52aa6cc0b9a766ed4617644ddbbab61431b13818 Mon Sep 17 00:00:00 2001 From: Soreine Date: Fri, 13 Jan 2017 18:52:55 +0100 Subject: [PATCH 2/2] Cover case of last part empty --- .../summary/__tests__/fixtures/empty-part.yaml | 13 ++++++++++++- .../src/modifiers/summary/__tests__/toDocument.js | 10 +++++++++- .../__tests__/fixtures/summary/empty-part.yaml | 13 ++++++++++++- .../src/parse/__tests__/summaryFromDocument.js | 10 +++++++++- packages/gitbook/src/parse/summaryFromDocument.js | 5 +++++ 5 files changed, 47 insertions(+), 4 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 index 406a7feed0..de11664b71 100644 --- a/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-part.yaml +++ b/packages/gitbook/src/modifiers/summary/__tests__/fixtures/empty-part.yaml @@ -26,7 +26,13 @@ nodes: type: header_two nodes: - kind: text - text: 'Empty part' + text: 'Empty part 1' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Empty part 2' - kind: block type: header_two @@ -45,3 +51,8 @@ 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 ee9770635a..0252103ff5 100644 --- a/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js +++ b/packages/gitbook/src/modifiers/summary/__tests__/toDocument.js @@ -117,7 +117,11 @@ describe('summaryToDocument', () => { ] }, { - title: 'Empty part', + title: 'Empty part 1', + articles: [] + }, + { + title: 'Empty part 2', articles: [] }, { @@ -127,6 +131,10 @@ describe('summaryToDocument', () => { title: 'Article 2' } ] + }, + { + title: 'Empty part 3', + articles: [] } ]); }); diff --git a/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml index 406a7feed0..de11664b71 100644 --- a/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml +++ b/packages/gitbook/src/parse/__tests__/fixtures/summary/empty-part.yaml @@ -26,7 +26,13 @@ nodes: type: header_two nodes: - kind: text - text: 'Empty part' + text: 'Empty part 1' + + - kind: block + type: header_two + nodes: + - kind: text + text: 'Empty part 2' - kind: block type: header_two @@ -45,3 +51,8 @@ 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 85117faa55..1714f269f2 100644 --- a/packages/gitbook/src/parse/__tests__/summaryFromDocument.js +++ b/packages/gitbook/src/parse/__tests__/summaryFromDocument.js @@ -125,7 +125,11 @@ describe('summaryFromDocument', () => { ] }, { - title: 'Empty part', + title: 'Empty part 1', + articles: [] + }, + { + title: 'Empty part 2', articles: [] }, { @@ -135,6 +139,10 @@ describe('summaryFromDocument', () => { title: 'Article 2' } ] + }, + { + title: 'Empty part 3', + articles: [] } ]); }); diff --git a/packages/gitbook/src/parse/summaryFromDocument.js b/packages/gitbook/src/parse/summaryFromDocument.js index c915cb3601..1ce14b8d19 100644 --- a/packages/gitbook/src/parse/summaryFromDocument.js +++ b/packages/gitbook/src/parse/summaryFromDocument.js @@ -82,6 +82,11 @@ function listParts(document) { } }); + if (pendingPart) { + // The last one was empty + parts.push(pendingPart); + } + return List(parts); }