From 683d9061380924def6df2263ab3756b3e0a6a13a Mon Sep 17 00:00:00 2001 From: Barbara Valdez <34872381+barbaravaldez@users.noreply.github.com> Date: Thu, 7 Apr 2022 16:19:33 -0700 Subject: [PATCH] Create directory when adding a book section from book tree view (#18966) * create folder when creating a book section * fix test --- extensions/notebook/src/book/bookTocManager.ts | 3 ++- extensions/notebook/src/dialog/addTocEntryDialog.ts | 3 ++- extensions/notebook/src/test/book/bookTocManager.test.ts | 3 +-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/extensions/notebook/src/book/bookTocManager.ts b/extensions/notebook/src/book/bookTocManager.ts index b55a510fc4..2ad7fe606a 100644 --- a/extensions/notebook/src/book/bookTocManager.ts +++ b/extensions/notebook/src/book/bookTocManager.ts @@ -460,7 +460,6 @@ export class BookTocManager implements IBookTocManager { public async addNewTocEntry(pathDetails: TocEntryPathHandler, bookItem: BookTreeItem, isSection?: boolean): Promise { let findSection: JupyterBookSection | undefined = undefined; - await fs.writeFile(pathDetails.filePath, ''); if (bookItem.contextValue === BookTreeItemType.section) { findSection = { file: bookItem.book.page.file, title: bookItem.book.page.title }; } @@ -470,8 +469,10 @@ export class BookTocManager implements IBookTocManager { }; if (isSection) { + await fs.mkdir(path.dirname(pathDetails.filePath)); fileEntryInToc.sections = []; } + await fs.writeFile(pathDetails.filePath, ''); if (bookItem.book.version === BookVersion.v1) { fileEntryInToc = convertTo(BookVersion.v1, fileEntryInToc); diff --git a/extensions/notebook/src/dialog/addTocEntryDialog.ts b/extensions/notebook/src/dialog/addTocEntryDialog.ts index 944ab4f0ef..a9ffe08c7d 100644 --- a/extensions/notebook/src/dialog/addTocEntryDialog.ts +++ b/extensions/notebook/src/dialog/addTocEntryDialog.ts @@ -99,7 +99,8 @@ export class AddTocEntryDialog { public async createFile(fileName: string, titleName: string): Promise { try { const dirPath = this._bookItem.contextValue === BookTreeItemType.savedBook ? this._bookItem.rootContentPath : path.dirname(this._bookItem.book.contentPath); - const filePath = path.posix.join(dirPath, fileName).concat(this._extension); + // For sections, we pass the name of the section in the path to create a directory with the same name + const filePath = this._isSection ? path.posix.join(dirPath, titleName, fileName).concat(this._extension) : path.posix.join(dirPath, fileName).concat(this._extension); await this.validatePath(dirPath, fileName.concat(this._extension)); const pathDetails = new TocEntryPathHandler(filePath, this._bookItem.rootContentPath, titleName); await this._tocManager.addNewTocEntry(pathDetails, this._bookItem, this._isSection); diff --git a/extensions/notebook/src/test/book/bookTocManager.test.ts b/extensions/notebook/src/test/book/bookTocManager.test.ts index 3704d49e14..1e9ab2f4c1 100644 --- a/extensions/notebook/src/test/book/bookTocManager.test.ts +++ b/extensions/notebook/src/test/book/bookTocManager.test.ts @@ -576,8 +576,7 @@ describe('BookTocManagerTests', function () { bookTocManager = new BookTocManager(sourceBookModel); const fileBasename = `addSectionTest-${generateGuid()}`; const sectionTitle = 'Section Test'; - const testFilePath = path.join(run.sectionA.contentFolder, 'sectionA', fileBasename).concat(FileExtension.Markdown); - await fs.writeFile(testFilePath, ''); + const testFilePath = path.join(run.sectionA.contentFolder, 'sectionA', sectionTitle,fileBasename).concat(FileExtension.Markdown); const pathDetails = new TocEntryPathHandler(testFilePath, run.sourceBook.root, sectionTitle); await bookTocManager.addNewTocEntry(pathDetails, sectionA, true); let toc: JupyterBookSection[] = yaml.safeLoad((await fs.promises.readFile(run.sourceBook.tocPath)).toString());