benrbray / prosemirror-math

Schema and plugins for "first-class" math support in ProseMirror!
https://benrbray.com/prosemirror-math/
MIT License
258 stars 36 forks source link

static HTML for math node views #61

Open piszczu4 opened 1 year ago

piszczu4 commented 1 year ago

How to generate rendered math for static html? Currently math is nicely rendered in editing mode when it's not rendered when using DOMSerializer to get static HTML

benrbray commented 1 year ago

At the moment, this isn't implemented, I will keep it in mind for the next release. (a PR would be welcome!)

In the meantime, I recommend writing a ProseMirror plugin that calls KaTeX on the math node during (or after) DOM serialization.

piszczu4 commented 1 year ago

Hi, I solved it in the following way. Do you have any suggestions? It's written in Tiptap framework btw


export const renderMathHTML = (displayMode: boolean) => (node: Node) => {
    let dom = document.createElement(
        displayMode ? "math-display" : "math-inline"
    );
    dom.className = "math-node";

    let tex = node.textContent;
    let src = document.createElement("span");
    src.className = "math-src";
    src.innerText = tex;

    let render = document.createElement("span");
    render.className = "math-render";

    katex.render(tex, render, {
        displayMode: displayMode,
        globalGroup: true,
    });

    dom.appendChild(src);
    dom.appendChild(render);

    return dom;
};
    parseHTML() {
        return [
            {
                tag: "math-inline",
                contentElement: "span.math-src",
            },
            ...defaultInlineMathParseRules,
        ];
    },