Add support for new version of Jupyter Book (#12086)

* Add support for new jupyter book version

* Add changes to the jupyter notebook to create books

* Create config file

* Add support of new version of jupyter book on ADS

* Fix paths for opening folder with v1 and v2 books

* Add tests for jupyter book v2

* Update tests

* Fix tests

* Fix get parent issue

* Address PR comments

* Fix bookVersion condition in getSections and fix issue on create book notebook

* Fix search

* update python notebook

* Remove commented lines
This commit is contained in:
Barbara Valdez
2020-09-10 11:32:58 -07:00
committed by GitHub
parent ff61eae164
commit 3ff9df8e0b
6 changed files with 959 additions and 582 deletions

View File

@@ -11,16 +11,17 @@ import * as constants from '../common/constants';
import { IPrompter, IQuestion, confirm } from '../prompts/question';
import CodeAdapter from '../prompts/adapter';
import { BookTreeItem, BookTreeItemType } from './bookTreeItem';
import { BookModel } from './bookModel';
import { BookModel, BookVersion } from './bookModel';
import { Deferred } from '../common/promise';
import { IBookTrustManager, BookTrustManager } from './bookTrustManager';
import * as loc from '../common/localizedConstants';
import * as glob from 'fast-glob';
import { isNullOrUndefined } from 'util';
import { IJupyterBookSectionV2, IJupyterBookSectionV1 } from '../contracts/content';
import { debounce, getPinnedNotebooks } from '../common/utils';
import { IBookPinManager, BookPinManager } from './bookPinManager';
const Content = 'content';
const content = 'content';
interface BookSearchResults {
notebookPaths: string[];
@@ -152,7 +153,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
// add file watcher on toc file.
if (!isNotebook) {
fs.watchFile(path.join(bookPath, '_data', 'toc.yml'), async (curr, prev) => {
fs.watchFile(this.currentBook.tableOfContentsPath, async (curr, prev) => {
if (curr.mtime > prev.mtime) {
let book = this.books.find(book => book.bookPath === bookPath);
if (book) {
@@ -206,7 +207,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
} finally {
// remove watch on toc file.
if (deletedBook && !deletedBook.isNotebook) {
fs.unwatchFile(path.join(deletedBook.bookPath, '_data', 'toc.yml'));
fs.unwatchFile(deletedBook.tableOfContentsPath);
}
}
}
@@ -247,7 +248,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
const sectionToOpen = bookRoot.findChildSection(urlToOpen);
urlPath = sectionToOpen?.url;
} else {
urlPath = this.currentBook.bookItems[0].tableOfContents.sections[0].url;
urlPath = this.currentBook.version === BookVersion.v1 ? (this.currentBook.bookItems[0].tableOfContents.sections[0] as IJupyterBookSectionV1).url : (this.currentBook.bookItems[0].tableOfContents.sections[0] as IJupyterBookSectionV2).file;
}
}
if (urlPath) {
@@ -260,8 +261,8 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
} else {
// The Notebook editor expects a posix path for the resource (it will still resolve to the correct fsPath based on OS)
const sectionToOpenMarkdown: string = path.posix.join(this.currentBook.bookPath, Content, urlPath.concat('.md'));
const sectionToOpenNotebook: string = path.posix.join(this.currentBook.bookPath, Content, urlPath.concat('.ipynb'));
const sectionToOpenMarkdown: string = path.posix.join(this.currentBook.contentFolderPath, urlPath.concat('.md'));
const sectionToOpenNotebook: string = path.posix.join(this.currentBook.contentFolderPath, urlPath.concat('.ipynb'));
if (await fs.pathExists(sectionToOpenMarkdown)) {
this.openMarkdown(sectionToOpenMarkdown);
}
@@ -411,13 +412,22 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
public async searchJupyterBooks(treeItem?: BookTreeItem): Promise<void> {
let folderToSearch: string;
if (treeItem && treeItem.sections !== undefined) {
if (treeItem.uri) {
folderToSearch = path.join(treeItem.root, Content, path.dirname(treeItem.uri));
} else {
folderToSearch = path.join(treeItem.root, Content);
if (treeItem.book.version === BookVersion.v1) {
if (treeItem.uri) {
folderToSearch = path.join(treeItem.book.root, content, path.dirname(treeItem.uri));
} else {
folderToSearch = path.join(treeItem.root, content);
}
} else if (treeItem.book.version === BookVersion.v2) {
if (treeItem.uri) {
folderToSearch = path.join(treeItem.book.root, path.dirname(treeItem.uri));
} else {
folderToSearch = path.join(treeItem.root);
}
}
} else if (this.currentBook && !this.currentBook.isNotebook) {
folderToSearch = path.join(this.currentBook.bookPath, Content);
folderToSearch = path.join(this.currentBook.contentFolderPath);
} else {
vscode.window.showErrorMessage(loc.noBooksSelectedError);
}
@@ -476,6 +486,9 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
private async getNotebooksInTree(folderPath: string): Promise<BookSearchResults> {
let tocTrimLength: number;
let ignorePaths: string[] = [];
let notebookConfig = vscode.workspace.getConfiguration(constants.notebookConfigKey);
let maxDepth = notebookConfig[constants.maxBookSearchDepth];
// Use default value if user enters an invalid value
@@ -486,13 +499,26 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
}
let escapedPath = glob.escapePath(folderPath.replace(/\\/g, '/'));
let bookFilter = path.posix.join(escapedPath, '**', '_data', 'toc.yml');
let bookPaths = await glob(bookFilter, { deep: maxDepth });
let tocTrimLength = '/_data/toc.yml'.length * -1;
bookPaths = bookPaths.map(path => path.slice(0, tocTrimLength));
let bookV1Filter = path.posix.join(escapedPath, '**', '_data', 'toc.yml');
let bookV2Filter = path.posix.join(escapedPath, '**', '_toc.yml');
let bookPaths = await glob([bookV1Filter, bookV2Filter], { deep: maxDepth });
let ignoreNotebook: string[];
bookPaths = bookPaths.map(function (path) {
if (path.includes('/_data/toc.yml')) {
tocTrimLength = '/_data/toc.yml'.length * -1;
ignoreNotebook = ['/**/*.ipynb'];
} else {
tocTrimLength = '/_toc.yml'.length * -1;
ignoreNotebook = ['/**/*.ipynb', '/*.ipynb'];
}
path = path.slice(0, tocTrimLength);
ignoreNotebook.map(notebook => ignorePaths.push(glob.escapePath(path) + notebook));
return path;
});
let notebookFilter = path.posix.join(escapedPath, '**', '*.ipynb');
let notebookPaths = await glob(notebookFilter, { ignore: bookPaths.map(path => glob.escapePath(path) + '/**/*.ipynb'), deep: maxDepth });
let notebookPaths = await glob(notebookFilter, { ignore: ignorePaths, deep: maxDepth });
return { notebookPaths: notebookPaths, bookPaths: bookPaths };
}
@@ -512,7 +538,7 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
getChildren(element?: BookTreeItem): Thenable<BookTreeItem[]> {
if (element) {
if (element.sections) {
return Promise.resolve(this.currentBook.getSections(element.tableOfContents, element.sections, element.root).then(sections => { return sections; }));
return Promise.resolve(this.currentBook.getSections(element.tableOfContents, element.sections, element.root, element.book).then(sections => { return sections; }));
} else {
return Promise.resolve([]);
}
@@ -528,7 +554,8 @@ export class BookTreeViewProvider implements vscode.TreeDataProvider<BookTreeIte
getParent(element?: BookTreeItem): vscode.ProviderResult<BookTreeItem> {
if (element?.uri) {
let parentPath: string;
parentPath = path.join(element.root, Content, element.uri.substring(0, element.uri.lastIndexOf(path.posix.sep)));
let contentFolder = element.book.version === BookVersion.v1 ? path.join(element.book.root, content) : element.book.root;
parentPath = path.join(contentFolder, element.uri.substring(0, element.uri.lastIndexOf(path.posix.sep)));
if (parentPath === element.root) {
return undefined;
}