/
index.ts
111 lines (95 loc) · 3.35 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ICommandPalette, ISessionContextDialogs } from '@jupyterlab/apputils';
import { IEditorServices } from '@jupyterlab/codeeditor';
import {
INotebookTracker,
INotebookWidgetFactory,
NotebookWidgetFactory
} from '@jupyterlab/notebook';
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
import { MySTContentFactory } from './MySTContentFactory';
import { LabIcon } from '@jupyterlab/ui-components';
import mystIconSvg from '../style/mystlogo.svg';
const mystIcon = new LabIcon({
name: 'myst-notebook-extension:mystIcon',
svgstr: mystIconSvg
});
function loadKatex() {
if (typeof document === 'undefined') {
return;
}
const head = document.getElementsByTagName('HEAD')[0];
const link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'https://cdn.jsdelivr.net/npm/katex@0.15.2/dist/katex.min.css';
head.appendChild(link);
}
/**
* Initialization data for the jupyterlab-mystjs extension.
*/
const plugin: JupyterFrontEndPlugin<void> = {
id: 'jupyterlab-mystjs:plugin',
autoStart: true,
optional: [ITranslator, ICommandPalette],
requires: [
IEditorServices,
IRenderMimeRegistry,
ISessionContextDialogs,
INotebookWidgetFactory,
INotebookTracker
],
activate: (
app: JupyterFrontEnd,
editorServices: IEditorServices,
rendermime: IRenderMimeRegistry,
sessionContextDialogs: ISessionContextDialogs,
notebookFactory: NotebookWidgetFactory.IFactory,
notebookTracker: INotebookTracker,
translator: ITranslator | null,
palette: ICommandPalette | null
) => {
console.log('JupyterLab extension jupyterlab-mystjs is activated!');
loadKatex();
const contentFactory = new MySTContentFactory();
const factory = new NotebookWidgetFactory({
name: 'Jupyter MyST Notebook',
// label: trans.__("Jupyter MyST Notebook"), // will be needed in JupyterLab 4
fileTypes: ['notebook'],
defaultFor: ['notebook'],
modelName: notebookFactory.modelName ?? 'notebook',
preferKernel: notebookFactory.preferKernel ?? true,
canStartKernel: notebookFactory.canStartKernel ?? true,
rendermime,
contentFactory,
editorConfig: notebookFactory.editorConfig,
notebookConfig: notebookFactory.notebookConfig,
mimeTypeService: editorServices.mimeTypeService,
sessionDialogs: sessionContextDialogs,
toolbarFactory: notebookFactory.toolbarFactory,
translator: nullTranslator
});
let id = 0;
factory.widgetCreated.connect((sender, widget) => {
// If the notebook panel does not have an ID, assign it one.
widget.id = widget.id || `myst-notebook-${++id}`;
// Set up the title icon
widget.title.icon = mystIcon ?? '';
widget.toolbar.title.icon = mystIcon;
widget.title.iconClass = '';
widget.title.iconLabel = 'MyST Notebook';
// Notify the widget tracker if restore data needs to update.
widget.context.pathChanged.connect(() => {
void (notebookTracker as any).save(widget);
});
// Add the notebook panel to the tracker.
void (notebookTracker as any).add(widget);
});
app.docRegistry.addWidgetFactory(factory);
}
};
export default plugin;