generated from RyotaUshio/obsidian-sample-plugin
/
main.ts
82 lines (66 loc) · 1.79 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
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
import { EditorSuggest, Plugin, SearchMatches, TFile, renderMath } from 'obsidian';
import { DEFAULT_SETTINGS, MyPluginSettings, SampleSettingTab } from './settings';
import { around } from 'monkey-around';
interface LinkInfo {
file: TFile;
matches: SearchMatches | null;
path: string;
score: number;
subpath?: string;
};
interface FileLinkInfo extends LinkInfo {
type: "file";
}
interface HeadingLinkInfo extends LinkInfo {
type: "heading";
heading: string;
level: number;
subpath: string;
}
interface BlockLinkInfo extends LinkInfo {
type: "block";
idMatch: SearchMatches | null;
subpath: string;
node?: any;
display: string;
content: string;
}
type Item = FileLinkInfo | HeadingLinkInfo | BlockLinkInfo;
type BuiltInAutocompletion = EditorSuggest<Item>;
export default class MyPlugin extends Plugin {
settings: MyPluginSettings;
prototype: BuiltInAutocompletion | null = null;
async onload() {
await this.loadSettings();
await this.saveSettings();
this.addSettingTab(new SampleSettingTab(this));
this.app.workspace.onLayoutReady(() => {
this.patch();
})
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
patch() {
// @ts-ignore
const prototype = this.app.workspace.editorSuggest.suggests[0].constructor.prototype as BuiltInAutocompletion;
const plugin = this;
const uninstaller = around(prototype, {
renderSuggestion(old) {
return function (item: Item, el: HTMLElement) {
if (item.type === "block") {
if (plugin.settings.math && item.node?.type === "math") {
el.appendChild(renderMath(item.node.value, true))
return;
}
}
old.call(this, item, el);
}
}
});
this.register(uninstaller);
}
}