-
Notifications
You must be signed in to change notification settings - Fork 1.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
fix mdx not rendering #7168
base: main
Are you sure you want to change the base?
fix mdx not rendering #7168
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@builder.io/qwik-city': patch | ||
--- | ||
|
||
FIX: mdx not rendering |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import type { BuildContext } from '../types'; | |
import { parseFrontmatter } from './frontmatter'; | ||
import { getExtension } from '../../utils/fs'; | ||
import type { CompileOptions } from '@mdx-js/mdx'; | ||
import { createHash } from 'node:crypto'; | ||
|
||
export async function createMdxTransformer(ctx: BuildContext): Promise<MdxTransform> { | ||
const { compile } = await import('@mdx-js/mdx'); | ||
|
@@ -69,11 +70,21 @@ export async function createMdxTransformer(ctx: BuildContext): Promise<MdxTransf | |
const file = new VFile({ value: code, path: id }); | ||
const compiled = await compile(file, options); | ||
const output = String(compiled.value); | ||
const addImport = `import { jsx } from '@builder.io/qwik';\n`; | ||
const newDefault = ` | ||
const hasher = createHash('sha256'); | ||
const key = hasher | ||
.update(output) | ||
.digest('base64') | ||
.slice(0, 8) | ||
.replace('+', '-') | ||
.replace('/', '_'); | ||
const addImport = `import { jsx, _jsxC, RenderOnce } from '@builder.io/qwik';\n`; | ||
const newDefault = ` | ||
const WrappedMdxContent = () => { | ||
const content = _createMdxContent({}); | ||
return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content; | ||
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, ${JSON.stringify(key)}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are only added to the v1 codebase right? Otherwise in v2 these would break with different transform functions being used like jsxsplit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
if (typeof MDXLayout === 'function'){ | ||
return jsx(MDXLayout, {children: content}); | ||
} | ||
return content; | ||
}; | ||
export default WrappedMdxContent; | ||
`; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ describe('mdx', async () => { | |
|
||
expect(result).toMatchInlineSnapshot(` | ||
{ | ||
"code": "import { jsx } from '@builder.io/qwik'; | ||
"code": "import { jsx, _jsxC, RenderOnce } from '@builder.io/qwik'; | ||
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "@builder.io/qwik/jsx-runtime"; | ||
export const headings = [{ | ||
"text": "Hello", | ||
|
@@ -60,10 +60,13 @@ describe('mdx', async () => { | |
})] | ||
}); | ||
} | ||
|
||
const WrappedMdxContent = () => { | ||
const content = _createMdxContent({}); | ||
return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content; | ||
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, "eB2HIyA1"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. great job on the unit test. Looks like if we do run into a problem with the transforms we'll know pretty quickly with this |
||
if (typeof MDXLayout === 'function'){ | ||
return jsx(MDXLayout, {children: content}); | ||
} | ||
return content; | ||
}; | ||
export default WrappedMdxContent; | ||
", | ||
|
@@ -95,7 +98,7 @@ export default function Layout({ children: content }) { | |
|
||
expect(result).toMatchInlineSnapshot(` | ||
{ | ||
"code": "import { jsx } from '@builder.io/qwik'; | ||
"code": "import { jsx, _jsxC, RenderOnce } from '@builder.io/qwik'; | ||
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "@builder.io/qwik/jsx-runtime"; | ||
export const headings = [{ | ||
"text": "Hello", | ||
|
@@ -134,10 +137,13 @@ export default function Layout({ children: content }) { | |
})] | ||
}); | ||
} | ||
|
||
const WrappedMdxContent = () => { | ||
const content = _createMdxContent({}); | ||
return typeof MDXLayout === 'function' ? jsx(MDXLayout, {children: content}) : content; | ||
const content = _jsxC(RenderOnce, {children: _jsxC(_createMdxContent, {}, 3, null)}, 3, "UdQmQWC3"); | ||
if (typeof MDXLayout === 'function'){ | ||
return jsx(MDXLayout, {children: content}); | ||
} | ||
return content; | ||
}; | ||
export default WrappedMdxContent; | ||
", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ok if we rely on node here for the hash?
Maybe something like this is preferred
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it's in build time, I think it's OK (I also checked and this should be supported by both deno and bun)