Skip to content

Commit b6dbf79

Browse files
authored
Merge pull request #7168 from QwikDev/pr-fix-mdx
fix mdx not rendering
2 parents 6f5cac3 + eb47d90 commit b6dbf79

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

.changeset/long-jobs-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik-city': patch
3+
---
4+
5+
FIX: mdx not rendering

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { BuildContext } from '../types';
55
import { parseFrontmatter } from './frontmatter';
66
import { getExtension } from '../../utils/fs';
77
import type { CompileOptions } from '@mdx-js/mdx';
8+
import { createHash } from 'node:crypto';
89

910
export async function createMdxTransformer(ctx: BuildContext): Promise<MdxTransform> {
1011
const { compile } = await import('@mdx-js/mdx');
@@ -69,11 +70,21 @@ export async function createMdxTransformer(ctx: BuildContext): Promise<MdxTransf
6970
const file = new VFile({ value: code, path: id });
7071
const compiled = await compile(file, options);
7172
const output = String(compiled.value);
72-
const addImport = `import { jsx } from '@builder.io/qwik';\n`;
73-
const newDefault = `
73+
const hasher = createHash('sha256');
74+
const key = hasher
75+
.update(output)
76+
.digest('base64')
77+
.slice(0, 8)
78+
.replace('+', '-')
79+
.replace('/', '_');
80+
const addImport = `import { jsx, _jsxC, RenderOnce } from '@builder.io/qwik';\n`;
81+
const newDefault = `
7482
const WrappedMdxContent = () => {
75-
const content = _createMdxContent({});
76-
return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content;
83+
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, ${JSON.stringify(key)});
84+
if (typeof MDXLayout === 'function'){
85+
return jsx(MDXLayout, {children: content});
86+
}
87+
return content;
7788
};
7889
export default WrappedMdxContent;
7990
`;

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('mdx', async () => {
2626

2727
expect(result).toMatchInlineSnapshot(`
2828
{
29-
"code": "import { jsx } from '@builder.io/qwik';
29+
"code": "import { jsx, _jsxC, RenderOnce } from '@builder.io/qwik';
3030
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "@builder.io/qwik/jsx-runtime";
3131
export const headings = [{
3232
"text": "Hello",
@@ -60,10 +60,13 @@ describe('mdx', async () => {
6060
})]
6161
});
6262
}
63-
63+
6464
const WrappedMdxContent = () => {
65-
const content = _createMdxContent({});
66-
return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content;
65+
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, "eB2HIyA1");
66+
if (typeof MDXLayout === 'function'){
67+
return jsx(MDXLayout, {children: content});
68+
}
69+
return content;
6770
};
6871
export default WrappedMdxContent;
6972
",
@@ -95,7 +98,7 @@ export default function Layout({ children: content }) {
9598

9699
expect(result).toMatchInlineSnapshot(`
97100
{
98-
"code": "import { jsx } from '@builder.io/qwik';
101+
"code": "import { jsx, _jsxC, RenderOnce } from '@builder.io/qwik';
99102
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "@builder.io/qwik/jsx-runtime";
100103
export const headings = [{
101104
"text": "Hello",
@@ -134,10 +137,13 @@ export default function Layout({ children: content }) {
134137
})]
135138
});
136139
}
137-
140+
138141
const WrappedMdxContent = () => {
139-
const content = _createMdxContent({});
140-
return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content;
142+
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, "UdQmQWC3");
143+
if (typeof MDXLayout === 'function'){
144+
return jsx(MDXLayout, {children: content});
145+
}
146+
return content;
141147
};
142148
export default WrappedMdxContent;
143149
",

0 commit comments

Comments
 (0)