Book Editing: Support adding new sections to a book (#17074)

* add a new section command
This commit is contained in:
Barbara Valdez
2021-09-14 11:31:43 -07:00
committed by GitHub
parent f92e6d24d4
commit ce13d01efb
10 changed files with 65 additions and 19 deletions

View File

@@ -20,6 +20,8 @@ import { BookTreeViewProvider } from '../../book/bookTreeView';
import { NavigationProviders } from '../../common/constants';
import { BookVersion } from '../../book/bookVersionHandler';
import * as yaml from 'js-yaml';
import { TocEntryPathHandler } from '../../book/tocEntryPathHandler';
import * as utils from '../../common/utils';
export function equalTOC(actualToc: IJupyterBookSectionV2[], expectedToc: IJupyterBookSectionV2[]): boolean {
for (let [i, section] of actualToc.entries()) {
@@ -552,6 +554,26 @@ describe('BookTocManagerTests', function () {
should(JSON.stringify(listFiles).includes('test')).be.true('Empty directories within the moving element directory are not deleted');
});
it('Add new section', async () => {
bookTocManager = new BookTocManager(sourceBookModel);
const fileBasename = `addSectionTest-${utils.generateGuid()}`;
const sectionTitle = 'Section Test';
const testFilePath = path.join(run.sectionA.sectionRoot, fileBasename).concat(utils.FileExtension.Markdown);
await fs.writeFile(testFilePath, '');
const pathDetails = new TocEntryPathHandler(testFilePath, run.sourceBook.rootBookFolderPath, sectionTitle);
await bookTocManager.addNewTocEntry(pathDetails, sectionA, true);
let toc: JupyterBookSection[] = yaml.safeLoad((await fs.promises.readFile(run.sourceBook.tocPath)).toString());
const sectionAIndex = toc.findIndex(entry => entry.title === sectionA.title);
let newSectionIndex = -1;
let newSection = undefined;
if (sectionAIndex) {
newSectionIndex = toc[sectionAIndex].sections?.findIndex(entry => entry.title === sectionTitle);
newSection = toc[sectionAIndex].sections[newSectionIndex];
}
should(newSectionIndex).not.be.equal(-1, 'The new section should exist in the toc file');
should(newSection.sections).not.undefined();
});
afterEach(async function (): Promise<void> {
sinon.restore();
if (await exists(sourceBookFolderPath)) {