[Editing Books] - Refactor buildToc method (#14532)

* refactor buildToc method to only modify the section found and return a boolean

* fix tests

* Address pr comments
This commit is contained in:
Barbara Valdez
2021-03-05 11:41:31 -08:00
committed by GitHub
parent b82942a030
commit a17a4a585e
3 changed files with 71 additions and 91 deletions

View File

@@ -20,6 +20,7 @@ import { BookTreeViewProvider } from '../../book/bookTreeView';
import { NavigationProviders } from '../../common/constants';
import * as loc from '../../common/localizedConstants';
export function equalTOC(actualToc: IJupyterBookSectionV2[], expectedToc: IJupyterBookSectionV2[]): boolean {
for (let [i, section] of actualToc.entries()) {
if (section.title !== expectedToc[i].title || section.file !== expectedToc[i].file) {
@@ -95,7 +96,7 @@ describe('BookTocManagerTests', function () {
}];
await bookTocManager.createBook(bookFolderPath, root2FolderPath);
should(equalTOC(bookTocManager.tableofContents[1].sections, expectedSection)).be.true;
should(bookTocManager.tableofContents[1].file).be.equal(path.join(path.sep, subfolder, 'index'));
should((bookTocManager.tableofContents[1] as IJupyterBookSectionV2).file).be.equal(path.join(path.sep, subfolder, 'index'));
});
it('should ignore invalid file extensions', async () => {
@@ -338,7 +339,11 @@ describe('BookTocManagerTests', function () {
treeItemCollapsibleState: undefined,
type: BookTreeItemType.Markdown,
version: run.version,
page: run.sectionA.sectionFormat
page: {
title: run.sectionA.sectionName,
file: path.join(path.sep, 'sectionA', 'readme'),
sections: run.sectionA.sectionFormat
}
};
// section B is from source book
@@ -353,7 +358,11 @@ describe('BookTocManagerTests', function () {
treeItemCollapsibleState: undefined,
type: BookTreeItemType.Markdown,
version: run.version,
page: run.sectionB.sectionFormat
page: {
title: run.sectionB.sectionName,
file: path.join(path.sep, 'sectionB', 'readme'),
sections: run.sectionB.sectionFormat
}
};
// notebook5 is from source book
@@ -374,12 +383,8 @@ describe('BookTocManagerTests', function () {
type: BookTreeItemType.Notebook,
version: run.version,
page: {
sections: [
{
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
]
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
};
@@ -400,12 +405,8 @@ describe('BookTocManagerTests', function () {
type: BookTreeItemType.Notebook,
version: run.version,
page: {
sections: [
{
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
]
'title': 'Notebook 5',
'file': path.join(path.sep, 'notebook5')
}
};
@@ -432,7 +433,7 @@ describe('BookTocManagerTests', function () {
sectionA.tableOfContentsPath = run.sourceBook.tocPath;
sectionB.tableOfContentsPath = run.sourceBook.tocPath;
notebook.tableOfContentsPath = run.sourceBook.tocPath;
duplicatedNotebook.tableOfContentsPath = run.sourceBook.tocPath;
duplicatedNotebook.tableOfContentsPath = undefined;
sectionA.sections = run.sectionA.sectionFormat;
sectionB.sections = run.sectionB.sectionFormat;
@@ -468,9 +469,9 @@ describe('BookTocManagerTests', function () {
}
// target book
await fs.writeFile(run.targetBook.tocPath, '- title: Welcome\n file: /readme\n- title: Section C\n file: /sectionC/readme\n sections:\n - title: Notebook6\n file: /sectionC/notebook6');
await fs.writeFile(run.targetBook.tocPath, '- title: Welcome\n file: /readme\n- title: Section C\n file: /sectionC/readme\n sections:\n - title: Notebook 6\n file: /sectionC/notebook6');
// source book
await fs.writeFile(run.sourceBook.tocPath, '- title: Notebook 5\n file: /notebook5\n- title: Section A\n file: /sectionA/readme\n sections:\n - title: Notebook1\n file: /sectionA/notebook1\n - title: Notebook2\n file: /sectionA/notebook2');
await fs.writeFile(run.sourceBook.tocPath, '- title: Notebook 5\n file: /notebook5\n- title: Section A\n file: /sectionA/readme\n sections:\n - title: Notebook1\n file: /sectionA/notebook1\n - title: Notebook2\n file: /sectionA/notebook2\n- title: Section B\n file: /sectionB/readme\n sections:\n - title: Notebook3\n file: /sectionB/notebook3\n - title: Notebook4\n file: /sectionB/notebook4');
const mockExtensionContext = new MockExtensionContext();