gatsby: [gatsby-remark-katex] Doesn’t work with gatsby-plugin-mdx
Description
I followed the instructions to install gatsby-remark-katex
on a website using gatsby-plugin-mdx
and it didn’t work. No errors, no nothing, it just didn’t transform the LaTeX in the Markdown.
Steps to reproduce
In this zip there are two projects:
doesnt-work
: This is a minimal project to show the issue. To create this project I just followed the instructions to install gatsby-remark-katex
.
works
: This is a workaround I found based on this comment and the documentation for remark-math
, in which they suggest to use rehype-katex
instead of remark-html-katex
.
You can run the projects with the usual npm install && npx gatsby develop
and visit http://localhost:8000
.
In summary, the difference is instead of:
// doesnt-work
$ npm install gatsby-transformer-remark gatsby-remark-katex katex
// gatsby-config.js
{
resolve: `gatsby-plugin-mdx`,
options: {
gatsbyRemarkPlugins: [`gatsby-remark-katex`]
}
}
you do:
// works
$ npm install remark-math rehype-katex katex
// gatsby-config.js
{
resolve: `gatsby-plugin-mdx`,
options: {
remarkPlugins: [require(`remark-math`)],
rehypePlugins: [require(`rehype-katex`)]
}
}
Expected result
The LaTeX in the Markdown should be rendered.
Actual result
The LaTeX in the Markdown appears unmodified.
Environment
System:
OS: macOS 10.15.3
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Shell: 5.7.1 - /bin/zsh
Binaries:
Node: 13.7.0 - /usr/local/bin/node
npm: 6.14.1 - /usr/local/bin/npm
Languages:
Python: 2.7.16 - /usr/bin/python
Browsers:
Chrome: 80.0.3987.122
Safari: 13.0.5
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 23
- Comments: 39 (21 by maintainers)
Commits related to this issue
- Fix issue with gatsby-plugin-mdx not picking up gatsbyRemarkPlugins. Fixes #21866. — committed to danedavid/gatsby by danedavid 4 years ago
- Fix issue with gatsby-plugin-mdx not picking up gatsbyRemarkPlugins. Fixes #21866. (#24194) — committed to gatsbyjs/gatsby by danedavid 4 years ago
@jannikbuschke
Unfortunately not yet. The root issue is that
remark-math
which is a core dependency ofgatsby-remark-katex
requires remark 13 to get working.However then it seems to need updating
@mdx-js/mdx
to 2.0.0 first and it’d be a bit heavy work to get done.I agree with you. It should work unless such incompatibility. But thus we need some workaround for right now.
Since it’s not a specific issue only with
gatsby-remark-katex
but more about general version compatibility, it maybe more helpful if it was somehow documented.Workaround
FYI, here is the workaround to enable KaTeX with
gatsby-plugin-mdx
.There are two points.
remark-math
andrehype-katex
insted ofgatsby-remark-katex
Version
gatsby@4.9.3
gatsby-plugin-mdx@3.9.1
Snippet
gatsby-config.js
gatsby-browser.js
Note that
katex
module comes withrehype-katex
therefor it doesn’t have to be installed individually.I tried it with the latest versions and the problem remains. Here’s my
package.json
:To try it for yourself, just use this
package.json
on the originaldoesnt-work
reproduction project I posted in my original message.It seems this is not properly documented yet and maybe the root issue is not yet resolved?
When digging through this thread I also faced issues with the recommend solutions, i.e. require(‘remark-math’), as require is not allowed.
And the different plugins (remark-math, remark-html-katex, gatsby-remark-katex, rehype-katex) also confuse me.
Am I assuming correct, that what is currently documented
is the way it should work, but does not?
@tnorlund @princefishthrower
What ended fixing the problem for me (march/2021 -
"gatsby": "^3.1.0", "gatsby-plugin-mdx": "^2.1.0",
) was explicitly including an older version of"remark-math": "^3.0.1",
instead of the latest version which is 4 version 4 relies on the newest version of remark with all of the newest bells and whistles, while gatsby-plugin-mdx still relies on remark v12. I hope this helps someone.I believe that’s the intended HTML output. You may be missing the CSS, which hides one of the two depending on the browser’s capabilities.
Hmm, funny. I have had this issue before and was using the workaround for a while. Not sure what has changed since then but the usual instructions work for me now
In
gatsby-config.js
,and in
gatsby-browser.js
For someone who wants to inspect further, the complete source is here.
@danedavid: Here’s what I’d start with:
Setup the local development environment.
Modify the sample website
doesnt-work
that I included in this issue to use the local Gatsby installation. (Usenpm link
.)Check that the
remark-math
parser plugin is being picked up. I’d modify the line to read something like:Check that the
gatsby-remark-katex
plugin itself is being picked up. I’d add aconsole.log()
there as well.Check that the visitors are finding the math nodes and producing appropriate results.
I’d expect the problem to become evident somewhere along this process.
Warning: Take what I’m saying with a grain of salt, as I’ve never contributed to the Gatsby codebase myself.
For What It’s Worth, this is the setup that worked for me:
Dependencies:
Gatsby Configuration:
Example in .mdx:
This is what I have so far. It works, but I cannot write ‘complex’ equations.
@tnorlund @princefishthrower I had the same problem and here’s the package versions I used and my
gatsby-config.js
. Hope it helps!