Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
bfc399e1
Commit
bfc399e1
authored
Mar 06, 2026
by
Jimmy Lai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Simplify markdown renderer integration
parent
4df3d7e0
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
1422 additions
and
439 deletions
+1422
-439
README.md
packages/next-markdown/README.md
+3
-5
package.json
packages/next-markdown/package.json
+2
-1
index.ts
packages/next-markdown/src/index.ts
+895
-97
app-render.tsx
packages/next/src/server/app-render/app-render.tsx
+167
-156
create-component-tree.tsx
...ages/next/src/server/app-render/create-component-tree.tsx
+7
-3
config.ts
packages/next/src/server/markdown/config.ts
+65
-0
renderer.test.tsx
packages/next/src/server/markdown/renderer.test.tsx
+132
-66
runtime.ts
packages/next/src/server/markdown/runtime.ts
+33
-6
render.tsx
packages/next/src/server/render.tsx
+97
-101
pnpm-lock.yaml
pnpm-lock.yaml
+21
-4
No files found.
packages/next-markdown/README.md
View file @
bfc399e1
...
...
@@ -10,11 +10,10 @@ change without notice.
## What it does
This package contains the generic React and HTML to Markdown logic used by
Next.js:
This package contains the generic React-to-Markdown logic used by Next.js:
-
React tree instrumentation for Markdown-specific markers
-
HTML to Markdown serialization
-
direct React tree rendering into an in-memory Markdown tree
-
root and segment-level component overrides
-
default heuristics for omitting interactive controls
...
...
@@ -26,7 +25,7 @@ Next.js-specific routing, content negotiation, and route export handling live in
The package currently exposes:
-
`markReactNode`
-
`render
Html
ToMarkdown`
-
`render
React
ToMarkdown`
-
marker constants
-
Markdown component and segment types
...
...
@@ -35,4 +34,3 @@ The package currently exposes:
```
bash
pnpm
--filter
@next/markdown build
```
packages/next-markdown/package.json
View file @
bfc399e1
...
...
@@ -24,7 +24,8 @@
"directory"
:
"packages/next-markdown"
},
"dependencies"
:
{
"node-html-parser"
:
"7.0.1"
"node-html-parser"
:
"7.0.1"
,
"react-reconciler"
:
"0.33.0"
},
"peerDependencies"
:
{
"react"
:
"^18.2.0 || ^19.0.0"
...
...
packages/next-markdown/src/index.ts
View file @
bfc399e1
This diff is collapsed.
Click to expand it.
packages/next/src/server/app-render/app-render.tsx
View file @
bfc399e1
This diff is collapsed.
Click to expand it.
packages/next/src/server/app-render/create-component-tree.tsx
View file @
bfc399e1
...
...
@@ -446,7 +446,8 @@ async function createComponentTreeInternal(
:
parentOptionalCatchAllParamName
// Resolve the segment param
const
isSegmentViewEnabled
=
!!
process
.
env
.
__NEXT_DEV_SERVER
const
isSegmentViewEnabled
=
!!
process
.
env
.
__NEXT_DEV_SERVER
&&
!
ctx
.
markdownSegmentByComponent
const
dir
=
(
process
.
env
.
NEXT_RUNTIME
===
'edge'
?
process
.
env
.
__NEXT_EDGE_PROJECT_DIR
...
...
@@ -669,13 +670,15 @@ async function createComponentTreeInternal(
)
// Convert the parallel route map into an object after all promises have been resolved.
const
isMarkdownRender
=
!!
ctx
.
markdownSegmentByComponent
let
parallelRouteProps
:
{
[
key
:
string
]:
React
.
ReactNode
}
=
{}
let
parallelRouteCacheNodeSeedData
:
{
[
key
:
string
]:
CacheNodeSeedData
|
null
}
=
{}
for
(
const
parallelRoute
of
parallelRouteMap
)
{
const
[
parallelRouteKey
,
parallelRouteProp
,
flightData
]
=
parallelRoute
parallelRouteProps
[
parallelRouteKey
]
=
parallelRouteProp
parallelRouteProps
[
parallelRouteKey
]
=
isMarkdownRender
&&
flightData
?
flightData
[
0
]
:
parallelRouteProp
parallelRouteCacheNodeSeedData
[
parallelRouteKey
]
=
flightData
}
...
...
@@ -1234,7 +1237,8 @@ async function createBoundaryConventionElement({
const
{
componentMod
:
{
createElement
,
Fragment
},
}
=
ctx
const
isSegmentViewEnabled
=
!!
process
.
env
.
__NEXT_DEV_SERVER
const
isSegmentViewEnabled
=
!!
process
.
env
.
__NEXT_DEV_SERVER
&&
!
ctx
.
markdownSegmentByComponent
const
dir
=
(
process
.
env
.
NEXT_RUNTIME
===
'edge'
?
process
.
env
.
__NEXT_EDGE_PROJECT_DIR
...
...
packages/next/src/server/markdown/config.ts
0 → 100644
View file @
bfc399e1
import
type
{
MarkdownComponents
}
from
'./runtime'
export
type
MarkdownRouteConfig
<
TRender
=
(...
args
:
any
[])
=>
unknown
>
=
{
enabled
:
boolean
components
:
MarkdownComponents
render
?:
TRender
}
export
function
loadMarkdownRootComponents
():
MarkdownComponents
{
if
(
process
.
env
.
NEXT_RUNTIME
===
'edge'
)
{
return
{}
}
return
(
require
(
'../../lib/require-markdown-components'
)
as
typeof
import
(
'../../lib/require-markdown-components'
)
).
requireMarkdownComponents
()
}
export
function
getMarkdownRouteConfig
<
TRender
=
(...
args
:
any
[])
=>
unknown
,
>
(
routeModule
:
{
markdown
?:
unknown
generateMarkdown
?:
unknown
}):
MarkdownRouteConfig
<
TRender
>
{
const
markdown
=
normalizeMarkdownRouteConfig
(
routeModule
.
markdown
)
const
render
=
typeof
routeModule
.
generateMarkdown
===
'function'
?
(
routeModule
.
generateMarkdown
as
TRender
)
:
undefined
return
{
enabled
:
markdown
.
enabled
||
!!
render
,
components
:
markdown
.
components
,
render
,
}
}
function
normalizeMarkdownRouteConfig
(
markdown
:
unknown
):
{
enabled
:
boolean
components
:
MarkdownComponents
}
{
if
(
markdown
===
true
)
{
return
{
enabled
:
true
,
components
:
{},
}
}
if
(
markdown
&&
typeof
markdown
===
'object'
)
{
return
{
enabled
:
true
,
components
:
'components'
in
markdown
&&
markdown
.
components
&&
typeof
markdown
.
components
===
'object'
?
(
markdown
.
components
as
MarkdownComponents
)
:
{},
}
}
return
{
enabled
:
false
,
components
:
{},
}
}
packages/next/src/server/markdown/renderer.test.tsx
View file @
bfc399e1
/* eslint-disable @next/internal/no-ambiguous-jsx -- Test fixtures use concise JSX trees to exercise React-to-Markdown instrumentation. */
import
React
from
'react'
import
{
renderToStaticMarkup
}
from
'react-dom/server'
import
{
MARKDOWN_SEGMENT_MARKER_TAG
,
markReactNode
,
render
Html
ToMarkdown
,
render
React
ToMarkdown
,
type
MarkdownSegmentDefinition
,
}
from
'../../../../next-markdown/src'
describe
(
'markdown renderer'
,
()
=>
{
it
(
'serializes basic markdown blocks'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
[
'<h1>Hello</h1>'
,
'<p><strong>World</strong> <a href="/docs">docs</a></p>'
,
'<pre><code class="language-ts">const value = 1</code></pre>'
,
'<ul><li>One</li><li>Two</li></ul>'
,
].
join
(
''
)
const
markdown
=
await
renderReactToMarkdown
(
<>
<
h1
>
Hello
</
h1
>
<
p
>
<
strong
>
World
</
strong
>
<
a
href=
"/docs"
>
docs
</
a
>
</
p
>
<
pre
>
<
code
className=
"language-ts"
>
const value = 1
</
code
>
</
pre
>
<
ul
>
<
li
>
One
</
li
>
<
li
>
Two
</
li
>
</
ul
>
</>
)
expect
(
markdown
).
toBe
(
...
...
@@ -24,21 +31,44 @@ describe('markdown renderer', () => {
)
})
it
(
'normalizes full-document html before serializing markdown'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
'<!DOCTYPE html><html><body><h1>Hello</h1><p>World</p></body></html>'
it
(
'serializes dangerouslySetInnerHTML without requiring a top-level html render'
,
async
()
=>
{
const
markdown
=
await
renderReactToMarkdown
(
<
div
dangerouslySetInnerHTML=
{
{
__html
:
'<h1>Hello</h1><p>World</p>'
,
}
}
/>
)
expect
(
markdown
).
toBe
(
'# Hello
\
n
\
nWorld'
)
})
it
(
'serializes images, tables, and nested lists'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
[
'<img src="/logo.png" alt="Logo" />'
,
'<table><tr><th>Name</th><th>Value</th></tr><tr><td>A</td><td>1</td></tr></table>'
,
'<ul><li>One<ul><li>Nested</li></ul></li><li>Two</li></ul>'
,
].
join
(
''
)
const
markdown
=
await
renderReactToMarkdown
(
<>
<
img
src=
"/logo.png"
alt=
"Logo"
/>
<
table
>
<
tbody
>
<
tr
>
<
th
>
Name
</
th
>
<
th
>
Value
</
th
>
</
tr
>
<
tr
>
<
td
>
A
</
td
>
<
td
>
1
</
td
>
</
tr
>
</
tbody
>
</
table
>
<
ul
>
<
li
>
One
<
ul
>
<
li
>
Nested
</
li
>
</
ul
>
</
li
>
<
li
>
Two
</
li
>
</
ul
>
</>
)
expect
(
markdown
).
toBe
(
...
...
@@ -54,11 +84,9 @@ describe('markdown renderer', () => {
</
div
>
)
const
html
=
renderToStaticMarkup
(
markReactNode
(<
Interactive
/>)
as
React
.
ReactElement
)
const
withoutOverride
=
await
renderHtmlToMarkdown
(
html
)
const
withOverride
=
await
renderHtmlToMarkdown
(
html
,
{
const
marked
=
markReactNode
(<
Interactive
/>)
as
React
.
ReactElement
const
withoutOverride
=
await
renderReactToMarkdown
(
marked
)
const
withOverride
=
await
renderReactToMarkdown
(
marked
,
{
rootComponents
:
{
div
({
attributes
,
children
,
renderDefault
})
{
if
(
attributes
[
'data-react-markdown-interactive'
]
===
'true'
)
{
...
...
@@ -82,20 +110,19 @@ describe('markdown renderer', () => {
return
<
button
>
Download
</
button
>
}
const
html
=
renderToStaticMarkup
(
markReactNode
(<
FancyButton
/>)
as
React
.
ReactElement
)
const
markdown
=
await
renderHtmlToMarkdown
(
html
,
{
rootComponents
:
{
button
()
{
return
'tag override'
},
FancyButton
()
{
return
'component override'
const
markdown
=
await
renderReactToMarkdown
(
markReactNode
(<
FancyButton
/>),
{
rootComponents
:
{
button
()
{
return
'tag override'
},
FancyButton
()
{
return
'component override'
},
},
}
,
}
)
}
)
expect
(
markdown
).
toBe
(
'component override'
)
})
...
...
@@ -110,32 +137,34 @@ describe('markdown renderer', () => {
return
<
p
>
Memo note
</
p
>
})
const
html
=
renderToStaticMarkup
(
const
markdown
=
await
renderReactToMarkdown
(
markReactNode
(
<>
<
FancyAction
/>
<
MemoNote
/>
</>
)
as
React
.
ReactElement
)
const
markdown
=
await
renderHtmlToMarkdown
(
html
,
{
rootComponents
:
{
FancyAction
()
{
return
'forward-ref override'
},
MemoNote
()
{
return
'memo override'
),
{
rootComponents
:
{
FancyAction
()
{
return
'forward-ref override'
},
MemoNote
()
{
return
'memo override'
},
},
}
,
}
)
}
)
expect
(
markdown
).
toBe
(
'forward-ref override
\
n
\
nmemo override'
)
})
it
(
'allows overrides to omit output by returning null'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
'<p>Keep me?</p><img src="/x.png" alt="X" />'
,
const
markdown
=
await
renderReactToMarkdown
(
<>
<
p
>
Keep me?
</
p
>
<
img
src=
"/x.png"
alt=
"X"
/>
</>,
{
rootComponents
:
{
p
()
{
...
...
@@ -149,8 +178,10 @@ describe('markdown renderer', () => {
})
it
(
'preserves inline spacing and formatting for paragraph overrides'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
'<p>Hello <strong>wide</strong> world</p>'
,
const
markdown
=
await
renderReactToMarkdown
(
<
p
>
Hello
<
strong
>
wide
</
strong
>
world
</
p
>,
{
rootComponents
:
{
p
({
children
})
{
...
...
@@ -197,16 +228,18 @@ describe('markdown renderer', () => {
],
])
const
markdown
=
await
renderHtmlToMarkdown
(
[
'<react-markdown-segment-marker data-segment-id="layout">'
,
'<section>'
,
'<react-markdown-segment-marker data-segment-id="page">'
,
'<p>Hello</p>'
,
'</react-markdown-segment-marker>'
,
'</section>'
,
'</react-markdown-segment-marker>'
,
].
join
(
''
),
const
markdown
=
await
renderReactToMarkdown
(
React
.
createElement
(
MARKDOWN_SEGMENT_MARKER_TAG
,
{
'data-segment-id'
:
'layout'
},
<
section
>
{
React
.
createElement
(
MARKDOWN_SEGMENT_MARKER_TAG
,
{
'data-segment-id'
:
'page'
},
<
p
>
Hello
</
p
>
)
}
</
section
>
),
{
rootComponents
:
{
p
({
children
})
{
...
...
@@ -220,14 +253,14 @@ describe('markdown renderer', () => {
expect
(
markdown
).
toBe
(
'layout-start
\
npage:Hello
\
nlayout-end'
)
})
it
(
'registers segment props while instrumenting components'
,
()
=>
{
it
(
'registers segment props while instrumenting components'
,
async
()
=>
{
const
captured
:
Array
<
any
>
=
[]
function
Page
(
props
:
{
title
:
string
})
{
return
<
p
>
{
props
.
title
}
</
p
>
}
const
html
=
renderToStaticMarkup
(
const
markdown
=
await
renderReactToMarkdown
(
markReactNode
(<
Page
title=
"Hello"
/>,
{
segmentByComponent
:
new
Map
([
[
...
...
@@ -244,7 +277,40 @@ describe('markdown renderer', () => {
)
expect
(
captured
).
toEqual
([{
title
:
'Hello'
}])
expect
(
html
).
toContain
(
'data-segment-id="page"'
)
expect
(
markdown
).
toBe
(
'Hello'
)
})
it
(
'strips internal segment explorer wrappers from markdown instrumentation'
,
async
()
=>
{
const
SegmentViewNode
=
function
SegmentViewNode
()
{
throw
new
Error
(
'should not be rendered'
)
}
const
SegmentViewStateNode
=
function
SegmentViewStateNode
()
{
throw
new
Error
(
'should not be rendered'
)
}
;(
SegmentViewNode
as
any
).
$$typeof
=
Symbol
.
for
(
'react.client.reference'
)
;(
SegmentViewNode
as
any
).
$$id
=
'/next-devtools/userspace/app/segment-explorer-node#SegmentViewNode'
;(
SegmentViewStateNode
as
any
).
$$typeof
=
Symbol
.
for
(
'react.client.reference'
)
;(
SegmentViewStateNode
as
any
).
$$id
=
'/next-devtools/userspace/app/segment-explorer-node#SegmentViewStateNode'
const
markdown
=
await
renderReactToMarkdown
(
markReactNode
(
<>
{
React
.
createElement
(
SegmentViewNode
as
any
,
null
,
<
p
>
Hello from app route
</
p
>
)
}
{
React
.
createElement
(
SegmentViewStateNode
as
any
)
}
</>
)
)
expect
(
markdown
).
toBe
(
'Hello from app route'
)
})
it
(
'does not invoke client reference component types while instrumenting'
,
()
=>
{
...
...
packages/next/src/server/markdown/runtime.ts
View file @
bfc399e1
...
...
@@ -18,14 +18,35 @@ function getMarkdownRuntime(): MarkdownRuntimeModule {
return
runtime
}
try
{
runtime
=
require
(
'../../../../next-markdown/src'
)
as
typeof
import
(
'../../../../next-markdown/src'
)
}
catch
{
runtime
=
require
(
'@next/markdown'
)
as
typeof
import
(
'@next/markdown'
)
let
loaded
:
MarkdownRuntimeModule
// eslint-disable-next-line no-eval -- Deferred so the packaged build doesn't try to bundle the local workspace source path.
const
dynamicRequire
=
eval
(
'require'
)
as
NodeJS
.
Require
const
fs
=
dynamicRequire
(
'node:fs'
)
as
typeof
import
(
'node:fs'
)
const
path
=
dynamicRequire
(
'node:path'
)
as
typeof
import
(
'node:path'
)
const
candidates
=
[
path
.
resolve
(
__dirname
,
'../../../../next-markdown/dist'
),
path
.
resolve
(
__dirname
,
'../../../../../next-markdown/dist'
),
]
for
(
const
candidate
of
candidates
)
{
if
(
fs
.
existsSync
(
`
${
candidate
}
.js`
)
||
fs
.
existsSync
(
path
.
join
(
candidate
,
'index.js'
))
)
{
loaded
=
dynamicRequire
(
candidate
)
as
MarkdownRuntimeModule
runtime
=
loaded
return
loaded
}
}
return
runtime
const
packageName
=
'@next/markdown'
loaded
=
dynamicRequire
(
packageName
)
as
MarkdownRuntimeModule
runtime
=
loaded
return
loaded
}
export
const
MARKDOWN_COMPONENT_MARKER_TAG
=
'react-markdown-component-marker'
...
...
@@ -44,6 +65,12 @@ export function renderHtmlToMarkdown(
return
getMarkdownRuntime
().
renderHtmlToMarkdown
(...
args
)
}
export
function
renderReactToMarkdown
(
...
args
:
Parameters
<
MarkdownRuntimeModule
[
'renderReactToMarkdown'
]
>
)
{
return
getMarkdownRuntime
().
renderReactToMarkdown
(...
args
)
}
export
type
{
MarkdownComponent
,
MarkdownComponentContext
,
...
...
packages/next/src/server/render.tsx
View file @
bfc399e1
...
...
@@ -111,11 +111,15 @@ import {
acceptsMarkdown
,
appendAcceptVaryHeader
,
}
from
'./markdown/accepts-markdown'
import
{
getMarkdownRouteConfig
,
loadMarkdownRootComponents
,
type
MarkdownRouteConfig
,
}
from
'./markdown/config'
import
{
markReactNode
,
type
MarkdownComponents
,
type
MarkdownSegmentDefinition
,
render
Html
ToMarkdown
,
render
React
ToMarkdown
,
}
from
'./markdown/runtime'
let
tryGetPreviewData
:
typeof
import
(
'./api-utils/node/try-get-preview-data'
).
tryGetPreviewData
...
...
@@ -139,16 +143,6 @@ if (process.env.NEXT_RUNTIME !== 'edge') {
postProcessHTML
=
async
(
html
:
string
)
=>
html
}
function
loadMarkdownRootComponents
():
MarkdownComponents
{
if
(
process
.
env
.
NEXT_RUNTIME
===
'edge'
)
{
return
{}
}
return
(
require
(
'../lib/require-markdown-components'
)
as
typeof
import
(
'../lib/require-markdown-components'
)
).
requireMarkdownComponents
()
}
function
noRouter
()
{
const
message
=
'No router instance found. you should only use "next/router" inside the client side of your app. https://nextjs.org/docs/messages/no-router-instance'
...
...
@@ -161,33 +155,90 @@ async function renderToString(element: React.ReactElement) {
return
streamToString
(
renderStream
)
}
function
normalizeMarkdownRouteConfig
(
markdown
:
unknown
):
{
enabled
:
boolean
components
:
MarkdownComponents
}
{
if
(
markdown
===
true
)
{
return
{
enabled
:
true
,
components
:
{},
}
}
type
PagesMarkdownRouteConfig
=
MarkdownRouteConfig
<
NonNullable
<
PagesModule
[
'generateMarkdown'
]
>
>
if
(
markdown
&&
typeof
markdown
===
'object'
)
{
return
{
enabled
:
true
,
components
:
'components'
in
markdown
&&
markdown
.
components
&&
typeof
markdown
.
components
===
'object'
?
(
markdown
.
components
as
MarkdownComponents
)
:
{},
}
async
function
renderPageToMarkdown
({
App
,
Component
,
props
,
query
,
router
,
metadata
,
res
,
routeConfig
,
}:
{
App
:
AppType
Component
:
NextComponentType
<
any
,
any
,
any
>
props
:
Record
<
string
,
any
>
query
:
NextParsedUrlQuery
router
:
NextRouter
metadata
:
PagesRenderResultMetadata
res
:
ServerResponse
routeConfig
:
PagesMarkdownRouteConfig
}):
Promise
<
RenderResult
>
{
const
render
=
routeConfig
.
render
const
pageDefinition
:
MarkdownSegmentDefinition
=
{
id
:
'page'
,
props
:
{},
components
:
routeConfig
.
components
,
render
:
render
?
async
(
pageProps
,
{
content
})
=>
render
(
{
props
:
pageProps
,
query
,
},
{
content
,
}
)
:
undefined
,
}
return
{
enabled
:
false
,
components
:
{},
const
markdownSegmentByComponent
=
new
Map
([
[
Component
,
{
id
:
pageDefinition
.
id
,
registerProps
(
componentProps
:
any
)
{
pageDefinition
.
props
=
componentProps
},
},
],
])
const
MarkdownPageComponent
=
function
NextMarkdownPageComponent
(
pageProps
:
Record
<
string
,
any
>
)
{
return
markReactNode
(
React
.
createElement
(
Component
,
pageProps
),
{
segmentByComponent
:
markdownSegmentByComponent
,
})
as
React
.
ReactElement
}
MarkdownPageComponent
.
displayName
=
`NextMarkdownPage(
${
getDisplayName
(
Component
)}
)`
const
pageContent
=
renderPageTree
(
App
,
MarkdownPageComponent
,
{
...
props
,
router
,
})
const
markdown
=
await
renderReactToMarkdown
(
pageContent
,
{
rootComponents
:
loadMarkdownRootComponents
(),
segments
:
new
Map
([[
pageDefinition
.
id
,
pageDefinition
]]),
})
metadata
.
headers
??
=
{}
metadata
.
headers
[
'Content-Type'
]
=
MARKDOWN_CONTENT_TYPE_HEADER
metadata
.
statusCode
=
res
.
statusCode
return
new
RenderResult
(
markdown
,
{
metadata
,
contentType
:
MARKDOWN_CONTENT_TYPE_HEADER
,
})
}
class
ServerRouter
implements
NextRouter
{
...
...
@@ -1284,18 +1335,14 @@ export async function renderToHTMLImpl(
// the response might be finished on the getInitialProps call
if
(
isResSent
(
res
)
&&
!
isSSG
)
return
RenderResult
.
EMPTY
const
markdownConfig
=
normalizeMarkdownRouteConfig
(
(
renderOpts
.
ComponentMod
as
any
).
markdown
)
const
generateMarkdown
=
typeof
(
renderOpts
.
ComponentMod
as
any
).
generateMarkdown
===
'function'
?
(
renderOpts
.
ComponentMod
as
any
).
generateMarkdown
:
undefined
const
markdownRoute
=
getMarkdownRouteConfig
<
NonNullable
<
PagesModule
[
'generateMarkdown'
]
>
>
(
renderOpts
.
ComponentMod
as
any
)
const
supportsMarkdownResponse
=
renderOpts
.
experimental
.
markdown
===
true
&&
process
.
env
.
NEXT_RUNTIME
!==
'edge'
&&
(
req
.
method
===
'GET'
||
req
.
method
===
'HEAD'
)
&&
(
markdownConfig
.
enabled
||
generateMarkdown
)
markdownRoute
.
enabled
const
acceptsMarkdownRequest
=
acceptsMarkdown
(
req
.
headers
.
accept
)
const
wantsMarkdown
=
supportsMarkdownResponse
&&
acceptsMarkdownRequest
...
...
@@ -1307,66 +1354,15 @@ export async function renderToHTMLImpl(
}
if
(
wantsMarkdown
)
{
const
pageDefinition
:
MarkdownSegmentDefinition
=
{
id
:
'page'
,
props
:
{},
components
:
markdownConfig
.
components
,
render
:
generateMarkdown
?
async
(
pageProps
,
{
content
})
=>
generateMarkdown
(
{
props
:
pageProps
,
query
,
},
{
content
,
}
)
:
undefined
,
}
const
markdownSegmentByComponent
=
new
Map
([
[
Component
,
{
id
:
pageDefinition
.
id
,
registerProps
(
componentProps
:
any
)
{
pageDefinition
.
props
=
componentProps
},
},
],
])
const
MarkdownPageComponent
=
function
NextMarkdownPageComponent
(
pageProps
:
Record
<
string
,
any
>
)
{
return
markReactNode
(
React
.
createElement
(
Component
,
pageProps
),
{
segmentByComponent
:
markdownSegmentByComponent
,
})
as
React
.
ReactElement
}
MarkdownPageComponent
.
displayName
=
`NextMarkdownPage(
${
getDisplayName
(
Component
)}
)`
const
pageContent
=
renderPageTree
(
App
,
MarkdownPageComponent
,
{
...
props
,
return
renderPageToMarkdown
({
App
,
Component
,
props
,
query
,
router
,
})
const
html
=
await
renderToString
(
pageContent
as
React
.
ReactElement
)
const
markdown
=
await
renderHtmlToMarkdown
(
html
,
{
rootComponents
:
loadMarkdownRootComponents
(),
segments
:
new
Map
([[
pageDefinition
.
id
,
pageDefinition
]]),
})
metadata
.
headers
??
=
{}
metadata
.
headers
[
'Content-Type'
]
=
MARKDOWN_CONTENT_TYPE_HEADER
metadata
.
statusCode
=
res
.
statusCode
return
new
RenderResult
(
markdown
,
{
metadata
,
contentType
:
MARKDOWN_CONTENT_TYPE_HEADER
,
res
,
routeConfig
:
markdownRoute
,
})
}
...
...
pnpm-lock.yaml
View file @
bfc399e1
...
...
@@ -296,7 +296,7 @@ importers:
version
:
5.2.1(eslint@9.37.0(jiti@2.5.1))
eslint-plugin-import
:
specifier
:
2.31.0
version
:
2.31.0(eslint@9.37.0(jiti@2.5.1))
version
:
2.31.0(
@typescript-eslint/parser@8.46.0(eslint@9.37.0(jiti@2.5.1))(typescript@5.9.2))(
eslint@9.37.0(jiti@2.5.1))
eslint-plugin-jest
:
specifier
:
27.6.3
version
:
27.6.3(eslint@9.37.0(jiti@2.5.1))(jest@29.7.0(@types/node@20.17.6(patch_hash=rvl3vkomen3tospgr67bzubfyu))(babel-plugin-macros@3.1.0))(typescript@5.9.2)
...
...
@@ -1864,6 +1864,9 @@ importers:
react
:
specifier
:
npm:react@19.3.0-canary-3bc2d414-20260304
version
:
19.3.0-canary-3bc2d414-20260304
react-reconciler
:
specifier
:
0.33.0
version
:
0.33.0(react@19.3.0-canary-3bc2d414-20260304)
packages/next-mdx
:
dependencies
:
...
...
@@ -15854,6 +15857,12 @@ packages:
react
:
npm:react@19.3.0-canary-3bc2d414-20260304
react-dom
:
npm:react-dom@19.3.0-canary-3bc2d414-20260304
react-reconciler@0.33.0
:
resolution
:
{
integrity
:
sha512-KetWRytFv1epdpJc3J4G75I4WrplZE5jOL7Yq0p34+OVOKF4Se7WrdIdVC45XsSSmUTlht2FM/fM1FZb1mfQeA==
}
engines
:
{
node
:
'
>=0.10.0'
}
peerDependencies
:
react
:
npm:react@19.3.0-canary-3bc2d414-20260304
react-refresh@0.12.0
:
resolution
:
{
integrity
:
sha512-suLIhrU2IHKL5JEKR/fAwJv7bbeq4kJ+pJopf77jHwuR+HmJS/HbrPIGsTBUVfw7tXPOmYv7UJ7PCaN49e8x4A==
}
engines
:
{
node
:
'
>=0.10.0'
}
...
...
@@ -27942,10 +27951,11 @@ snapshots:
-
bluebird
-
supports-color
eslint-module-utils@2.12.0(eslint-import-resolver-node@0.3.9)(eslint@9.37.0(jiti@2.5.1))
:
eslint-module-utils@2.12.0(
@typescript-eslint/parser@8.46.0(eslint@9.37.0(jiti@2.5.1))(typescript@5.9.2))(
eslint-import-resolver-node@0.3.9)(eslint@9.37.0(jiti@2.5.1))
:
dependencies
:
debug
:
3.2.7
optionalDependencies
:
'
@typescript-eslint/parser'
:
8.46.0(eslint@9.37.0(jiti@2.5.1))(typescript@5.9.2)
eslint
:
9.37.0(jiti@2.5.1)
eslint-import-resolver-node
:
0.3.9
transitivePeerDependencies
:
...
...
@@ -27986,7 +27996,7 @@ snapshots:
-
typescript
optional
:
true
eslint-plugin-import@2.31.0(eslint@9.37.0(jiti@2.5.1))
:
eslint-plugin-import@2.31.0(
@typescript-eslint/parser@8.46.0(eslint@9.37.0(jiti@2.5.1))(typescript@5.9.2))(
eslint@9.37.0(jiti@2.5.1))
:
dependencies
:
'
@rtsao/scc'
:
1.1.0
array-includes
:
3.1.8
...
...
@@ -27997,7 +28007,7 @@ snapshots:
doctrine
:
2.1.0
eslint
:
9.37.0(jiti@2.5.1)
eslint-import-resolver-node
:
0.3.9
eslint-module-utils
:
2.12.0(eslint-import-resolver-node@0.3.9)(eslint@9.37.0(jiti@2.5.1))
eslint-module-utils
:
2.12.0(
@typescript-eslint/parser@8.46.0(eslint@9.37.0(jiti@2.5.1))(typescript@5.9.2))(
eslint-import-resolver-node@0.3.9)(eslint@9.37.0(jiti@2.5.1))
hasown
:
2.0.2
is-core-module
:
2.15.1
is-glob
:
4.0.3
...
...
@@ -28008,6 +28018,8 @@ snapshots:
semver
:
6.3.1
string.prototype.trimend
:
1.0.9
tsconfig-paths
:
3.15.0
optionalDependencies
:
'
@typescript-eslint/parser'
:
8.46.0(eslint@9.37.0(jiti@2.5.1))(typescript@5.9.2)
transitivePeerDependencies
:
-
eslint-import-resolver-typescript
-
eslint-import-resolver-webpack
...
...
@@ -35647,6 +35659,11 @@ snapshots:
react
:
19.3.0-canary-3bc2d414-20260304
react-dom
:
19.3.0-canary-3bc2d414-20260304(react@19.3.0-canary-3bc2d414-20260304)
react-reconciler@0.33.0(react@19.3.0-canary-3bc2d414-20260304)
:
dependencies
:
react
:
19.3.0-canary-3bc2d414-20260304
scheduler
:
0.28.0-canary-3bc2d414-20260304
react-refresh@0.12.0
:
{}
react-remove-scroll-bar@2.3.6(@types/react@19.2.10)(react@19.3.0-canary-3bc2d414-20260304)
:
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment