generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.ts
153 lines (132 loc) · 3.87 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
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
import { Editor, MarkdownView, Plugin, Notice } from "obsidian";
import { PseudocodeSettingTab } from "src/setting_tab";
import { PseudocodeSuggestor } from "src/auto_complete";
import {
PseudocodeSettings,
DEFAULT_SETTINGS,
PseudocodeBlockInit,
BLOCK_NAME,
} from "src/setting";
import {
translateUnsupportedMacrosPerf,
checkTranslatedMacros,
} from "src/latex_translator";
import { createExportButton } from "src/export_button";
import { extractInlineMacros } from "src/inline_macro";
import { setObserver, detachObserver, setPseudocodeTheme } from "src/theme";
import * as pseudocode from "pseudocode";
export default class PseudocodePlugin extends Plugin {
settings: PseudocodeSettings;
preamble: string = "";
async pseudocodeHandler(
source: string,
el: HTMLElement,
ctx: any
): Promise<any> {
const blockDiv = el.createDiv({ cls: "pseudocode-block" });
const blockWidth = this.settings.blockSize;
blockDiv.style.width = `${blockWidth}em`;
// Extract inline macros
const [inlineMacros, nonMacroLines] = extractInlineMacros(source);
const allPreamble = this.preamble + inlineMacros;
// find all $ enclosements in source, and add the preamble.
// TODO: Might be able to optimize.
const mathRegex = /\$(.*?)\$/g;
const withPreamble = nonMacroLines.replace(mathRegex, (_, group1) => {
return `$${allPreamble}${group1}$`;
});
const preEl = blockDiv.createEl("pre", {
cls: "code",
text: withPreamble,
});
try {
pseudocode.renderElement(preEl, this.settings.jsSettings);
createExportButton(this, blockDiv, inlineMacros, nonMacroLines);
} catch (error) {
console.log(error);
const errorSpan = blockDiv.createEl("span", {
text: "\u274C " + error.message,
});
errorSpan.classList.add("error-message");
blockDiv.empty();
blockDiv.appendChild(errorSpan);
}
// Set the pseudocode theme
if (this.settings.followSystemTheme) {
setPseudocodeTheme(blockDiv);
}
}
async onload() {
await this.loadSettings();
setObserver();
if (this.settings.preambleEnabled) {
console.log("Preamble is enabled.");
await this.loadPreamble();
}
this.registerMarkdownCodeBlockProcessor(
BLOCK_NAME,
this.pseudocodeHandler.bind(this)
);
// Register suggest
this.registerEditorSuggest(new PseudocodeSuggestor(this));
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new PseudocodeSettingTab(this.app, this));
// Auto-gen pseudocode block command.
this.addCommand({
id: "pseudocode-in-obs",
name: "Insert a new pseudocode block",
editorCallback: (editor: Editor, view: MarkdownView) => {
editor.replaceSelection(PseudocodeBlockInit);
},
});
}
onunload() {
detachObserver();
}
async loadPreamble() {
try {
this.preamble = await this.app.vault.adapter.read(
this.settings.preamblePath
);
} catch (error) {
console.log(error);
// Extract the search path from the error message.
const searchPath = error.message.match(/'(.*?)'/g)[0];
new Notice(
"Pseudocode Plugin: Preamble file not found at " +
searchPath +
"."
);
this.preamble = "";
return;
}
try {
this.preamble = translateUnsupportedMacrosPerf(this.preamble);
this.preamble = checkTranslatedMacros(this.preamble);
console.log("Loaded preamble:\n" + this.preamble);
console.log(
"Preamble file loaded. You can check the detail in console."
);
if (this.settings.preambleLoadedNotice) {
new Notice("Pseudocode Plugin: Preamble file loaded.");
}
} catch (error) {
console.log(error);
new Notice(
"Pseudocode Plugin: Preamble file contains invalid LaTeX. " +
"Please refer to console for details."
);
this.preamble = "";
}
}
async loadSettings() {
this.settings = Object.assign(
{},
DEFAULT_SETTINGS,
await this.loadData()
);
}
async saveSettings() {
await this.saveData(this.settings);
}
}