Skip to content

Commit bc92aab

Browse files
authored
Merge pull request #7277 from double-thinker/custom-components-mdx
fix(mdx): support passing props to MDX content
2 parents b8bfe22 + ecabe28 commit bc92aab

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

.changeset/breezy-lizards-dress.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@builder.io/qwik-city': patch
3+
---
4+
5+
FIX: MDX content now accepts `props`, including `components` that lets you use your own custom components
6+
7+
To add custom components to your MDX content, you can now do this:
8+
9+
```tsx
10+
// routes/example/index.tsx
11+
import Content from './markdown.mdx';
12+
import MyComponent from '../../components/my-component/my-component';
13+
import { component$ } from '@builder.io/qwik';
14+
15+
export default component$(() => <Content components={{ MyComponent }} />);
16+
```
17+
18+
You can also use props in JS expressions. See https://mdxjs.com/docs/using-mdx/#props

packages/qwik-city/src/buildtime/markdown/mdx.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ export async function createMdxTransformer(ctx: BuildContext): Promise<MdxTransf
7979
.replace('/', '_');
8080
const addImport = `import { jsx, _jsxC, RenderOnce } from '@builder.io/qwik';\n`;
8181
const newDefault = `
82-
const WrappedMdxContent = () => {
83-
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, ${JSON.stringify(key)});
82+
const WrappedMdxContent = (props = {}) => {
83+
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, props, 3, null)}, 3, ${JSON.stringify(key)});
8484
if (typeof MDXLayout === 'function'){
8585
return jsx(MDXLayout, {children: content});
8686
}

packages/qwik-city/src/buildtime/markdown/mdx.unit.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ describe('mdx', async () => {
6161
});
6262
}
6363
64-
const WrappedMdxContent = () => {
65-
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, "eB2HIyA1");
64+
const WrappedMdxContent = (props = {}) => {
65+
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, props, 3, null)}, 3, "eB2HIyA1");
6666
if (typeof MDXLayout === 'function'){
6767
return jsx(MDXLayout, {children: content});
6868
}
@@ -138,8 +138,8 @@ export default function Layout({ children: content }) {
138138
});
139139
}
140140
141-
const WrappedMdxContent = () => {
142-
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, "UdQmQWC3");
141+
const WrappedMdxContent = (props = {}) => {
142+
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, props, 3, null)}, 3, "UdQmQWC3");
143143
if (typeof MDXLayout === 'function'){
144144
return jsx(MDXLayout, {children: content});
145145
}

0 commit comments

Comments
 (0)