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

MDX Field serialises latex expression #1319

Open
shmmh opened this issue Sep 28, 2024 · 1 comment
Open

MDX Field serialises latex expression #1319

shmmh opened this issue Sep 28, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@shmmh
Copy link

shmmh commented Sep 28, 2024

So the Mdx content editor serialises latex expressions. I guess it serialises all the JS expressions.
image

image

Acorn parse error is thrown when I modify the file locally.
image

The issue might be with Prosemirror and/or with Mdx. Without remark-math plugin, even my vscode editor throws an acron parse error.

I found Prosemirror Math extension.

@ethan-ou
Copy link

I had the same problem and I found it was due to acorn escaping curly braces {} to avoid text being converted into MDX expressions. In MDX you can add variables to your markdown as soon as you add the braces:

export const year = 2023
In {year}, the snowfall was above average.

Acorn escapes these but the unintended consequence is that curly braces in math equations no longer work.

The way I found around this is to create a remark plugin to remove the escapes before they're rendered. Here's the plugin:

// You'll need to install unist-util-visit, mdast and mdast-util-math as dependencies to use this plugin.
import { visit } from "unist-util-visit";
import type { Root } from "mdast";
import type { InlineMath } from "mdast-util-math";

const replaceEscapes = (text: string) => text.replaceAll("\\_", "_").replaceAll("\\{", "{");

export default function remarkMathRemoveEscapes() {
  return (tree: Root) => {
    visit(tree, "inlineMath", (node: InlineMath) => {
      node.value = replaceEscapes(node.value);
      if (node.data && node.data.hChildren) {
        const children = [];
        for (const child of node.data.hChildren) {
          child.value = replaceEscapes(child.value);
          children.push(child);
        }

        node.data.hChildren = children;
      }
    });
  };
}

And here's how I'm using it in an Astro project:

import { defineConfig } from "astro/config";
import remarkMathRemoveEscapes from "./extensions/remark-math-remove-escapes/src";
import remarkMath from "remark-math";
import rehypeKatex from "rehype-katex";
import "katex/dist/contrib/mhchem.mjs";

// https://astro.build/config
export default defineConfig({
// more config here...
  markdown: {
    remarkPlugins: [[remarkMath, { singleDollarTextMath: false }], remarkMathRemoveEscapes],
    rehypePlugins: [rehypeKatex],
  },
});

The result is you can use Keystatic to write Latex and it'll render correctly on your site. But you'll need to escape any curly braces to write Latex in markdown.

@emmatown emmatown added the enhancement New feature or request label Dec 9, 2024
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

3 participants