Closed Bulletninja closed 5 years ago
The @mdx-js/runtime
by default doesn't work with imports, thats why you need some setup in order for them to work with this widget.
https://github.com/karolis-sh/gatsby-mdx/tree/master/packages/netlify-cms-widget-mdx#install
Something like this should work:
import CMS from 'netlify-cms';
import { MdxControl, setupPreview } from 'netlify-cms-widget-mdx';
import PagePreview from './preview-templates/page-preview';
import PostPreview from './preview-templates/post-preview';
import MyComponent from '../components/MyComponent';
CMS.registerPreviewTemplate('pages', PagePreview);
CMS.registerPreviewTemplate('posts', PostPreview);
CMS.registerWidget('mdx', MdxControl, setupPreview({
allowedImports: {
'../../src/components/MyComponent': {
ImportDefault: MyComponent,
}
}
}));
As this is rather verbose I suggest having an index.js
file in your src/components
that re-exports every component that you would like to use in mdx file:
import { MyComponent } from '../../src/components';
<MyComponent />
import CMS from 'netlify-cms';
import { MdxControl, setupPreview } from 'netlify-cms-widget-mdx';
import PagePreview from './preview-templates/page-preview';
import PostPreview from './preview-templates/post-preview';
import * as Components from '../components';
CMS.registerPreviewTemplate('pages', PagePreview);
CMS.registerPreviewTemplate('posts', PostPreview);
CMS.registerWidget('mdx', MdxControl, setupPreview({
allowedImports: {
'../../src/components/MyComponent': {
Import: Components,
}
}
}));
I created a simple components in
src/components/MyComponent
but i get this error:when i try to use it like this:
My
config.yml
:and my cms config:
Am i missing something? 🤔
PS: After clearing cache and rerunning things i get: