Skip to content

Commit

Permalink
Merge pull request #17 from MinusGix/obsidian-live-preview
Browse files Browse the repository at this point in the history
  • Loading branch information
xldenis authored Feb 7, 2022
2 parents 6c5e3bb + 1f9cc44 commit 4f21f43
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 42 deletions.
61 changes: 27 additions & 34 deletions main.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}

}
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -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"
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ [email protected]:
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"
Expand Down

0 comments on commit 4f21f43

Please sign in to comment.