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

@@ -6,9 +6,12 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { IJupyterBookSection, IJupyterBookToc } from '../contracts/content';
import { JupyterBookSection, IJupyterBookToc, IJupyterBookSectionV2, IJupyterBookSectionV1 } from '../contracts/content';
import * as loc from '../common/localizedConstants';
import { isBookItemPinned } from '../common/utils';
import { BookVersion } from './bookModel';
const content = 'content';
export enum BookTreeItemType {
Book = 'Book',
@@ -26,13 +29,15 @@ export interface BookTreeItemFormat {
type: BookTreeItemType;
treeItemCollapsibleState: number;
isUntitled: boolean;
version?: string;
}
export class BookTreeItem extends vscode.TreeItem {
private _sections: IJupyterBookSection[];
private _sections: JupyterBookSection[];
private _uri: string | undefined;
private _previousUri: string;
private _nextUri: string;
public readonly version: string;
public command: vscode.Command;
constructor(public book: BookTreeItemFormat, icons: any) {
@@ -41,6 +46,7 @@ export class BookTreeItem extends vscode.TreeItem {
if (book.type === BookTreeItemType.Book) {
this.collapsibleState = book.treeItemCollapsibleState;
this._sections = book.page;
this.version = book.version;
if (book.isUntitled) {
this.contextValue = 'providedBook';
} else {
@@ -78,7 +84,7 @@ export class BookTreeItem extends vscode.TreeItem {
vscode.TreeItemCollapsibleState.Collapsed :
vscode.TreeItemCollapsibleState.None;
this._sections = this.book.page.sections || this.book.page.subsections;
this._uri = this.book.page.url;
this._uri = this.book.version === BookVersion.v1 ? this.book.page.url : this.book.page.file;
if (this.book.tableOfContents.sections) {
let index = (this.book.tableOfContents.sections.indexOf(this.book.page));
@@ -101,14 +107,18 @@ export class BookTreeItem extends vscode.TreeItem {
private setPreviousUri(index: number): void {
let i = --index;
while (i > -1) {
if (this.book.tableOfContents.sections[i].url) {
let pathToNotebook: string;
if (this.book.version === BookVersion.v2 && (this.book.tableOfContents.sections[i] as IJupyterBookSectionV2).file) {
// The Notebook editor expects a posix path for the resource (it will still resolve to the correct fsPath based on OS)
let pathToNotebook = path.posix.join(this.book.root, 'content', this.book.tableOfContents.sections[i].url.concat('.ipynb'));
// eslint-disable-next-line no-sync
if (fs.existsSync(pathToNotebook)) {
this._previousUri = pathToNotebook;
return;
}
pathToNotebook = path.posix.join(this.book.root, (this.book.tableOfContents.sections[i] as IJupyterBookSectionV2).file.concat('.ipynb'));
} else if ((this.book.tableOfContents.sections[i] as IJupyterBookSectionV1).url) {
pathToNotebook = path.posix.join(this.book.root, content, (this.book.tableOfContents.sections[i] as IJupyterBookSectionV1).url.concat('.ipynb'));
}
// eslint-disable-next-line no-sync
if (fs.existsSync(pathToNotebook)) {
this._previousUri = pathToNotebook;
return;
}
i--;
}
@@ -117,14 +127,18 @@ export class BookTreeItem extends vscode.TreeItem {
private setNextUri(index: number): void {
let i = ++index;
while (i < this.book.tableOfContents.sections.length) {
if (this.book.tableOfContents.sections[i].url) {
let pathToNotebook: string;
if (this.book.version === BookVersion.v2 && (this.book.tableOfContents.sections[i] as IJupyterBookSectionV2).file) {
// The Notebook editor expects a posix path for the resource (it will still resolve to the correct fsPath based on OS)
let pathToNotebook = path.posix.join(this.book.root, 'content', this.book.tableOfContents.sections[i].url.concat('.ipynb'));
// eslint-disable-next-line no-sync
if (fs.existsSync(pathToNotebook)) {
this._nextUri = pathToNotebook;
return;
}
pathToNotebook = path.posix.join(this.book.root, (this.book.tableOfContents.sections[i] as IJupyterBookSectionV2).file.concat('.ipynb'));
} else if ((this.book.tableOfContents.sections[i] as IJupyterBookSectionV1).url) {
pathToNotebook = path.posix.join(this.book.root, content, (this.book.tableOfContents.sections[i] as IJupyterBookSectionV1).url.concat('.ipynb'));
}
// eslint-disable-next-line no-sync
if (fs.existsSync(pathToNotebook)) {
this._nextUri = pathToNotebook;
return;
}
i++;
}
@@ -164,14 +178,14 @@ export class BookTreeItem extends vscode.TreeItem {
* Helper method to find a child section with a specified URL
* @param url The url of the section we're searching for
*/
public findChildSection(url?: string): IJupyterBookSection | undefined {
public findChildSection(url?: string): JupyterBookSection | undefined {
if (!url) {
return undefined;
}
return this.findChildSectionRecur(this, url);
return this.findChildSectionRecur(this as JupyterBookSection, url);
}
private findChildSectionRecur(section: IJupyterBookSection, url: string): IJupyterBookSection | undefined {
private findChildSectionRecur(section: JupyterBookSection, url: string): JupyterBookSection | undefined {
if (section.url && section.url === url) {
return section;
} else if (section.sections) {