forked from jupyterlab/jupyterlab
/
index.ts
96 lines (88 loc) · 2.66 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
/*-----------------------------------------------------------------------------
| Copyright (c) Jupyter Development Team.
| Distributed under the terms of the Modified BSD License.
|----------------------------------------------------------------------------*/
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { IDocumentManager } from '@jupyterlab/docmanager';
import {
ILatexTypesetter,
IRenderMimeRegistry,
RenderMimeRegistry,
standardRendererFactories
} from '@jupyterlab/rendermime';
namespace CommandIDs {
export const handleLink = 'rendermime:handle-local-link';
}
/**
* A plugin providing a rendermime registry.
*/
const plugin: JupyterFrontEndPlugin<IRenderMimeRegistry> = {
id: '@jupyterlab/rendermime-extension:plugin',
requires: [],
optional: [IDocumentManager, ILatexTypesetter],
provides: IRenderMimeRegistry,
activate: activate,
autoStart: true
};
/**
* Export the plugin as default.
*/
export default plugin;
/**
* Activate the rendermine plugin.
*/
function activate(
app: JupyterFrontEnd,
docManager: IDocumentManager | null,
latexTypesetter: ILatexTypesetter | null
) {
if (docManager) {
app.commands.addCommand(CommandIDs.handleLink, {
label: 'Handle Local Link',
execute: args => {
const path = args['path'] as string | undefined | null;
const id = args['id'] as string | undefined | null;
if (!path) {
return;
}
// First check if the path exists on the server.
return docManager.services.contents
.get(path, { content: false })
.then(() => {
// Open the link with the default rendered widget factory,
// if applicable.
const factory = docManager.registry.defaultRenderedWidgetFactory(
path
);
const widget = docManager.openOrReveal(path, factory.name);
// Handle the hash if one has been provided.
if (widget && id) {
widget.setFragment(id);
}
});
}
});
}
return new RenderMimeRegistry({
initialFactories: standardRendererFactories,
linkHandler: !docManager
? null
: {
handleLink: (node: HTMLElement, path: string, id?: string) => {
// If node has the download attribute explicitly set, use the
// default browser downloading behavior.
if (node.tagName === 'A' && node.hasAttribute('download')) {
return;
}
app.commandLinker.connectNode(node, CommandIDs.handleLink, {
path,
id
});
}
},
latexTypesetter
});
}