-
I have a Nextjs-based blog, where I use MDX for the posts. Posts can be displayed in the post detail, where the whole content is rendered, or the posts list, where just the "first lines" are displayed. With MDX 1 I used to do this by providing a Something in these lines: import { Children } from 'react';
import { MDXProvider } from '@mdx-js/react';
import dynamic from 'next/dynamic';
const wrapper = ({ children }) => <>{Children.toArray(children).slice(0, 5)}</>;
export const PostPreview = ({ post }) => {
const Content = dynamic(() => import(`../posts/${post.fileName}`));
return (
<MDXProvider components={{ wrapper }}>
<Content />
</MDXProvider>
);
}; This does not seem to be possible anymore with MDX 2, as it seems to pass just one wrapping component when using the This issue seems to be somehow related: #2029
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Indeed, it’s impossible. On your question:
|
Beta Was this translation helpful? Give feedback.
-
From @wooorm |
Beta Was this translation helpful? Give feedback.
-
I had a similar problem — a slide deck where I want to grab the JSX elements from my MDX file, group them based on some rules, then dynamically control which slide(s) are shown on screen. In MDX1, I did the same thing you did — used In MDX2, I added a little one-off remark plugin that wraps the entire result in my component: const wrapIt = () => {
const transformer = (tree) => {
const wrapItNode = {
type: 'mdxJsxFlowElement',
name: 'MyAwesomeComponent',
attributes: [],
children: tree.children,
};
tree.children = [wrapItNode];
};
return transformer;
}; And then I pass that component via the previous This allowed me to upgrade to MDX2 with minimal changes. |
Beta Was this translation helpful? Give feedback.
From @wooorm
#2189 (reply in thread)