-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
index.ts
204 lines (186 loc) · 5.24 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
/**
* @packageDocumentation
* @module markdownviewer-extension
*/
import {
ILayoutRestorer,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import { ISanitizer, WidgetTracker } from '@jupyterlab/apputils';
import { PathExt } from '@jupyterlab/coreutils';
import {
IMarkdownViewerTracker,
MarkdownDocument,
MarkdownViewer,
MarkdownViewerFactory,
MarkdownViewerTableOfContentsFactory
} from '@jupyterlab/markdownviewer';
import {
IRenderMime,
IRenderMimeRegistry,
markdownRendererFactory
} from '@jupyterlab/rendermime';
import { ISettingRegistry } from '@jupyterlab/settingregistry';
import { ITableOfContentsRegistry } from '@jupyterlab/toc';
import { ITranslator } from '@jupyterlab/translation';
/**
* The command IDs used by the markdownviewer plugin.
*/
namespace CommandIDs {
export const markdownPreview = 'markdownviewer:open';
export const markdownEditor = 'markdownviewer:edit';
}
/**
* The name of the factory that creates markdown viewer widgets.
*/
const FACTORY = 'Markdown Preview';
/**
* The markdown viewer plugin.
*/
const plugin: JupyterFrontEndPlugin<IMarkdownViewerTracker> = {
activate,
id: '@jupyterlab/markdownviewer-extension:plugin',
description: 'Adds markdown file viewer and provides its tracker.',
provides: IMarkdownViewerTracker,
requires: [IRenderMimeRegistry, ITranslator],
optional: [
ILayoutRestorer,
ISettingRegistry,
ITableOfContentsRegistry,
ISanitizer
],
autoStart: true
};
/**
* Activate the markdown viewer plugin.
*/
function activate(
app: JupyterFrontEnd,
rendermime: IRenderMimeRegistry,
translator: ITranslator,
restorer: ILayoutRestorer | null,
settingRegistry: ISettingRegistry | null,
tocRegistry: ITableOfContentsRegistry | null,
sanitizer: IRenderMime.ISanitizer | null
): IMarkdownViewerTracker {
const trans = translator.load('jupyterlab');
const { commands, docRegistry } = app;
// Add the markdown renderer factory.
rendermime.addFactory(markdownRendererFactory);
const namespace = 'markdownviewer-widget';
const tracker = new WidgetTracker<MarkdownDocument>({
namespace
});
let config: Partial<MarkdownViewer.IConfig> = {
...MarkdownViewer.defaultConfig
};
/**
* Update the settings of a widget.
*/
function updateWidget(widget: MarkdownViewer): void {
Object.keys(config).forEach((k: keyof MarkdownViewer.IConfig) => {
widget.setOption(k, config[k] ?? null);
});
}
if (settingRegistry) {
const updateSettings = (settings: ISettingRegistry.ISettings) => {
config = settings.composite as Partial<MarkdownViewer.IConfig>;
tracker.forEach(widget => {
updateWidget(widget.content);
});
};
// Fetch the initial state of the settings.
settingRegistry
.load(plugin.id)
.then((settings: ISettingRegistry.ISettings) => {
settings.changed.connect(() => {
updateSettings(settings);
});
updateSettings(settings);
})
.catch((reason: Error) => {
console.error(reason.message);
});
}
// Register the MarkdownViewer factory.
const factory = new MarkdownViewerFactory({
rendermime,
name: FACTORY,
label: trans.__('Markdown Preview'),
primaryFileType: docRegistry.getFileType('markdown'),
fileTypes: ['markdown'],
defaultRendered: ['markdown']
});
factory.widgetCreated.connect((sender, widget) => {
// Notify the widget tracker if restore data needs to update.
widget.context.pathChanged.connect(() => {
void tracker.save(widget);
});
// Handle the settings of new widgets.
updateWidget(widget.content);
void tracker.add(widget);
});
docRegistry.addWidgetFactory(factory);
// Handle state restoration.
if (restorer) {
void restorer.restore(tracker, {
command: 'docmanager:open',
args: widget => ({ path: widget.context.path, factory: FACTORY }),
name: widget => widget.context.path
});
}
commands.addCommand(CommandIDs.markdownPreview, {
label: trans.__('Markdown Preview'),
execute: args => {
const path = args['path'];
if (typeof path !== 'string') {
return;
}
return commands.execute('docmanager:open', {
path,
factory: FACTORY,
options: args['options']
});
}
});
commands.addCommand(CommandIDs.markdownEditor, {
execute: () => {
const widget = tracker.currentWidget;
if (!widget) {
return;
}
const path = widget.context.path;
return commands.execute('docmanager:open', {
path,
factory: 'Editor',
options: {
mode: 'split-right'
}
});
},
isVisible: () => {
const widget = tracker.currentWidget;
return (
(widget && PathExt.extname(widget.context.path) === '.md') || false
);
},
label: trans.__('Show Markdown Editor')
});
if (tocRegistry) {
tocRegistry.add(
new MarkdownViewerTableOfContentsFactory(
tracker,
rendermime.markdownParser,
sanitizer ?? rendermime.sanitizer
)
);
}
return tracker;
}
/**
* Export the plugin as default.
*/
export default plugin;