Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for async KaTeX #22

Open
gzuidhof opened this issue Apr 8, 2021 · 1 comment
Open

Support for async KaTeX #22

gzuidhof opened this issue Apr 8, 2021 · 1 comment
Labels
enhancement New feature or request high-priority

Comments

@gzuidhof
Copy link
Sponsor

gzuidhof commented Apr 8, 2021

Hi benrbray,

Thank you for this library, I have integrated it into Starboard Notebook (you can try it here). Before the NPM package was released I vendored all the code (read: copy pasted), but now that it's out I would love to get rid of my own version.

I was only able to remove about half of the code: in Starboard Notebook I load as much stuff asynchronously as possible to keep the initial load and render time fast, KaTeX included.

Basically the only change I made is this in NodeView:

katexLoader.then((katex) => {
    if (!this._mathRenderElt) return;
    // render katex, but fail gracefully
    try {
        katex.render(texString, this._mathRenderElt, this._katexOptions);
        this._mathRenderElt.classList.remove("parse-error");
        this.dom.setAttribute("title", "");
    } catch (err) {
        if (err instanceof ParseError) {
            console.error(err);
            this._mathRenderElt.classList.add("parse-error");
            this.dom.setAttribute("title", err.toString());
        } else {
            throw err;
        }
    }
});

I don't know the best way to fit this into the library.

Perhaps instead of importing katex directly one could pass an object with this signature:

interface MathRenderer {
  render(value: string, element: HTMLElement, rendererOptions: any): Promise<{ok: true} | {ok: false, error: Error}>
}

I saw in another issue that you wanted to support Mathjax as well, this would be a step towards supporting that too.

@benrbray benrbray added enhancement New feature or request high-priority labels Apr 11, 2021
@benrbray
Copy link
Owner

benrbray commented Apr 19, 2021

fyi I'm actively working on this (outside of my day job), trying to put some care into how options might be passed to/from NodeViews.

In addition to your async KaTeX use case, I want to support MathJax with the next release. Right now some of the KaTeX-specific design decisions are tangled up with the NodeView code. MathJax is a bit of a behemoth, so I'm trying to understand the various ways that MathJax (as well as KaTeX or other math renderers) might be included in an application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request high-priority
Projects
None yet
Development

No branches or pull requests

2 participants