-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
MDXRenderer: components available globally as shortcodes not working #22257
Comments
I believe the correct place for your custom components to work is to put them into MDXProvider, @apolopena |
To expand on @flikteoh's comment... In your code, you are passing your components to From the docs:
// src/components/layout.js
import React from "react"
import { MDXProvider } from "@mdx-js/react"
import { Chart, Pullquote, Message } from "./ui"
const shortcodes = { Chart, Pullquote, Message }
export default ({ children }) => (
<MDXProvider components={shortcodes}>{children}</MDXProvider>
) The You can specify the default layout component for different types of mdx pages in the plugin options. // in gatsby-config.js
plugins: [
{
resolve: `gatsby-plugin-mdx`,
options: {
defaultLayouts: {
default: require.resolve("./src/components/layout.js"),
posts: require.resolve("./src/components/posts-layout.js"),
},
},
},
] |
Thank you for opening this! As others have said you'll need to use We're marking this issue as answered and closing it for now but please feel free to comment here if you would like to continue this discussion. We also recommend heading over to our communities if you have questions that are not bug reports or feature requests. We hope we managed to help and thank you for using Gatsby! |
Thanks to all of you. Turns out I did not read the docs properly. It is useful to see #21682 |
Looks like everyone here gets it, but I'll just post this for the next soul trying to use it with MDXRenderer. You want your MDXProvider to wrap your MDXRenderer. Then you will have access to all your components
|
Description
I am trying to get JSX components to work globally in my markdown files for my blog as per the documentation: Importing and Using Components in MDX
The proper html is not being rendered from my custom component inthe markdown, Rather I get a plain
When I import my custom component into a .mdx file in src/pages and import the component from within the .md file as per import-components-for-use-from-another-library the proper html is rendered:
I have simplified my test to putting a
<MDXRenderer>
right in theblog.js
file which resides insrc/pages
to rule out any issues with pathing andcreatePages
ingatsby-node.js
The contents of my blog.js is:
My custom component is here:
src/components/ImgCaption.js
and looks like this:
My
gatsby-config.js
looks like this:Steps to reproduce
test.md
in/content/assets/blog
test.md
contains the following content:Expected result
The following html should be rendered from the custom component:
Actual result
The following html is rendered for the html component:
Environment
System:
OS: Windows 10 10.0.18362
CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
Binaries:
Node: 12.4.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.19.1 - ~\AppData\Roaming\npm\yarn.CMD
npm: 6.13.2 - C:\Program Files\nodejs\npm.CMD
Languages:
Python: 2.7.16 - /c/Python27/python
Browsers:
Edge: 44.18362.449.0
npmPackages:
gatsby: ^2.3.25 => 2.19.34
gatsby-image: ^2.0.39 => 2.2.43
gatsby-plugin-feed-mdx: ^1.0.0 => 1.0.1
gatsby-plugin-google-analytics: ^2.0.18 => 2.1.37
gatsby-plugin-manifest: ^2.0.29 => 2.2.45
gatsby-plugin-mdx: ^1.0.52 => 1.0.78
gatsby-plugin-netlify-cms: ^4.0.0 => 4.1.41
gatsby-plugin-offline: ^2.0.25 => 2.2.10
gatsby-plugin-react-helmet: ^3.0.12 => 3.1.23
gatsby-plugin-sass: ^2.1.30 => 2.1.30
gatsby-plugin-sharp: ^2.0.35 => 2.4.8
gatsby-plugin-styled-components: ^3.0.7 => 3.1.20
gatsby-plugin-typography: ^2.2.13 => 2.3.24
gatsby-remark-copy-linked-files: ^2.0.11 => 2.1.39
gatsby-remark-external-links: ^0.0.4 => 0.0.4
gatsby-remark-images: ^2.0.6 => 2.0.6
gatsby-remark-responsive-iframe: ^2.1.1 => 2.2.33
gatsby-remark-smartypants: ^2.0.9 => 2.1.22
gatsby-remark-vscode: ^1.0.4 => 1.4.0
gatsby-source-filesystem: ^2.0.29 => 2.1.51
gatsby-transformer-sharp: ^2.1.18 => 2.3.17
The text was updated successfully, but these errors were encountered: