Skip to content

Commit

Permalink
Merge pull request #23 from wei2912/custom-preamble-location
Browse files Browse the repository at this point in the history
  • Loading branch information
xldenis authored Dec 31, 2022
2 parents eea5bc8 + 1cf85dc commit 917fde9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ node_modules
package-lock.json

# build
data.json
main.js
*.js.map
*.js.map
92 changes: 71 additions & 21 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { loadMathJax, Plugin } from 'obsidian';
import { loadMathJax, App, Plugin, PluginManifest, PluginSettingTab, Setting } from 'obsidian';

const DEFAULT_PREAMBLE_PATH = "preamble.sty";
interface PluginSettings {
preamblePath: string;
}

const DEFAULT_SETTINGS: PluginSettings = {
preamblePath: "preamble.sty",
};

export default class JaxPlugin extends Plugin {
async read_preamble () {
return await this.app.vault.adapter.read(DEFAULT_PREAMBLE_PATH);
app: App;
settings: PluginSettings;

constructor(app: App, manifest: PluginManifest) {
super(app, manifest);
this.app = app;
this.settings = DEFAULT_SETTINGS;
}

async onload() {
// Load MathJax so that we can modify it
// Otherwise, it would not be loaded when this plugin is loaded
await loadMathJax();
async loadPreamble() {
const preamble = await this.app.vault.adapter.read(this.settings.preamblePath);

if (!MathJax) {
console.warn("MathJax was not defined despite loading it.");
return;
}

// Read the preamble out from the file
let preamble = await this.read_preamble();

if (MathJax.tex2chtml == undefined) {
MathJax.startup.ready = () => {
MathJax.startup.defaultReady();
Expand All @@ -28,12 +29,61 @@ export default class JaxPlugin extends Plugin {
} else {
MathJax.tex2chtml(preamble);
}
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}

async saveSettings() {
await this.saveData(this.settings);
}

async onload() {
await this.loadSettings();
this.addSettingTab(new JaxPluginSettingTab(this.app, this));

// Load MathJax so that we can modify it
// Otherwise, it would not be loaded when this plugin is loaded
await loadMathJax();

if (!MathJax) {
console.warn("MathJax was not defined despite loading it.");
return;
}

await this.loadPreamble();
// TODO: Refresh view?
}
}

onunload() {
onunload() {
// TODO: Is it possible to remove our definitions?
console.log('Unloading Extended MathJax');
}
}
console.log('Unloading Extended MathJax');
}
}

class JaxPluginSettingTab extends PluginSettingTab {
plugin: JaxPlugin;

constructor(app: App, plugin: JaxPlugin) {
super(app, plugin);
this.plugin = plugin;
}

display(): void {
const { containerEl } = this;
containerEl.empty();

new Setting(containerEl)
.setName('Preamble path')
.setDesc('Path to global preamble. (Requires reload!)')
.addText((text) =>
text
.setValue(this.plugin.settings.preamblePath)
.onChange(async (value) => {
this.plugin.settings.preamblePath = value;
await this.plugin.saveSettings();
})
);
}
}

0 comments on commit 917fde9

Please sign in to comment.