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

static HTML for math node views #61

Open
piszczu4 opened this issue Aug 21, 2023 · 2 comments
Open

static HTML for math node views #61

piszczu4 opened this issue Aug 21, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@piszczu4
Copy link

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 benrbray added the enhancement New feature or request label Aug 22, 2023
@benrbray
Copy link
Owner

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
Copy link
Author

piszczu4 commented Sep 5, 2023

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,
		];
	},

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

No branches or pull requests

2 participants