2022-03-06 15:49:09 +01:00
|
|
|
import {Controller} from "@hotwired/stimulus";
|
|
|
|
|
2022-03-06 16:09:24 +01:00
|
|
|
//import "katex";
|
2022-03-06 15:49:09 +01:00
|
|
|
import 'katex/dist/katex.css';
|
2022-03-06 16:09:24 +01:00
|
|
|
import {auto} from "@popperjs/core";
|
|
|
|
//import renderMathInElement from "katex/dist/contrib/auto-render";
|
2022-03-06 15:49:09 +01:00
|
|
|
|
|
|
|
export default class extends Controller {
|
|
|
|
connect() {
|
|
|
|
this.applyLatex();
|
|
|
|
this.element.addEventListener('markdown:finished', () => this.applyLatex());
|
|
|
|
}
|
|
|
|
|
|
|
|
applyLatex() {
|
2022-03-06 16:09:24 +01:00
|
|
|
//Only import the katex library, if we have an delimiter string in our element text
|
|
|
|
let str = this.element.textContent;
|
|
|
|
if(str.match(/(\$|\\\(|\\\[).+(\$|\\\)|\\\])/)) {
|
|
|
|
import('katex/dist/contrib/auto-render').then((autorender) => {
|
|
|
|
//This calls renderMathInElement()
|
|
|
|
autorender.default(this.element, {
|
|
|
|
delimiters: [
|
|
|
|
{left: "$$", right: "$$", display: true},
|
|
|
|
{left: "$", right: "$", display: false},
|
|
|
|
{left: "\\(", right: "\\)", display: false},
|
|
|
|
{left: "\\[", right: "\\]", display: true}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-06 15:49:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mutate() {
|
|
|
|
this.applyLatex();
|
|
|
|
}
|
|
|
|
}
|