-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
KaTeX expressions with escapes and braces throw errors #370
Comments
We ran into this in gatsby-mdx too (actually we sort of run into things like this a lot because of the way the gatsby plugins function). I added some replacement code in a babel plugin that we use to post-process MDX content after remark and hast plugins have run but before we ship the JSX. if (
path.node.openingElement.name.name ===
"annotation" /* && if gatsby-remark-katex is enabled */
) {
const genCode = path.node.children.map(ast => generate.default(ast).code);
path.node.children = [
t.jSXText(
genCode
.join("")
.replace("{", "{")
.replace("}", "}")
)
];
}
There's a similar "javascript string escaping"-related issue in #366. so I wonder if we should keep adding to the list of "non-escaped sections" (code blocks, |
I tried copying that file into a {
"presets": [
"next/babel"
],
"plugins": [
"./build/babelPluginHtmlAttrToJsxAttr.js"
]
} And it didn't work. I tried adding debugging messages and it turns out the On another note, I think I found a bug in your babel plugin implementation: The replace calls with a string will only replace the first occurance (MDN docs), so you will need to replace it to a regex expression and pass the |
yeah, probably because in gatsby-mdx we apply it to the MDX output before returning it for the user's babel config to run on. It's used as some internal infrastructure, not as a plugin inline with whatever's in If I had to guess, I'd assume that the JSX expressions are already compiled to function calls by something in the preset before the plugin even gets to run, thus no JSX expressions means no logs.
cool, thanks for the heads up. iirc, I only manually tested it on a file that had one expression in it and there aren't any tests on the plugin yet because I still haven't released the 0.3.x line while I try to discover more processing issues. |
I finally found out the cause were the backward slashes ( const visit = require('unist-util-visit');
const remarkMath = require('remark-math');
const katex = require('rehype-katex');
module.exports = require('@zeit/next-mdx')({
options: {
mdPlugins: [remarkMath],
hastPlugins: [
katex,
() => tree =>
visit(tree, 'element', node => {
if (node.tagName !== 'annotation') {
return;
}
if (!node.properties || node.properties.encoding !== 'application/x-tex') {
return;
}
const { value } = node.children[0];
// Double escape backward slashes
node.children[0].value = value.replace(/\\/g, '\\\\');
})
]
},
pageExtensions: ['js', 'mdx']
}) The escapes get removed by KaTeX when rendering, but it leaves the original TeX parsed string in an |
I believe this is true for any backslashed content in an MDX file (especially ones that are escape-code related cause trouble). It's also the way I got #366 (comment) working. Wonder if something like this rehype plugin should be included by default? |
This should now be fixed in |
Subject of the issue
I'm trying to setup a Next.js site with mdx and these remark plugins:
[email protected]
@kwangkim/[email protected]
(I couldn't find the module's source on GitHub, but you can view it on this RunKit)And this file under
pages/
:I assume the escapes inside braces (
{}
) are confusing MDX and it's trying to parse them as a expression, which causes the following error:(gist error)
As you can see, it's complaining about a "bad character escape sequence". One way to fix this would be to ignore all the braces and escapes
{}\
inside$
(inlineMath) or$$
(math) blocks.Your environment
@zeit/next-mdx
: 1.2.0@mdx-js/mdx
: 0.15.7next
: 7.0.2-canary.49Steps to reproduce
Create new Next.js project with the following
next.config.js
file:Now, create
pages/foo.mdx
:Now, run
next
and loadlocalhost:3000/foo
in your browser.Expected behaviour
The KaTeX escapes should be ignored even if they are inside braces (
{}
).Actual behaviour
The KaTeX escapes and generally, all the content inside braces doesn't get ignored, causing compiling and React rendering issues.
The text was updated successfully, but these errors were encountered: