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,7 +6,7 @@
},
"language_info": {
"name": "python",
"version": "3.7.2",
"version": "3.6.6",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
@@ -24,8 +24,13 @@
"cell_type": "markdown",
"source": [
"# Create Jupyter Book\n",
"This is a notebook to create a [Jupyter Book](https://jupyterbook.org/intro.html), which is an organized collections of Jupyter notebooks.\n",
"\n",
"<strong style=\"color: red; opacity: 0.80;\">Note: Jupyter Books in Azure Data Studio only support jupyter-book versions <= 0.6.4 To create a Jupyter Book, we will be uninstalling versions newer than 0.6.4 and replacing with 0.6.4</strong>\n",
" \n",
"\n",
"The best way to use this notebook is by clicking Run all in the toolbar above. This will run all the cells in a step-by-step order so that you can create your Jupyter Book.\n",
"\n",
" \n",
"## 1. Installation\n",
"\n",
"To install the Jupyter Book command-line interface (CLI), use `pip`!"
@@ -39,22 +44,44 @@
"source": [
"import sys\r\n",
"\r\n",
"#install jupyter-book 0.6.4\r\n",
"cmd = f'{sys.executable} -m pip show jupyter-book'\r\n",
"cmdOutput = !{cmd}\r\n",
"if len(cmdOutput) > 1 and '0.6.4' in cmdOutput[1]:\r\n",
" print('Jupyter-book required version is already installed!')\r\n",
"elif len(cmdOutput) > 1:\r\n",
" print('Unsupported version of Jupyter-book installed, Please wait while we uninstall and install the supported version.')\r\n",
" !pip uninstall jupyter-book --yes\r\n",
" !pip install jupyter-book==0.6.4\r\n",
"else:\r\n",
" print('Installing Jupyter-book...')\r\n",
" !pip install jupyter-book==0.6.4"
"def getJupyterBookVersion():\r\n",
" cmd = f'{sys.executable} -m pip show jupyter-book'\r\n",
" cmdOutput = !{cmd}\r\n",
" if len(cmdOutput) <= 1:\r\n",
" !pip install jupyter-book\r\n",
" cmd = f'{sys.executable} -m pip show jupyter-book'\r\n",
" cmdOutput = !{cmd}\r\n",
" for x in cmdOutput:\r\n",
" if 'version' in x.lower():\r\n",
" version_str = tuple(x.split(': ')[1].replace('.', ''))\r\n",
" version = tuple(int(x) for x in version_str)\r\n",
" return version, version_str\r\n",
"\r\n",
"try:\r\n",
" version, version_str = getJupyterBookVersion()\r\n",
" if version:\r\n",
" display_version = '.'.join(version_str)\r\n",
" print(f'Jupyter-book version {display_version} is already installed!')\r\n",
" use_current = input(f'Would you like to create a book using the current installed version [{display_version}] of jupyter-book? [y/n]').lower()\r\n",
"\r\n",
" while use_current not in ['yes', 'y', 'no', 'n']:\r\n",
" use_current = input(f'Would you like to create a book using the current installed version [{display_version}] of jupyter-book? [y/n]').lower()\r\n",
" if use_current in ['no', 'n']:\r\n",
" install_version = input(f'Please enter the version you would like to use (eg. 0.7.4)')\r\n",
" if install_version:\r\n",
" !pip uninstall jupyter-book --yes\r\n",
" version = ''\r\n",
" print('Installing Jupyter-book...')\r\n",
" !pip install jupyter-book==\"$install_version\"\r\n",
" version, _ = getJupyterBookVersion()\r\n",
"except Exception as e:\r\n",
" raise SystemExit(str(e))"
],
"metadata": {
"azdata_cell_guid": "8bd77173-2f63-4bf8-95e8-af2a654fc91e",
"tags": []
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
@@ -75,7 +102,11 @@
{
"cell_type": "code",
"source": [
"import os, re, shutil\r\n",
"import os, re, subprocess\r\n",
"from sys import platform\r\n",
"import shutil\r\n",
"\r\n",
"os.environ[\"LC_ALL\"]=\"en_US.UTF-8\"\r\n",
"\r\n",
"try:\r\n",
" overwrite = False\r\n",
@@ -91,17 +122,33 @@
"\r\n",
" while (not os.path.exists(content_folder)):\r\n",
" content_folder = input('Cannot find folder ' + content_folder + '. Please provide another path: ')\r\n",
" \r\n",
" if overwrite:\r\n",
" !jupyter-book create \"$book_name\" --content-folder \"$content_folder\" --overwrite\r\n",
" else:\r\n",
" !jupyter-book create \"$book_name\" --content-folder \"$content_folder\"\r\n",
" if version and version < (0,7,0):\r\n",
" if overwrite:\r\n",
" !jupyter-book create \"$book_name\" --content-folder \"$content_folder\" --overwrite\r\n",
" else:\r\n",
" !jupyter-book create \"$book_name\" --content-folder \"$content_folder\"\r\n",
" elif version:\r\n",
" if overwrite:\r\n",
" shutil.rmtree(book_name)\r\n",
" else:\r\n",
" # waiting for shutil to remove the directory\r\n",
" while os.path.exists(book_name):\r\n",
" pass\r\n",
" subprocess.check_call(['mkdir', book_name])\r\n",
" content_folder = os.path.join(content_folder,'')\r\n",
" if platform in ['linux', 'darwin']:\r\n",
" subprocess.check_call(['cp', '-r',content_folder, book_name])\r\n",
" else:\r\n",
" subprocess.check_call(['xcopy', content_folder, book_name])\r\n",
" !jupyter-book toc \"$book_name\"\r\n",
"except Exception as e:\r\n",
" raise SystemExit(str(e))"
],
"metadata": {
"azdata_cell_guid": "d1a363f0-d854-4466-be87-d01d4c7e51ef",
"tags": []
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
@@ -123,22 +170,9 @@
"cell_type": "code",
"source": [
"# Update toc file, book title and clean up the directores\n",
"\n",
"import shutil\n",
"from os import path\n",
"tocFilePath = path.join(book_name, \"_data\", \"toc.yml\")\n",
"f = open(tocFilePath, \"r\")\n",
"title = ''\n",
"replacedString = ''\n",
"result = f.read()\n",
"f.close()\n",
"contentFolders = []\n",
"\n",
"firstLevelUrls = re.findall(r'^(?:\\s+$[\\r\\n]+)+(\\- url: [a-zA-Z0-9\\\\.\\s\\-\\/]+$[\\r\\n]+)', result, re.MULTILINE)\n",
"urls = re.findall(r'- url: [a-zA-Z0-9\\\\.\\s\\-\\/]+$', result, re.MULTILINE)\n",
"headers = re.findall(r'- header: [a-zA-Z0-9\\\\.\\s-]+$', result, re.MULTILINE)\n",
"# all the markdown urls are placed at the end of the list. \n",
"possibleMarkdowns = re.findall(r'(- url: [a-zA-Z0-9\\\\.\\s\\-\\/]+$[\\r\\n]+)[\\r\\n]', result, re.MULTILINE)\n",
"\n",
"import yaml\n",
"def getMarkdownFile(url):\n",
" # url are usually defined in toc as: \"- url: <Path\\to\\notebookFile>\"\n",
" # substring from 7th postion excluding the \"- url: \" from the path\n",
@@ -151,106 +185,164 @@
" markdownFilePath = url[7:].rstrip() + '.md'\n",
" return markdownFilePath\n",
"\n",
"try:\n",
" if (firstLevelUrls or headers or urls):\n",
" if (firstLevelUrls and len(firstLevelUrls) == 1):\n",
" for url in firstLevelUrls:\n",
" # check first link is the markdown\n",
" title = url[url.rindex(path.sep)+1:].rstrip()\n",
" markdownFilePath = getMarkdownFile(url)\n",
" rootmarkdownExists = path.exists(markdownFilePath)\n",
" markdownUrl = urls[len(urls) -1]\n",
" if (rootmarkdownExists):\n",
" markdownUrl = url\n",
" replacedString = \"\\n- title: %s\\n url: /%s\\n not_numbered: true\\n\" % (title, title)\n",
" # else check if the last link in the url list is markdown \n",
" elif (not headers and path.exists(getMarkdownFile(markdownUrl))):\n",
" rootmarkdownExists = True\n",
" title = markdownUrl[markdownUrl.rindex(path.sep)+1:].rstrip()\n",
" replacedString = \"\\n- title: %s\\n url: /%s\\n not_numbered: true\\n expand_sections: true\\n sections: %s\" % (title, title, url)\n",
" result = result.replace(markdownUrl, '')\n",
" # if there not markdowns and folders contains markdowns, handle them\n",
" elif (possibleMarkdowns):\n",
" markdownUrl = possibleMarkdowns[0]\n",
" title = markdownUrl[markdownUrl.rindex(path.sep)+1:].rstrip()\n",
" markdownFilePath = getMarkdownFile(markdownUrl)\n",
"def getAllSections(visited, data, current):\n",
" if current not in visited:\n",
" visited.append(current)\n",
" for x in range(len(current)):\n",
" if 'sections' in current[x].keys():\n",
" tmp = {}\n",
" tmp['title'] = path.dirname(current[x]['file']).title()\n",
" tmp['file'] = current[x]['file']\n",
" tmp['numbered'] = False\n",
" tmp['expand_sections'] = True\n",
" tmp['sections'] = current[x]['sections']\n",
" current[x] = tmp\n",
" getAllSections(visited, data, current[x]['sections'])\n",
" else:\n",
" current[x]['title'] = path.basename(current[x]['file']).title()\n",
"\n",
"if version < (0,7,0):\n",
" tocFilePath = path.join(book_name, \"_data\", \"toc.yml\")\n",
" f = open(tocFilePath, \"r\")\n",
" title = ''\n",
" replacedString = ''\n",
" result = f.read()\n",
" f.close()\n",
" contentFolders = []\n",
"\n",
" firstLevelUrls = re.findall(r'^(?:\\s+$[\\r\\n]+)+(\\- url: [a-zA-Z0-9\\\\.\\s\\-\\/]+$[\\r\\n]+)', result, re.MULTILINE)\n",
" urls = re.findall(r'- url: [a-zA-Z0-9\\\\.\\s\\-\\/]+$', result, re.MULTILINE)\n",
" headers = re.findall(r'- header: [a-zA-Z0-9\\\\.\\s-]+$', result, re.MULTILINE)\n",
" # all the markdown urls are placed at the end of the list. \n",
" possibleMarkdowns = re.findall(r'(- url: [a-zA-Z0-9\\\\.\\s\\-\\/]+$[\\r\\n]+)[\\r\\n]', result, re.MULTILINE)\n",
"\n",
" try:\n",
" if (firstLevelUrls or headers or urls):\n",
" if (firstLevelUrls and len(firstLevelUrls) == 1):\n",
" for url in firstLevelUrls:\n",
" # check first link is the markdown\n",
" title = url[url.rindex(path.sep)+1:].rstrip()\n",
" markdownFilePath = getMarkdownFile(url)\n",
" rootmarkdownExists = path.exists(markdownFilePath)\n",
" markdownUrl = urls[len(urls) -1]\n",
" if (rootmarkdownExists):\n",
" markdownUrl = url\n",
" replacedString = \"\\n- title: %s\\n url: /%s\\n not_numbered: true\\n\" % (title, title)\n",
" # else check if the last link in the url list is markdown \n",
" elif (not headers and path.exists(getMarkdownFile(markdownUrl))):\n",
" rootmarkdownExists = True\n",
" title = markdownUrl[markdownUrl.rindex(path.sep)+1:].rstrip()\n",
" replacedString = \"\\n- title: %s\\n url: /%s\\n not_numbered: true\\n expand_sections: true\\n sections: %s\" % (title, title, url)\n",
" result = result.replace(markdownUrl, '')\n",
" # there is no markdown and we're adding the first link as is\n",
" else:\n",
" markdownUrl = url\n",
" replacedString = \"\\n- title: %s\\n url: /%s\\n not_numbered: true\\n\" % (title, title)\n",
" result = result.replace(url, replacedString)\n",
" # Folders and handling them -> each header is a folder\n",
" if (headers):\n",
" for header in headers:\n",
" title = header[10:].rstrip()\n",
" # filters all the urls with /headerName in them\n",
" filtered = list(filter(lambda x: (\"%s%s%s\" % (path.sep, title.lower(), path.sep)) in x.lower(), urls))\n",
" index = urls.index(filtered[len(filtered)-1])\n",
" markdownFilePath = getMarkdownFile(filtered[len(filtered)-1])\n",
" markdownExists = path.exists(markdownFilePath)\n",
" if (not markdownExists):\n",
" index = urls.index(filtered[0])\n",
" folderEndIndex = urls[index].rindex(path.sep)\n",
" caseSensitiveFolderName = urls[index][urls[index].rindex(path.sep, 0, folderEndIndex)+1:folderEndIndex]\n",
" contentFolders.append(caseSensitiveFolderName)\n",
" urlValue = urls[index][urls[index].rindex(path.sep)+1:].rstrip()\n",
" replacedString = \"\\n- title: %s\\n url: /%s/%s\\n%s expand_sections: true\\n sections: \" % (title, caseSensitiveFolderName, urlValue, ' not_numbered: true\\n' if markdownExists else '')\n",
" result = result.replace(header, replacedString)\n",
" if (markdownExists):\n",
" result = result.replace(urls[index], '')\n",
" del urls[index]\n",
" if (urls):\n",
" # if there not markdowns and folders contains markdowns, handle them\n",
" elif (possibleMarkdowns):\n",
" markdownUrl = possibleMarkdowns[0]\n",
" title = markdownUrl[markdownUrl.rindex(path.sep)+1:].rstrip()\n",
" markdownFilePath = getMarkdownFile(markdownUrl)\n",
" rootmarkdownExists = path.exists(markdownFilePath)\n",
" if (rootmarkdownExists):\n",
" replacedString = \"\\n- title: %s\\n url: /%s\\n not_numbered: true\\n expand_sections: true\\n sections: %s\" % (title, title, url)\n",
" result = result.replace(markdownUrl, '')\n",
" # there is no markdown and we're adding the first link as is\n",
" else:\n",
" markdownUrl = url\n",
" replacedString = \"\\n- title: %s\\n url: /%s\\n not_numbered: true\\n\" % (title, title)\n",
" result = result.replace(url, replacedString)\n",
" # Folders and handling them -> each header is a folder\n",
" if (headers):\n",
" for header in headers:\n",
" title = header[10:].rstrip()\n",
" # filters all the urls with /headerName in them\n",
" filtered = list(filter(lambda x: (\"%s%s%s\" % (path.sep, title.lower(), path.sep)) in x.lower(), urls))\n",
" index = urls.index(filtered[len(filtered)-1])\n",
" markdownFilePath = getMarkdownFile(filtered[len(filtered)-1])\n",
" markdownExists = path.exists(markdownFilePath)\n",
" if (not markdownExists):\n",
" index = urls.index(filtered[0])\n",
" folderEndIndex = urls[index].rindex(path.sep)\n",
" caseSensitiveFolderName = urls[index][urls[index].rindex(path.sep, 0, folderEndIndex)+1:folderEndIndex]\n",
" contentFolders.append(caseSensitiveFolderName)\n",
" urlValue = urls[index][urls[index].rindex(path.sep)+1:].rstrip()\n",
" replacedString = \"\\n- title: %s\\n url: /%s/%s\\n%s expand_sections: true\\n sections: \" % (title, caseSensitiveFolderName, urlValue, ' not_numbered: true\\n' if markdownExists else '')\n",
" result = result.replace(header, replacedString)\n",
" if (markdownExists):\n",
" result = result.replace(urls[index], '')\n",
" del urls[index]\n",
" if (urls):\n",
" for url in urls:\n",
" title = url[url.rindex(path.sep)+1:].rstrip()\n",
" urlValue = title\n",
" if (len(contentFolders) > 0):\n",
" folders = url[7:].split(path.sep)\n",
" if (folders[len(folders)-2] in contentFolders):\n",
" parentFolder = contentFolders.index(folders[len(folders)-2])\n",
" urlValue = \"%s/%s\" % (contentFolders[parentFolder], title)\n",
" replacedString = \"\\n - title: %s\\n url: /%s\" % (title, urlValue)\n",
" else:\n",
" replacedString = \"\\n - title: %s\\n url: /%s\" % (title, urlValue) if rootmarkdownExists else \"\\n- title: %s\\n url: /%s\" % (title, urlValue)\n",
" result = result.replace(url, replacedString)\n",
" fwrite = open(tocFilePath, \"w\")\n",
" fwrite.write(result)\n",
" fwrite.close()\n",
" # formattinf any left over urls in the file\n",
" elif (urls):\n",
" for url in urls:\n",
" title = url[url.rindex(path.sep)+1:].rstrip()\n",
" urlValue = title\n",
" if (len(contentFolders) > 0):\n",
" folders = url[7:].split(path.sep)\n",
" if (folders[len(folders)-2] in contentFolders):\n",
" parentFolder = contentFolders.index(folders[len(folders)-2])\n",
" urlValue = \"%s/%s\" % (contentFolders[parentFolder], title)\n",
" replacedString = \"\\n - title: %s\\n url: /%s\" % (title, urlValue)\n",
" else:\n",
" replacedString = \"\\n - title: %s\\n url: /%s\" % (title, urlValue) if rootmarkdownExists else \"\\n- title: %s\\n url: /%s\" % (title, urlValue)\n",
" replacedString = \"\\n - title: %s\\n url: /%s\" % (title, urlValue) if rootmarkdownExists else \"\\n- title: %s\\n url: /%s\" % (title, urlValue)\n",
" result = result.replace(url, replacedString)\n",
" fwrite = open(tocFilePath, \"w\")\n",
" fwrite.write(result)\n",
" fwrite.close()\n",
" # formattinf any left over urls in the file\n",
" elif (urls):\n",
" for url in urls:\n",
" title = url[url.rindex(path.sep)+1:].rstrip()\n",
" urlValue = title\n",
" replacedString = \"\\n - title: %s\\n url: /%s\" % (title, urlValue) if rootmarkdownExists else \"\\n- title: %s\\n url: /%s\" % (title, urlValue)\n",
" result = result.replace(url, replacedString)\n",
" fwrite = open(tocFilePath, \"w\")\n",
" fwrite = open(tocFilePath, \"w\")\n",
" fwrite.write(result)\n",
" fwrite.close()\n",
" else:\n",
" raise SystemExit(f'\\n File Name contains unsupported-characters (ex: underscores) by Jupyter Book.\\n')\n",
" # Update the Book title in config file\n",
" configFilePath = path.join(book_name, \"_config.yml\")\n",
" f = open(configFilePath, \"r\")\n",
" result = f.read()\n",
" f.close()\n",
" titleLine = re.search(r'title: [a-zA-Z0-9\\\\.\\s\\-\\/]+$', result, re.MULTILINE).group()\n",
" title = 'title: %s' % (path.splitext(path.basename(book_name))[0])\n",
" result = result.replace(titleLine, title)\n",
" fwrite = open(configFilePath, \"w\")\n",
" fwrite.write(result)\n",
" fwrite.close()\n",
" else:\n",
" raise SystemExit(f'\\n File Name contains unsupported-characters (ex: underscores) by Jupyter Book.\\n')\n",
" # Update the Book title in config file\n",
" # cleanup the directories\n",
" with os.scandir(book_name) as root_dir:\n",
" for path in root_dir:\n",
" if path.is_file() and path.name not in ('_config.yml'):\n",
" os.remove(path)\n",
" if path.is_dir() and path.name not in ('_data', 'content'):\n",
" shutil.rmtree(path)\n",
" except Exception as e:\n",
" raise SystemExit(str(e))\n",
"else:\n",
" tocFilePath = path.join(book_name, \"_toc.yml\")\n",
" configFilePath = path.join(book_name, \"_config.yml\")\n",
" f = open(configFilePath, \"r\")\n",
" result = f.read()\n",
" f.close()\n",
" titleLine = re.search(r'title: [a-zA-Z0-9\\\\.\\s\\-\\/]+$', result, re.MULTILINE).group()\n",
" title = 'title: %s' % (path.splitext(path.basename(book_name))[0])\n",
" result = result.replace(titleLine, title)\n",
" fwrite = open(configFilePath, \"w\")\n",
" fwrite.write(result)\n",
" fwrite.close()\n",
" # cleanup the directories\n",
" with os.scandir(book_name) as root_dir:\n",
" for path in root_dir:\n",
" if path.is_file() and path.name not in ('_config.yml'):\n",
" os.remove(path)\n",
" if path.is_dir() and path.name not in ('_data', 'content'):\n",
" shutil.rmtree(path)\n",
"except Exception as e:\n",
" raise SystemExit(str(e))"
" visited = []\n",
" # modify generated toc file\n",
" with open(tocFilePath, 'r') as stream:\n",
" data = yaml.safe_load(stream)\n",
" if not isinstance(data, list):\n",
" new_data = []\n",
" for k in data:\n",
" if k == 'file':\n",
" new_data.append({k : data[k]})\n",
" elif k == 'sections':\n",
" new_data[-1].update({k : data[k]})\n",
" data = new_data\n",
" for k in data:\n",
" if 'sections' in k:\n",
" getAllSections([], data, k['sections'])\n",
"\n",
" with open(tocFilePath, 'w') as outfile:\n",
" yaml.dump(data, outfile, sort_keys=False)\n",
" # create config file\n",
" config = {}\n",
" config['title'] = path.basename(book_name).title()\n",
" with open(configFilePath, 'w') as output:\n",
" yaml.dump(config, output, default_flow_style=False)\n",
""
],
"metadata": {
"azdata_cell_guid": "6124730b-f52e-4103-8dbb-e3a62325fb55",
@@ -284,7 +376,9 @@
],
"metadata": {
"azdata_cell_guid": "33d8e1cb-1eec-41ed-a368-1aeef9af62d4",
"tags": []
"tags": [
"hide_input"
]
},
"outputs": [],
"execution_count": null
@@ -294,9 +388,14 @@
"source": [
"<span style=\"color:red\">**Note**: On clicking the above link, we create a temporary toc.yml file for your convenience.</span>\r\n",
"\r\n",
"**For versions < 0.7.0**\r\n",
"\r\n",
" Please update that file inside your book (located at: *YourbookPath*/_data/toc.yml) if you want to further customize your book following \r\n",
" instructions at https://jupyterbook.org/guide/01-5_tour.html#Table-of-Contents.\r\n",
""
" instructions at https://legacy.jupyterbook.org/guide/01-5_tour.html#Table-of-Contents.\r\n",
"\r\n",
"**For the newer versions**\r\n",
"\r\n",
"Please update the _toc.yml file inside your book (located at: YourbookPath/_toc.yml). And refer to the documentation at https://jupyterbook.org/customize/toc.html for further customization."
],
"metadata": {
"azdata_cell_guid": "d193d588-847b-4725-9591-098d0fb24343"

View File

@@ -5,22 +5,30 @@
import * as vscode from 'vscode';
import * as yaml from 'js-yaml';
import { BookTreeItem, BookTreeItemType } from './bookTreeItem';
import { BookTreeItem, BookTreeItemType, BookTreeItemFormat } from './bookTreeItem';
import * as constants from '../common/constants';
import * as path from 'path';
import * as fileServices from 'fs';
import * as fs from 'fs-extra';
import * as loc from '../common/localizedConstants';
import { IJupyterBookToc, IJupyterBookSection } from '../contracts/content';
import { IJupyterBookToc, JupyterBookSection, IJupyterBookSectionV2, IJupyterBookSectionV1 } from '../contracts/content';
const fsPromises = fileServices.promises;
const content = 'content';
export enum BookVersion {
v1 = 'v1',
v2 = 'v2'
}
export class BookModel {
private _bookItems: BookTreeItem[];
private _allNotebooks = new Map<string, BookTreeItem>();
private _tableOfContentsPath: string;
private _contentFolderPath: string;
private _configPath: string;
private _bookVersion: BookVersion;
private _rootPath: string;
private _errorMessage: string;
constructor(
@@ -37,11 +45,38 @@ export class BookModel {
if (this.isNotebook) {
this.readNotebook();
} else {
await this.loadTableOfContentFiles(this.bookPath);
await this.readBookStructure(this.bookPath);
await this.loadTableOfContentFiles();
await this.readBooks();
}
}
public async readBookStructure(folderPath: string): Promise<void> {
// check book structure to determine version
let isOlderVersion: boolean;
this._configPath = path.posix.join(folderPath, '_config.yml');
try {
isOlderVersion = (await fs.stat(path.posix.join(folderPath, '_data'))).isDirectory() && (await fs.stat(path.posix.join(folderPath, content))).isDirectory();
} catch {
isOlderVersion = false;
}
if (isOlderVersion) {
let isTocFile = (await fs.stat(path.posix.join(folderPath, '_data', 'toc.yml'))).isFile();
if (isTocFile) {
this._tableOfContentsPath = path.posix.join(folderPath, '_data', 'toc.yml');
}
this._bookVersion = BookVersion.v1;
this._contentFolderPath = path.posix.join(folderPath, content, '');
this._rootPath = path.dirname(path.dirname(this._tableOfContentsPath));
} else {
this._contentFolderPath = folderPath;
this._tableOfContentsPath = path.posix.join(folderPath, '_toc.yml');
this._rootPath = path.dirname(this._tableOfContentsPath);
this._bookVersion = BookVersion.v2;
}
}
public getAllNotebooks(): Map<string, BookTreeItem> {
return this._allNotebooks;
}
@@ -50,14 +85,12 @@ export class BookModel {
return this._allNotebooks.get(this.openAsUntitled ? path.basename(uri) : uri);
}
public async loadTableOfContentFiles(folderPath: string): Promise<void> {
public async loadTableOfContentFiles(): Promise<void> {
if (this.isNotebook) {
return;
}
let tableOfContentsPath: string = path.posix.join(folderPath, '_data', 'toc.yml');
if (await fs.pathExists(tableOfContentsPath)) {
this._tableOfContentsPath = tableOfContentsPath;
if (await fs.pathExists(this._tableOfContentsPath)) {
vscode.commands.executeCommand('setContext', 'bookOpened', true);
} else {
this._errorMessage = loc.missingTocError;
@@ -111,16 +144,16 @@ export class BookModel {
}
if (this._tableOfContentsPath) {
let root: string = path.dirname(path.dirname(this._tableOfContentsPath));
try {
let fileContents = await fsPromises.readFile(path.join(root, '_config.yml'), 'utf-8');
let fileContents = await fsPromises.readFile(this._configPath, 'utf-8');
const config = yaml.safeLoad(fileContents.toString());
fileContents = await fsPromises.readFile(this._tableOfContentsPath, 'utf-8');
const tableOfContents: any = yaml.safeLoad(fileContents.toString());
let tableOfContents: any = yaml.safeLoad(fileContents.toString());
let book: BookTreeItem = new BookTreeItem({
version: this._bookVersion,
title: config.title,
contentPath: this._tableOfContentsPath,
root: root,
root: this._rootPath,
tableOfContents: { sections: this.parseJupyterSections(tableOfContents) },
page: tableOfContents,
type: BookTreeItemType.Book,
@@ -145,11 +178,11 @@ export class BookModel {
return this._bookItems;
}
public async getSections(tableOfContents: IJupyterBookToc, sections: IJupyterBookSection[], root: string): Promise<BookTreeItem[]> {
public async getSections(tableOfContents: IJupyterBookToc, sections: JupyterBookSection[], root: string, book: BookTreeItemFormat): Promise<BookTreeItem[]> {
let notebooks: BookTreeItem[] = [];
for (let i = 0; i < sections.length; i++) {
if (sections[i].url) {
if (sections[i].external) {
if (sections[i].url || (sections[i] as IJupyterBookSectionV2).file) {
if (sections[i].url && ((sections[i] as IJupyterBookSectionV1).external || book.version === BookVersion.v2)) {
let externalLink: BookTreeItem = new BookTreeItem({
title: sections[i].title,
contentPath: undefined,
@@ -158,7 +191,8 @@ export class BookModel {
page: sections[i],
type: BookTreeItemType.ExternalLink,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
isUntitled: this.openAsUntitled
isUntitled: this.openAsUntitled,
version: book.version
},
{
light: this._extensionContext.asAbsolutePath('resources/light/link.svg'),
@@ -168,20 +202,29 @@ export class BookModel {
notebooks.push(externalLink);
} else {
let pathToNotebook = path.join(root, 'content', sections[i].url.concat('.ipynb'));
let pathToMarkdown = path.join(root, 'content', sections[i].url.concat('.md'));
let pathToNotebook: string;
let pathToMarkdown: string;
if (book.version === BookVersion.v2) {
pathToNotebook = path.join(book.root, (sections[i] as IJupyterBookSectionV2).file.concat('.ipynb'));
pathToMarkdown = path.join(book.root, (sections[i] as IJupyterBookSectionV2).file.concat('.md'));
} else if (sections[i].url) {
pathToNotebook = path.join(book.root, content, (sections[i] as IJupyterBookSectionV1).url.concat('.ipynb'));
pathToMarkdown = path.join(book.root, content, (sections[i] as IJupyterBookSectionV1).url.concat('.md'));
}
// Note: Currently, if there is an ipynb and a md file with the same name, Jupyter Books only shows the notebook.
// Following Jupyter Books behavior for now
if (await fs.pathExists(pathToNotebook)) {
let notebook = new BookTreeItem({
title: sections[i].title,
title: sections[i].title ? sections[i].title : (sections[i] as IJupyterBookSectionV2).file,
contentPath: pathToNotebook,
root: root,
tableOfContents: tableOfContents,
page: sections[i],
type: BookTreeItemType.Notebook,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
isUntitled: this.openAsUntitled
isUntitled: this.openAsUntitled,
version: book.version
},
{
light: this._extensionContext.asAbsolutePath('resources/light/notebook.svg'),
@@ -204,14 +247,15 @@ export class BookModel {
}
} else if (await fs.pathExists(pathToMarkdown)) {
let markdown: BookTreeItem = new BookTreeItem({
title: sections[i].title,
title: sections[i].title ? sections[i].title : (sections[i] as IJupyterBookSectionV2).file,
contentPath: pathToMarkdown,
root: root,
tableOfContents: tableOfContents,
page: sections[i],
type: BookTreeItemType.Markdown,
treeItemCollapsibleState: vscode.TreeItemCollapsibleState.Collapsed,
isUntitled: this.openAsUntitled
isUntitled: this.openAsUntitled,
version: book.version
},
{
light: this._extensionContext.asAbsolutePath('resources/light/markdown.svg'),
@@ -235,7 +279,7 @@ export class BookModel {
* Recursively parses out a section of a Jupyter Book.
* @param section The input data to parse
*/
private parseJupyterSections(section: any[]): IJupyterBookSection[] {
private parseJupyterSections(section: any[]): JupyterBookSection[] {
try {
return section.reduce((acc, val) => Array.isArray(val.sections) ?
acc.concat(val).concat(this.parseJupyterSections(val.sections)) : acc.concat(val), []);
@@ -246,15 +290,25 @@ export class BookModel {
}
throw this._errorMessage;
}
}
public get tableOfContentsPath(): string {
return this._tableOfContentsPath;
}
public get contentFolderPath(): string {
return this._contentFolderPath;
}
public get configPath(): string {
return this._configPath;
}
public get errorMessage(): string {
return this._errorMessage;
}
public get version(): string {
return this._bookVersion;
}
}

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) {

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;
}

View File

@@ -64,7 +64,7 @@ export type OutputType =
| 'update_display_data';
export interface IJupyterBookToc {
sections: IJupyterBookSection[];
sections: JupyterBookSection[];
}
/**
@@ -73,7 +73,7 @@ export interface IJupyterBookToc {
* This is taken from https://github.com/jupyter/jupyter-book/blob/master/jupyter_book/book_template/_data/toc.yml but is not
* enforced so invalid JSON may result in expected values being undefined.
*/
export interface IJupyterBookSection {
export interface IJupyterBookSectionV1 {
/**
* Title of chapter or section
*/
@@ -85,7 +85,7 @@ export interface IJupyterBookSection {
/**
* Contains a list of more entries that make up the chapter's/section's sub-sections
*/
sections?: IJupyterBookSection[];
sections?: IJupyterBookSectionV1[];
/**
* If the section shouldn't have a number in the sidebar
*/
@@ -114,3 +114,40 @@ export interface IJupyterBookSection {
*/
header?: boolean;
}
/**
* A section of a Jupyter book.
*
* This is taken from https://github.com/jupyter/jupyter-book/blob/master/jupyter_book/book_template/_toc.yml but is not
* enforced so invalid JSON may result in expected values being undefined.
*/
export interface IJupyterBookSectionV2 {
/**
* Title of chapter or section
*/
title?: string;
/**
* Path to notebook relative to root folder.
*/
file?: string;
/**
* Contains a list of more entries that make up the chapter's/section's sub-sections
*/
sections?: IJupyterBookSectionV2[];
/**
* If the section shouldn't have a number in the sidebar
*/
numbered?: boolean;
/**
* If you'd like the sections of this chapter to always be expanded in the sidebar.
*/
expand_sections?: boolean;
/**
* External link
*/
url?: string;
}
// type that supports new and old version
export type JupyterBookSection = IJupyterBookSectionV1 | IJupyterBookSectionV2;

View File

@@ -47,7 +47,6 @@ export function equalBookItems(book: BookTreeItem, expectedBook: IExpectedBookIt
describe('BooksTreeViewTests', function () {
describe('BookTreeViewProvider', () => {
let mockExtensionContext: vscode.ExtensionContext;
let appContext: AppContext;
let nonBookFolderPath: string;
@@ -418,405 +417,552 @@ describe('BooksTreeViewTests', function () {
});
describe('BookTreeViewProvider.getTableOfContentFiles', function (): void {
let rootFolderPath: string;
let tableOfContentsFile: string;
describe('BookTreeViewProvider.getTableOfContentFiles', function() {
let rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let folder: vscode.WorkspaceFolder;
this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let dataFolderPath: string = path.join(rootFolderPath, '_data');
tableOfContentsFile = path.join(dataFolderPath, 'toc.yml');
let tableOfContentsFileIgnore: string = path.join(rootFolderPath, 'toc.yml');
await fs.mkdir(rootFolderPath);
await fs.mkdir(dataFolderPath);
await fs.writeFile(tableOfContentsFile, '- title: Notebook1\n url: /notebook1\n sections:\n - title: Notebook2\n url: /notebook2\n - title: Notebook3\n url: /notebook3\n- title: Markdown\n url: /markdown\n- title: GitHub\n url: https://github.com/\n external: true');
await fs.writeFile(tableOfContentsFileIgnore, '');
const mockExtensionContext = new MockExtensionContext();
folder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
it('should ignore toc.yml files not in _data folder', async () => {
await bookTreeViewProvider.currentBook.loadTableOfContentFiles(rootFolderPath);
let path = bookTreeViewProvider.currentBook.tableOfContentsPath;
should(vscode.Uri.file(path).fsPath).equal(vscode.Uri.file(tableOfContentsFile).fsPath);
});
this.afterAll(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
let runs = [
{
it: 'v1',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'content'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableOfContentsFile' : path.join(rootFolderPath,'_data', 'toc.yml'),
'notebook2File' : path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
'tableOfContentsFileIgnore' : path.join(rootFolderPath, 'toc.yml')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n url: /notebook1\n sections:\n - title: Notebook2\n url: /notebook2\n - title: Notebook3\n url: /notebook3\n- title: Markdown\n url: /markdown\n- title: GitHub\n url: https://github.com/\n external: true'
}
},
{
it: 'v2',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableOfContentsFile' : path.join(rootFolderPath, '_toc.yml'),
'notebook2File' : path.join(rootFolderPath,'notebook2.ipynb'),
'tableOfContentsFileIgnore' : path.join(rootFolderPath, '_data', 'toc.yml')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n file: /notebook1\n sections:\n - title: Notebook2\n file: /notebook2\n - title: Notebook3\n file: /notebook3\n- title: Markdown\n file: /markdown\n- title: GitHub\n url: https://github.com/\n'
}
}
];
runs.forEach(function (run){
describe('BookTreeViewProvider.getTableOfContentFiles on ' + run.it, function (): void {
let folder: vscode.WorkspaceFolder;
before(async () => {
await fs.mkdir(rootFolderPath);
await fs.mkdir(run.folderPaths.dataFolderPath);
if(run.it === 'v1') {
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.writeFile(run.folderPaths.tableOfContentsFile, run.contents.toc);
await fs.writeFile(run.folderPaths.tableOfContentsFileIgnore, '');
await fs.writeFile(run.folderPaths.notebook2File, '');
const mockExtensionContext = new MockExtensionContext();
folder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
if(run.it === 'v1') {
it('should ignore toc.yml files not in _data folder', async () => {
await bookTreeViewProvider.currentBook.readBookStructure(rootFolderPath);
await bookTreeViewProvider.currentBook.loadTableOfContentFiles();
let path = bookTreeViewProvider.currentBook.tableOfContentsPath;
should(vscode.Uri.file(path).fsPath).equal(vscode.Uri.file(run.folderPaths.tableOfContentsFile).fsPath);
});
} else if (run.it === 'v2'){
it('should ignore toc.yml files not under the root book folder', async () => {
await bookTreeViewProvider.currentBook.readBookStructure(rootFolderPath);
await bookTreeViewProvider.currentBook.loadTableOfContentFiles();
let path = bookTreeViewProvider.currentBook.tableOfContentsPath;
should(vscode.Uri.file(path).fsPath).equal(vscode.Uri.file(run.folderPaths.tableOfContentsFile).fsPath);
});
}
after(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
}
});
});
});
});
describe('BookTreeViewProvider.getBooks', function (): void {
let rootFolderPath: string;
let configFile: string;
let folder: vscode.WorkspaceFolder;
describe('BookTreeViewProvider.getBooks', function() {
let rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let tocFile: string;
this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let dataFolderPath: string = path.join(rootFolderPath, '_data');
configFile = path.join(rootFolderPath, '_config.yml');
tocFile = path.join(dataFolderPath, 'toc.yml');
await fs.mkdir(rootFolderPath);
await fs.mkdir(dataFolderPath);
await fs.writeFile(tocFile, 'title: Test');
const mockExtensionContext = new MockExtensionContext();
folder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
it('should show error message if config.yml file not found', async () => {
await bookTreeViewProvider.currentBook.readBooks();
should(bookTreeViewProvider.currentBook.errorMessage).equal(readBookError(bookTreeViewProvider.currentBook.bookPath, `ENOENT: no such file or directory, open '${configFile}'`));
});
it('should show error if toc.yml file format is invalid', async function (): Promise<void> {
await fs.writeFile(configFile, 'title: Test Book');
await bookTreeViewProvider.currentBook.readBooks();
should(bookTreeViewProvider.currentBook.errorMessage).equal(readBookError(bookTreeViewProvider.currentBook.bookPath, `Invalid toc file`));
});
this.afterAll(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
let runs = [
{
it: 'v1',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'content'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath,'_data', 'toc.yml'),
'notebook2File' : path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
}
}, {
it: 'v2',
folderPaths : {
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath, '_toc.yml'),
'notebook2File' : path.join(rootFolderPath, 'notebook2.ipynb'),
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
}
}
];
runs.forEach(function (run){
describe('BookTreeViewProvider.getBooks on ' + run.it, function (): void {
let folder: vscode.WorkspaceFolder;
before(async () => {
await fs.mkdir(rootFolderPath);
if(run.it === 'v1'){
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.writeFile(run.folderPaths.tableofContentsFile, run.contents.config);
const mockExtensionContext = new MockExtensionContext();
folder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
it('should show error message if config.yml file not found', async () => {
await bookTreeViewProvider.currentBook.readBooks();
should(bookTreeViewProvider.currentBook.errorMessage).equal(readBookError(bookTreeViewProvider.currentBook.bookPath, `ENOENT: no such file or directory, open '${run.folderPaths.configFile}'`));
});
it('should show error if toc.yml file format is invalid', async function (): Promise<void> {
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
await bookTreeViewProvider.currentBook.readBooks();
should(bookTreeViewProvider.currentBook.errorMessage).equal(readBookError(bookTreeViewProvider.currentBook.bookPath, `Invalid toc file`));
});
after(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
}
});
});
})});
describe('BookTreeViewProvider.getSections', function() {
let rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let runs = [
{
it: 'v1',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'content'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath,'_data', 'toc.yml'),
'notebook2File' : path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
}
},{
it: 'v2',
folderPaths : {
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath,'_toc.yml'),
'notebook2File' : path.join(rootFolderPath,'notebook2.ipynb'),
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
}
}
];
runs.forEach(function (run){
describe('BookTreeViewProvider.getSections on ' + run.it, function (): void {
let folder: vscode.WorkspaceFolder[];
let expectedNotebook2: IExpectedBookItem;
before(async () => {
expectedNotebook2 = {
title: 'Notebook2',
url: '/notebook2',
previousUri: undefined,
nextUri: undefined
};
await fs.mkdir(rootFolderPath);
if(run.it === 'v1'){
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
await fs.writeFile(run.folderPaths.tableofContentsFile, run.contents.toc);
await fs.writeFile(run.folderPaths.notebook2File, '');
const mockExtensionContext = new MockExtensionContext();
folder = [{
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
}];
bookTreeViewProvider = new BookTreeViewProvider(folder, mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
it('should show error if notebook or markdown file is missing', async function (): Promise<void> {
let books: BookTreeItem[] = bookTreeViewProvider.currentBook.bookItems;
let children = await bookTreeViewProvider.currentBook.getSections({ sections: [] }, books[0].sections, rootFolderPath, books[0].book);
should(bookTreeViewProvider.currentBook.errorMessage).equal('Missing file : Notebook1');
// rest of book should be detected correctly even with a missing file
equalBookItems(children[0], expectedNotebook2);
});
after(async function (): Promise<void> {
if (await exists(rootFolderPath)) await promisify(rimraf)(rootFolderPath);
});
});
})});
describe('BookTreeViewProvider.Commands', function() {
let rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let runs = [
{
it: 'v1',
folderPaths : {
'dataFolderPath' : path.join(rootFolderPath, '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'content'),
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath,'_data', 'toc.yml'),
'notebook1File' : path.join(rootFolderPath, 'content', 'notebook1.ipynb'),
'notebook2File' : path.join(rootFolderPath, 'content', 'notebook2.ipynb'),
'markdownFile' : path.join(rootFolderPath, 'content', 'readme.md')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Home\n url: /readme\n- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2'
}
},
{
it: 'v2',
folderPaths : {
'configFile' : path.join(rootFolderPath, '_config.yml'),
'tableofContentsFile' : path.join(rootFolderPath, '_toc.yml'),
'notebook1File' : path.join(rootFolderPath, 'notebook1.ipynb'),
'notebook2File' : path.join(rootFolderPath, 'notebook2.ipynb'),
'markdownFile' : path.join(rootFolderPath, 'readme.md')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Home\n file: /readme\n- title: Notebook1\n file: /notebook1\n- title: Notebook2\n file: /notebook2'
}
}
];
runs.forEach(function (run){
describe('BookTreeViewProvider.Commands on ' + run.it, function (): void {
before(async () => {
await fs.mkdir(rootFolderPath);
if(run.it === 'v1'){
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
await fs.writeFile(run.folderPaths.tableofContentsFile, run.contents.toc);
await fs.writeFile(run.folderPaths.notebook1File, '');
await fs.writeFile(run.folderPaths.notebook2File, '');
await fs.writeFile(run.folderPaths.markdownFile, '');
const mockExtensionContext = new MockExtensionContext();
bookTreeViewProvider = new BookTreeViewProvider([], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
afterEach(async function (): Promise<void> {
sinon.restore();
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
it('should add book and initialize book on openBook', async () => {
should(bookTreeViewProvider.books.length).equal(0, 'Invalid books on initialize.');
let showPreviewSpy = sinon.spy(bookTreeViewProvider, 'showPreviewFile');
await bookTreeViewProvider.openBook(rootFolderPath);
should(bookTreeViewProvider.books.length).equal(1, 'Failed to initialize the book on open');
should(showPreviewSpy.notCalled).be.true('Should not call showPreviewFile when showPreview isn\' true');
});
it('openMarkdown should open markdown in the editor', async () => {
let executeCommandSpy = sinon.spy(vscode.commands, 'executeCommand');
let notebookPath = run.folderPaths.markdownFile;
bookTreeViewProvider.openMarkdown(notebookPath);
should(executeCommandSpy.calledWith('markdown.showPreview')).be.true('openMarkdown should have called markdown.showPreview');
});
// TODO: Need to investigate why it's failing on linux.
it.skip('openNotebook should open notebook in the editor', async () => {
let showNotebookSpy = sinon.spy(azdata.nb, 'showNotebookDocument');
let notebookPath = run.folderPaths.notebook2File;
await bookTreeViewProvider.openNotebook(notebookPath);
should(showNotebookSpy.calledWith(vscode.Uri.file(notebookPath))).be.true(`Should have opened the notebook from ${notebookPath} in the editor.`);
});
it('openNotebookAsUntitled should open a notebook as untitled file in the editor', async () => {
let notebookPath = run.folderPaths.notebook2File;
await bookTreeViewProvider.openNotebookAsUntitled(notebookPath);
should(azdata.nb.notebookDocuments.find(doc => doc.uri.scheme === 'untitled')).not.be.undefined();
});
it('openExternalLink should open link', async () => {
let executeCommandSpy = sinon.spy(vscode.commands, 'executeCommand');
let notebookPath = run.folderPaths.markdownFile;
bookTreeViewProvider.openMarkdown(notebookPath);
should(executeCommandSpy.calledWith('markdown.showPreview')).be.true('openMarkdown should have called markdown.showPreview');
});
it('should call showPreviewFile on openBook when showPreview flag is set', async () => {
await bookTreeViewProvider.closeBook(bookTreeViewProvider.books[0].bookItems[0]);
let showPreviewSpy = sinon.spy(bookTreeViewProvider, 'showPreviewFile');
await bookTreeViewProvider.openBook(rootFolderPath, undefined, true);
should(bookTreeViewProvider.books.length).equal(1, 'Failed to initialize the book on open');
should(showPreviewSpy.calledOnce).be.true('Should have called showPreviewFile.');
});
it('should add book when bookPath contains special characters on openBook', async () => {
let rootFolderPath2 = path.join(os.tmpdir(), `BookTestData(1)_${uuid.v4()}`);
let dataFolderPath2 = path.join(rootFolderPath2, '_data');
let contentFolderPath2 = path.join(rootFolderPath2, 'content');
let configFile2 = path.join(rootFolderPath2, '_config.yml');
let tableOfContentsFile2 = path.join(dataFolderPath2, 'toc.yml');
let notebook2File2 = path.join(contentFolderPath2, 'notebook2.ipynb');
await fs.mkdir(rootFolderPath2);
await fs.mkdir(dataFolderPath2);
await fs.mkdir(contentFolderPath2);
await fs.writeFile(configFile2, 'title: Test Book');
await fs.writeFile(tableOfContentsFile2, '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2');
await fs.writeFile(notebook2File2, '');
await bookTreeViewProvider.openBook(rootFolderPath2);
should(bookTreeViewProvider.books.length).equal(2, 'Failed to initialize the book on open');
await promisify(rimraf)(rootFolderPath2);
});
it('should get notebook path with untitled schema on openNotebookAsUntitled', async () => {
let notebookUri = bookTreeViewProvider.getUntitledNotebookUri(run.folderPaths.notebook2File);
should(notebookUri.scheme).equal('untitled', 'Failed to get untitled uri of the resource');
});
it('openNotebookFolder without folderPath should prompt for folder path and invoke loadNotebooksInFolder', async () => {
sinon.stub(vscode.window, 'showOpenDialog').returns(Promise.resolve([vscode.Uri.file(rootFolderPath)]));
let loadNotebooksSpy = sinon.spy(bookTreeViewProvider, 'loadNotebooksInFolder');
await bookTreeViewProvider.openNotebookFolder();
should(loadNotebooksSpy.calledWith(rootFolderPath)).be.true('openNotebookFolder should have called loadNotebooksInFolder passing the folderPath');
});
it('openNotebookFolder with folderPath shouldn\'t prompt for folder path but invoke loadNotebooksInFolder with the provided folderPath', async () => {
let showOpenDialogSpy = sinon.spy(vscode.window, 'showOpenDialog');
let loadNotebooksSpy = sinon.spy(bookTreeViewProvider, 'loadNotebooksInFolder');
await bookTreeViewProvider.openNotebookFolder(rootFolderPath);
should(showOpenDialogSpy.notCalled).be.true('openNotebookFolder with folderPath shouldn\'t prompt to select folder');
should(loadNotebooksSpy.calledOnce).be.true('openNotebookFolder should have called loadNotebooksInFolder');
});
it('openNewBook should prompt for notebook path and invoke openBook', async () => {
sinon.stub(vscode.window, 'showOpenDialog').returns(Promise.resolve([vscode.Uri.file(rootFolderPath)]));
let openBookSpy = sinon.spy(bookTreeViewProvider, 'openBook');
await bookTreeViewProvider.openNewBook();
should(openBookSpy.calledOnce).be.true('openNewBook should have called openBook');
});
it('searchJupyterBooks should call command that opens Search view', async () => {
let executeCommandSpy = sinon.spy(vscode.commands, 'executeCommand');
await bookTreeViewProvider.searchJupyterBooks(bookTreeViewProvider.books[0].bookItems[0]);
should(executeCommandSpy.calledWith('workbench.action.findInFiles')).be.true('searchJupyterBooks should have called command to open Search view');
});
it('saveJupyterBooks should prompt location and openBook', async () => {
let saveFolderPath = path.join(os.tmpdir(), `Book_${uuid.v4()}`);
await fs.mkdir(saveFolderPath);
sinon.stub(vscode.window, 'showOpenDialog').returns(Promise.resolve([vscode.Uri.file(saveFolderPath)]));
let executeCommandSpy = sinon.spy(vscode.commands, 'executeCommand');
await bookTreeViewProvider.saveJupyterBooks();
should(executeCommandSpy.calledWith('bookTreeView.openBook')).be.true('saveJupyterBooks should have called command openBook after saving');
await promisify(rimraf)(saveFolderPath);
});
it('should remove book on closeBook', async () => {
let length: number = bookTreeViewProvider.books.length;
await bookTreeViewProvider.closeBook(bookTreeViewProvider.books[0].bookItems[0]);
should(bookTreeViewProvider.books.length).equal(length - 1, 'Failed to remove the book on close');
});
after(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
}
});
});
});
});
describe('BookTreeViewProvider.getSections', function (): void {
let rootFolderPath: string;
let tableOfContentsFile: string;
describe('BookTreeViewProvider.openNotebookFolder', function() {
let rootFolderPath = path.join(os.tmpdir(), `BookFolderTest_${uuid.v4()}`);
let bookTreeViewProvider: BookTreeViewProvider;
let folder: vscode.WorkspaceFolder;
let expectedNotebook2: IExpectedBookItem;
let runs = [
{
it: 'v1',
folderPaths : {
'bookFolderPath' : path.join(rootFolderPath, 'BookTestData'),
'dataFolderPath' : path.join(rootFolderPath, 'BookTestData', '_data'),
'contentFolderPath' : path.join(rootFolderPath, 'BookTestData', 'content'),
'configFile' : path.join(rootFolderPath, 'BookTestData', '_config.yml'),
'tableOfContentsFile' : path.join(rootFolderPath,'BookTestData','_data', 'toc.yml'),
'bookNotebookFile' : path.join(rootFolderPath, 'BookTestData', 'content', 'notebook1.ipynb'),
'notebookFolderPath' : path.join(rootFolderPath, 'NotebookTestData'),
'standaloneNotebookFile' : path.join(rootFolderPath, 'NotebookTestData','notebook2.ipynb')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n url: /notebook1',
'bookTitle' : 'Test Book',
'standaloneNotebookTitle' : 'notebook2'
}
},
{
it: 'v2',
folderPaths : {
'bookFolderPath' : path.join(rootFolderPath, 'BookTestData'),
'configFile' : path.join(rootFolderPath, 'BookTestData', '_config.yml'),
'tableOfContentsFile' : path.join(rootFolderPath,'BookTestData', '_toc.yml'),
'bookNotebookFile' : path.join(rootFolderPath, 'BookTestData','notebook1.ipynb'),
'notebookFolderPath' : path.join(rootFolderPath, 'NotebookTestData'),
'standaloneNotebookFile' : path.join(rootFolderPath, 'NotebookTestData','notebook2.ipynb')
},
contents : {
'config' : 'title: Test Book',
'toc' : '- title: Notebook1\n file: /notebook1',
'bookTitle' : 'Test Book',
'standaloneNotebookTitle' : 'notebook2'
}
}];
runs.forEach(function (run){
describe('BookTreeViewProvider.openNotebookFolder on ' + run.it, function (): void {
before(async () => {
this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let dataFolderPath = path.join(rootFolderPath, '_data');
let contentFolderPath = path.join(rootFolderPath, 'content');
let configFile = path.join(rootFolderPath, '_config.yml');
tableOfContentsFile = path.join(dataFolderPath, 'toc.yml');
let notebook2File = path.join(contentFolderPath, 'notebook2.ipynb');
expectedNotebook2 = {
title: 'Notebook2',
url: '/notebook2',
previousUri: undefined,
nextUri: undefined
};
await fs.mkdir(rootFolderPath);
await fs.mkdir(dataFolderPath);
await fs.mkdir(contentFolderPath);
await fs.writeFile(configFile, 'title: Test Book');
await fs.writeFile(tableOfContentsFile, '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2');
await fs.writeFile(notebook2File, '');
await fs.mkdir(rootFolderPath);
await fs.mkdir(run.folderPaths.bookFolderPath);
if(run.it === 'v1') {
await fs.mkdir(run.folderPaths.dataFolderPath);
await fs.mkdir(run.folderPaths.contentFolderPath);
}
await fs.mkdir(run.folderPaths.notebookFolderPath);
await fs.writeFile(run.folderPaths.configFile, run.contents.config);
await fs.writeFile(run.folderPaths.tableOfContentsFile, run.contents.toc);
await fs.writeFile(run.folderPaths.bookNotebookFile, '');
await fs.writeFile(run.folderPaths.standaloneNotebookFile, '');
const mockExtensionContext = new MockExtensionContext();
folder = {
uri: vscode.Uri.file(rootFolderPath),
name: '',
index: 0
};
bookTreeViewProvider = new BookTreeViewProvider([folder], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
const mockExtensionContext = new MockExtensionContext();
bookTreeViewProvider = new BookTreeViewProvider([], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
it('should include books and notebooks when opening parent folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(rootFolderPath);
should(bookTreeViewProvider.books.length).equal(2, 'Should have loaded a book and a notebook');
validateIsBook(bookTreeViewProvider.books[0]);
validateIsNotebook(bookTreeViewProvider.books[1]);
});
it('should include only books when opening books folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(run.folderPaths.bookFolderPath);
should(bookTreeViewProvider.books.length).equal(1, 'Should have loaded only one book');
validateIsBook(bookTreeViewProvider.books[0]);
});
it('should include only notebooks when opening notebooks folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(run.folderPaths.notebookFolderPath);
should(bookTreeViewProvider.books.length).equal(1, 'Should have loaded only one notebook');
validateIsNotebook(bookTreeViewProvider.books[0]);
});
afterEach(async function (): Promise<void> {
let bookItems = await bookTreeViewProvider.getChildren();
await Promise.all(bookItems.map(bookItem => bookTreeViewProvider.closeBook(bookItem)));
});
after(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
}
});
let validateIsBook = (book: BookModel) => {
should(book.isNotebook).be.false();
should(book.bookItems.length).equal(1);
let bookItem = book.bookItems[0];
let bookDetails = bookItem.book;
should(bookDetails.type).equal(BookTreeItemType.Book);
should(bookDetails.title).equal(run.contents.bookTitle);
should(bookDetails.contentPath).equal(run.folderPaths.tableOfContentsFile.replace(/\\/g, '/'));
should(bookDetails.root).equal(run.folderPaths.bookFolderPath.replace(/\\/g, '/'));
should(bookDetails.tableOfContents.sections).not.equal(undefined);
should(bookDetails.page).not.equal(undefined);
};
let validateIsNotebook = (book: BookModel) => {
should(book.isNotebook).be.true();
should(book.bookItems.length).equal(1);
let bookItem = book.bookItems[0];
should(book.getAllNotebooks().get(vscode.Uri.file(run.folderPaths.standaloneNotebookFile).fsPath)).equal(bookItem);
let bookDetails = bookItem.book;
should(bookDetails.type).equal(BookTreeItemType.Notebook);
should(bookDetails.title).equal(run.contents.standaloneNotebookTitle);
should(bookDetails.contentPath).equal(run.folderPaths.standaloneNotebookFile.replace(/\\/g, '/'));
should(bookDetails.root).equal(run.folderPaths.notebookFolderPath.replace(/\\/g, '/'));
should(bookDetails.tableOfContents.sections).equal(undefined);
should(bookDetails.page.sections).equal(undefined);
};
});
});
it('should show error if notebook or markdown file is missing', async function (): Promise<void> {
let books: BookTreeItem[] = bookTreeViewProvider.currentBook.bookItems;
let children = await bookTreeViewProvider.currentBook.getSections({ sections: [] }, books[0].sections, rootFolderPath);
should(bookTreeViewProvider.currentBook.errorMessage).equal('Missing file : Notebook1');
// rest of book should be detected correctly even with a missing file
equalBookItems(children[0], expectedNotebook2);
});
this.afterAll(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
}
});
});
describe('BookTreeViewProvider.Commands', function (): void {
let rootFolderPath: string;
let tableOfContentsFile: string;
let bookTreeViewProvider: BookTreeViewProvider;
this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookTestData_${uuid.v4()}`);
let dataFolderPath = path.join(rootFolderPath, '_data');
let contentFolderPath = path.join(rootFolderPath, 'content');
let configFile = path.join(rootFolderPath, '_config.yml');
tableOfContentsFile = path.join(dataFolderPath, 'toc.yml');
let notebook1File = path.join(contentFolderPath, 'notebook1.ipynb');
let notebook2File = path.join(contentFolderPath, 'notebook2.ipynb');
let markdownFile = path.join(contentFolderPath, 'readme.md');
await fs.mkdir(rootFolderPath);
await fs.mkdir(dataFolderPath);
await fs.mkdir(contentFolderPath);
await fs.writeFile(configFile, 'title: Test Book');
await fs.writeFile(tableOfContentsFile, '- title: Home\n url: /readme\n- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2');
await fs.writeFile(notebook1File, '');
await fs.writeFile(notebook2File, '');
await fs.writeFile(markdownFile, '');
const mockExtensionContext = new MockExtensionContext();
bookTreeViewProvider = new BookTreeViewProvider([], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
afterEach(async function (): Promise<void> {
sinon.restore();
await vscode.commands.executeCommand('workbench.action.closeAllEditors');
});
it('should add book and initialize book on openBook', async () => {
should(bookTreeViewProvider.books.length).equal(0, 'Invalid books on initialize.');
let showPreviewSpy = sinon.spy(bookTreeViewProvider, 'showPreviewFile');
await bookTreeViewProvider.openBook(rootFolderPath);
should(bookTreeViewProvider.books.length).equal(1, 'Failed to initialize the book on open');
should(showPreviewSpy.notCalled).be.true('Should not call showPreviewFile when showPreview isn\' true');
});
it('openMarkdown should open markdown in the editor', async () => {
let executeCommandSpy = sinon.spy(vscode.commands, 'executeCommand');
let notebookPath = path.join(rootFolderPath, 'content', 'readme.md');
bookTreeViewProvider.openMarkdown(notebookPath);
should(executeCommandSpy.calledWith('markdown.showPreview')).be.true('openMarkdown should have called markdown.showPreview');
});
// TODO: Need to investigate why it's failing on linux.
it.skip('openNotebook should open notebook in the editor', async () => {
let showNotebookSpy = sinon.spy(azdata.nb, 'showNotebookDocument');
let notebookPath = path.join(rootFolderPath, 'content', 'notebook2.ipynb');
await bookTreeViewProvider.openNotebook(notebookPath);
should(showNotebookSpy.calledWith(vscode.Uri.file(notebookPath))).be.true(`Should have opened the notebook from ${notebookPath} in the editor.`);
});
it('openNotebookAsUntitled should open a notebook as untitled file in the editor', async () => {
let notebookPath = path.join(rootFolderPath, 'content', 'notebook2.ipynb');
await bookTreeViewProvider.openNotebookAsUntitled(notebookPath);
should(azdata.nb.notebookDocuments.find(doc => doc.uri.scheme === 'untitled')).not.be.undefined();
});
it('openExternalLink should open link', async () => {
let executeCommandSpy = sinon.spy(vscode.commands, 'executeCommand');
let notebookPath = path.join(rootFolderPath, 'content', 'readme.md');
bookTreeViewProvider.openMarkdown(notebookPath);
should(executeCommandSpy.calledWith('markdown.showPreview')).be.true('openMarkdown should have called markdown.showPreview');
});
it('should call showPreviewFile on openBook when showPreview flag is set', async () => {
await bookTreeViewProvider.closeBook(bookTreeViewProvider.books[0].bookItems[0]);
let showPreviewSpy = sinon.spy(bookTreeViewProvider, 'showPreviewFile');
await bookTreeViewProvider.openBook(rootFolderPath, undefined, true);
should(bookTreeViewProvider.books.length).equal(1, 'Failed to initialize the book on open');
should(showPreviewSpy.calledOnce).be.true('Should have called showPreviewFile.');
});
it('should add book when bookPath contains special characters on openBook', async () => {
let rootFolderPath2 = path.join(os.tmpdir(), `BookTestData(1)_${uuid.v4()}`);
let dataFolderPath2 = path.join(rootFolderPath2, '_data');
let contentFolderPath2 = path.join(rootFolderPath2, 'content');
let configFile2 = path.join(rootFolderPath2, '_config.yml');
let tableOfContentsFile2 = path.join(dataFolderPath2, 'toc.yml');
let notebook2File2 = path.join(contentFolderPath2, 'notebook2.ipynb');
await fs.mkdir(rootFolderPath2);
await fs.mkdir(dataFolderPath2);
await fs.mkdir(contentFolderPath2);
await fs.writeFile(configFile2, 'title: Test Book');
await fs.writeFile(tableOfContentsFile2, '- title: Notebook1\n url: /notebook1\n- title: Notebook2\n url: /notebook2');
await fs.writeFile(notebook2File2, '');
await bookTreeViewProvider.openBook(rootFolderPath2);
should(bookTreeViewProvider.books.length).equal(2, 'Failed to initialize the book on open');
await promisify(rimraf)(rootFolderPath2);
});
it('should get notebook path with untitled schema on openNotebookAsUntitled', async () => {
let notebookUri = bookTreeViewProvider.getUntitledNotebookUri(path.join(rootFolderPath, 'content', 'notebook2.ipynb'));
should(notebookUri.scheme).equal('untitled', 'Failed to get untitled uri of the resource');
});
it('openNotebookFolder without folderPath should prompt for folder path and invoke loadNotebooksInFolder', async () => {
sinon.stub(vscode.window, 'showOpenDialog').returns(Promise.resolve([vscode.Uri.file(rootFolderPath)]));
let loadNotebooksSpy = sinon.spy(bookTreeViewProvider, 'loadNotebooksInFolder');
await bookTreeViewProvider.openNotebookFolder();
should(loadNotebooksSpy.calledWith(rootFolderPath)).be.true('openNotebookFolder should have called loadNotebooksInFolder passing the folderPath');
});
it('openNotebookFolder with folderPath shouldn\'t prompt for folder path but invoke loadNotebooksInFolder with the provided folderPath', async () => {
let showOpenDialogSpy = sinon.spy(vscode.window, 'showOpenDialog');
let loadNotebooksSpy = sinon.spy(bookTreeViewProvider, 'loadNotebooksInFolder');
await bookTreeViewProvider.openNotebookFolder(rootFolderPath);
should(showOpenDialogSpy.notCalled).be.true('openNotebookFolder with folderPath shouldn\'t prompt to select folder');
should(loadNotebooksSpy.calledOnce).be.true('openNotebookFolder should have called loadNotebooksInFolder');
});
it('openNewBook should prompt for notebook path and invoke openBook', async () => {
sinon.stub(vscode.window, 'showOpenDialog').returns(Promise.resolve([vscode.Uri.file(rootFolderPath)]));
let openBookSpy = sinon.spy(bookTreeViewProvider, 'openBook');
await bookTreeViewProvider.openNewBook();
should(openBookSpy.calledOnce).be.true('openNewBook should have called openBook');
});
it('searchJupyterBooks should call command that opens Search view', async () => {
let executeCommandSpy = sinon.spy(vscode.commands, 'executeCommand');
await bookTreeViewProvider.searchJupyterBooks(bookTreeViewProvider.books[0].bookItems[0]);
should(executeCommandSpy.calledWith('workbench.action.findInFiles')).be.true('searchJupyterBooks should have called command to open Search view');
});
it('saveJupyterBooks should prompt location and openBook', async () => {
let saveFolderPath = path.join(os.tmpdir(), `Book_${uuid.v4()}`);
await fs.mkdir(saveFolderPath);
sinon.stub(vscode.window, 'showOpenDialog').returns(Promise.resolve([vscode.Uri.file(saveFolderPath)]));
let executeCommandSpy = sinon.spy(vscode.commands, 'executeCommand');
await bookTreeViewProvider.saveJupyterBooks();
should(executeCommandSpy.calledWith('bookTreeView.openBook')).be.true('saveJupyterBooks should have called command openBook after saving');
await promisify(rimraf)(saveFolderPath);
});
it('should remove book on closeBook', async () => {
let length: number = bookTreeViewProvider.books.length;
await bookTreeViewProvider.closeBook(bookTreeViewProvider.books[0].bookItems[0]);
should(bookTreeViewProvider.books.length).equal(length - 1, 'Failed to remove the book on close');
});
this.afterAll(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
}
});
});
describe('BookTreeViewProvider.openNotebookFolder', function (): void {
let rootFolderPath: string;
let bookFolderPath: string;
let bookTitle: string;
let notebookFolderPath: string;
let tableOfContentsFile: string;
let standaloneNotebookTitle: string;
let standaloneNotebookFile: string;
let bookTreeViewProvider: BookTreeViewProvider;
this.beforeAll(async () => {
rootFolderPath = path.join(os.tmpdir(), `BookFolderTest_${uuid.v4()}`);
bookFolderPath = path.join(rootFolderPath, 'BookTestData');
let dataFolderPath = path.join(bookFolderPath, '_data');
let contentFolderPath = path.join(bookFolderPath, 'content');
let configFile = path.join(bookFolderPath, '_config.yml');
tableOfContentsFile = path.join(dataFolderPath, 'toc.yml');
let bookNotebookFile = path.join(contentFolderPath, 'notebook1.ipynb');
notebookFolderPath = path.join(rootFolderPath, 'NotebookTestData');
standaloneNotebookTitle = 'notebook2';
standaloneNotebookFile = path.join(notebookFolderPath, `${standaloneNotebookTitle}.ipynb`);
await fs.mkdir(rootFolderPath);
await fs.mkdir(bookFolderPath);
await fs.mkdir(dataFolderPath);
await fs.mkdir(contentFolderPath);
await fs.mkdir(notebookFolderPath);
bookTitle = 'Test Book';
await fs.writeFile(configFile, `title: ${bookTitle}`);
await fs.writeFile(tableOfContentsFile, '- title: Notebook1\n url: /notebook1');
await fs.writeFile(bookNotebookFile, '');
await fs.writeFile(standaloneNotebookFile, '');
const mockExtensionContext = new MockExtensionContext();
bookTreeViewProvider = new BookTreeViewProvider([], mockExtensionContext, false, 'bookTreeView', NavigationProviders.NotebooksNavigator);
let errorCase = new Promise((resolve, reject) => setTimeout(() => resolve(), 5000));
await Promise.race([bookTreeViewProvider.initialized, errorCase.then(() => { throw new Error('BookTreeViewProvider did not initialize in time'); })]);
});
it('should include books and notebooks when opening parent folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(rootFolderPath);
should(bookTreeViewProvider.books.length).equal(2, 'Should have loaded a book and a notebook');
validateIsBook(bookTreeViewProvider.books[0]);
validateIsNotebook(bookTreeViewProvider.books[1]);
});
it('should include only books when opening books folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(bookFolderPath);
should(bookTreeViewProvider.books.length).equal(1, 'Should have loaded only one book');
validateIsBook(bookTreeViewProvider.books[0]);
});
it('should include only notebooks when opening notebooks folder', async () => {
await bookTreeViewProvider.loadNotebooksInFolder(notebookFolderPath);
should(bookTreeViewProvider.books.length).equal(1, 'Should have loaded only one notebook');
validateIsNotebook(bookTreeViewProvider.books[0]);
});
this.afterEach(async function (): Promise<void> {
let bookItems = await bookTreeViewProvider.getChildren();
await Promise.all(bookItems.map(bookItem => bookTreeViewProvider.closeBook(bookItem)));
});
this.afterAll(async function (): Promise<void> {
if (await exists(rootFolderPath)) {
await promisify(rimraf)(rootFolderPath);
}
});
let validateIsBook = (book: BookModel) => {
should(book.isNotebook).be.false();
should(book.bookItems.length).equal(1);
let bookItem = book.bookItems[0];
let bookDetails = bookItem.book;
should(bookDetails.type).equal(BookTreeItemType.Book);
should(bookDetails.title).equal(bookTitle);
should(bookDetails.contentPath).equal(tableOfContentsFile.replace(/\\/g, '/'));
should(bookDetails.root).equal(bookFolderPath.replace(/\\/g, '/'));
should(bookDetails.tableOfContents.sections).not.equal(undefined);
should(bookDetails.page).not.equal(undefined);
};
let validateIsNotebook = (book: BookModel) => {
should(book.isNotebook).be.true();
should(book.bookItems.length).equal(1);
let bookItem = book.bookItems[0];
should(book.getAllNotebooks().get(vscode.Uri.file(standaloneNotebookFile).fsPath)).equal(bookItem);
let bookDetails = bookItem.book;
should(bookDetails.type).equal(BookTreeItemType.Notebook);
should(bookDetails.title).equal(standaloneNotebookTitle);
should(bookDetails.contentPath).equal(standaloneNotebookFile.replace(/\\/g, '/'));
should(bookDetails.root).equal(notebookFolderPath.replace(/\\/g, '/'));
should(bookDetails.tableOfContents.sections).equal(undefined);
should(bookDetails.page.sections).equal(undefined);
};
});
});