-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
54 lines (44 loc) · 1.41 KB
/
main.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
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import * as ObsidianHelpers from 'util/obsidianHelper';
import widgets from 'codemirror-widgets';
import katex from 'katex';
export default class MyPlugin extends Plugin {
async onload() {
console.log("Editor math loaded");
// bind math widget to all the CodeMirror edtiors
this.registerCodeMirror((cm: CodeMirror.Editor) => {
this.handleInitialLoad(cm);
});
}
onunload() {
console.log("Editor math unloaded");
}
// Only Triggered during initial Load
handleInitialLoad = (cm: CodeMirror.Editor) => {
// Create a type of widget
var WidgetMath = widgets.createType({
mixins: [
widgets.mixins.re(/\$\$([^$]+)\$\$/g, function(match:any) {
return {
props: {
text: match[1]
}
};
}),
// widgets.mixins.editDelimit('$$', '$$')
widgets.mixins.editParagraph()
],
createElement: function(widget:any) {
// Create the spam to replace the formula
var span = document.createElement('span');
// Render the formula using katex
katex.render(widget.props.text, span)
return span;
}
});
// Create a widgets manager connected to an editor
var manager = widgets.createManager(cm);
// Connect a type of widget to the manager
manager.enable(WidgetMath);
};
}