Skip to content

Test and fix conversion of empty parts #1675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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'
33 changes: 33 additions & 0 deletions packages/gitbook/src/modifiers/summary/__tests__/toDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
{
Expand Down
4 changes: 3 additions & 1 deletion packages/gitbook/src/modifiers/summary/toDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ function summaryToDocument(summary) {
}));
}

nodes.push(articlesToBlock(articles));
if (!articles.isEmpty()) {
nodes.push(articlesToBlock(articles));
}
});

return Document.create({ nodes });
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
35 changes: 35 additions & 0 deletions packages/gitbook/src/parse/__tests__/summaryFromDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
32 changes: 25 additions & 7 deletions packages/gitbook/src/parse/summaryFromDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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);
}

Expand Down