-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from MinusGix/obsidian-live-preview
- Loading branch information
Showing
5 changed files
with
36 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|