From 1f9cc448b3a9ccc76a101435d2edfc9af3cd74ab Mon Sep 17 00:00:00 2001 From: MinusGix Date: Sun, 6 Feb 2022 17:57:53 -0600 Subject: [PATCH] Upgrade Obsidian; Modify MathJax in onload; Fix preamble loading --- main.ts | 61 +++++++++++++++++++++++---------------------------- manifest.json | 4 ++-- package.json | 2 +- versions.json | 3 ++- yarn.lock | 8 +++---- 5 files changed, 36 insertions(+), 42 deletions(-) diff --git a/main.ts b/main.ts index 1e3551f..1b8b349 100644 --- a/main.ts +++ b/main.ts @@ -1,46 +1,39 @@ -import { Notice, Plugin } from 'obsidian'; -import { readFileSync } from 'fs'; +import { loadMathJax, Plugin } from 'obsidian'; const DEFAULT_PREAMBLE_PATH = "preamble.sty"; export default class JaxPlugin extends Plugin { async read_preamble () { - const file = this.app.vault.getAbstractFileByPath(DEFAULT_PREAMBLE_PATH); - console.log(`Loading preamble from ${file}`); - const content = await this.app.vault.read(file); - return content; + return await this.app.vault.adapter.read(DEFAULT_PREAMBLE_PATH); } - onload() { - var preludeLoaded = false; - - this.registerMarkdownPostProcessor((el, ctx) => { - if (typeof MathJax != 'undefined' && !preludeLoaded) { - this.read_preamble().then((c) => { - preludeLoaded = true; - - // Check if MathJax has already loaded and render the preamble - if (MathJax.tex2chtml == undefined) { - MathJax.startup.ready = () => { - MathJax.startup.defaultReady(); - MathJax.tex2chtml(c); - } - } else { - MathJax.tex2chtml(c); - } - - // Refresh the active view to re-render its math content - let activeLeaf = window.app.workspace.activeLeaf; - let preview = activeLeaf.view.previewMode; - preview.set(preview.get(), true) - }); - } - - }) + async onload() { + // 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; + } + + // Read the preamble out from the file + let preamble = await this.read_preamble(); + + if (MathJax.tex2chtml == undefined) { + MathJax.startup.ready = () => { + MathJax.startup.defaultReady(); + MathJax.tex2chtml(preamble); + }; + } else { + MathJax.tex2chtml(preamble); + } + + // TODO: Refresh view? } onunload() { + // TODO: Is it possible to remove our definitions? console.log('Unloading Extended MathJax'); } -} - +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 8fb85c7..3a8bf31 100644 --- a/manifest.json +++ b/manifest.json @@ -1,8 +1,8 @@ { "id": "obsidian-latex", "name": "Extended MathJax", - "version": "0.2.0", - "minAppVersion": "0.11.13", + "version": "0.3.0", + "minAppVersion": "0.12.16", "description": "Adds support for a MathJax preamble and enables additional MathJax extensions for specific domains (chemistry, proofs).", "author": "Xavier Denis", "authorUrl": "https://github.com/xldenis/obsidian-latex", diff --git a/package.json b/package.json index 4c5851c..643d44c 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "@rollup/plugin-typescript": "^6.0.0", "@types/node": "^14.14.2", "mathjax": "^3.1.2", - "obsidian": "^0.12.0", + "obsidian": "^0.12.16", "rollup": "^2.32.1", "tslib": "^2.0.3", "typescript": "^4.0.3" diff --git a/versions.json b/versions.json index 788156a..1918603 100644 --- a/versions.json +++ b/versions.json @@ -1,4 +1,5 @@ { "0.1.0": "0.9.12", - "0.2.0": "0.11.13" + "0.2.0": "0.11.13", + "0.3.0": "0.12.16" } diff --git a/yarn.lock b/yarn.lock index 990e768..bda06d0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -213,10 +213,10 @@ moment@2.29.1: resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3" integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ== -obsidian@^0.12.0: - version "0.12.0" - resolved "https://registry.yarnpkg.com/obsidian/-/obsidian-0.12.0.tgz#830286aaaa3efd17a11b4205510c6b386ca20b1a" - integrity sha512-0C6xb1xfYFaFjz5Q00XS0bT5y1BSFrl75sTV/MN/gOIWIPkyTTPZAtXZIy0dAnDwwIVgE3eB60HpfUI9xiBFGQ== +obsidian@^0.12.16: + version "0.12.17" + resolved "https://registry.yarnpkg.com/obsidian/-/obsidian-0.12.17.tgz#8efe75310d0e3988cdeccfbb176d3a8ff7b363c7" + integrity sha512-YvCAlRym9D8zNPXt6Ez8QubSTVGoChx6lb58zqI13Dcrz3l1lgUO+pcOGDiD5Qa67nzDZLXo3aV2rqkCCpTvGQ== dependencies: "@types/codemirror" "0.0.108" moment "2.29.1"