Render latex using stimulus controller.

This commit is contained in:
Jan Böhmer 2022-03-06 15:49:09 +01:00
parent c54336fb47
commit cef74ff35a
8 changed files with 51 additions and 4 deletions

View file

@ -0,0 +1,28 @@
import {Controller} from "@hotwired/stimulus";
import "katex";
import 'katex/dist/katex.css';
import renderMathInElement from "katex/dist/contrib/auto-render";
/* stimulusFetch: 'lazy' */
export default class extends Controller {
connect() {
this.applyLatex();
this.element.addEventListener('markdown:finished', () => this.applyLatex());
}
applyLatex() {
renderMathInElement(this.element, {
delimiters: [
{left: "$$", right: "$$", display: true},
{left: "$", right: "$", display: false},
{left: "\\(", right: "\\)", display: false},
{left: "\\[", right: "\\]", display: true}
]
});
}
mutate() {
this.applyLatex();
}
}