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
Show 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.
...
@@ -10,11 +10,10 @@ change without notice.
## What it does
## What it does
This package contains the generic React and HTML to Markdown logic used by
This package contains the generic React-to-Markdown logic used by Next.js:
Next.js:
-
React tree instrumentation for Markdown-specific markers
-
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
-
root and segment-level component overrides
-
default heuristics for omitting interactive controls
-
default heuristics for omitting interactive controls
...
@@ -26,7 +25,7 @@ Next.js-specific routing, content negotiation, and route export handling live in
...
@@ -26,7 +25,7 @@ Next.js-specific routing, content negotiation, and route export handling live in
The package currently exposes:
The package currently exposes:
-
`markReactNode`
-
`markReactNode`
-
`render
Html
ToMarkdown`
-
`render
React
ToMarkdown`
-
marker constants
-
marker constants
-
Markdown component and segment types
-
Markdown component and segment types
...
@@ -35,4 +34,3 @@ The package currently exposes:
...
@@ -35,4 +34,3 @@ The package currently exposes:
```
bash
```
bash
pnpm
--filter
@next/markdown build
pnpm
--filter
@next/markdown build
```
```
packages/next-markdown/package.json
View file @
bfc399e1
...
@@ -24,7 +24,8 @@
...
@@ -24,7 +24,8 @@
"directory"
:
"packages/next-markdown"
"directory"
:
"packages/next-markdown"
},
},
"dependencies"
:
{
"dependencies"
:
{
"node-html-parser"
:
"7.0.1"
"node-html-parser"
:
"7.0.1"
,
"react-reconciler"
:
"0.33.0"
},
},
"peerDependencies"
:
{
"peerDependencies"
:
{
"react"
:
"^18.2.0 || ^19.0.0"
"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(
...
@@ -446,7 +446,8 @@ async function createComponentTreeInternal(
:
parentOptionalCatchAllParamName
:
parentOptionalCatchAllParamName
// Resolve the segment param
// Resolve the segment param
const
isSegmentViewEnabled
=
!!
process
.
env
.
__NEXT_DEV_SERVER
const
isSegmentViewEnabled
=
!!
process
.
env
.
__NEXT_DEV_SERVER
&&
!
ctx
.
markdownSegmentByComponent
const
dir
=
const
dir
=
(
process
.
env
.
NEXT_RUNTIME
===
'edge'
(
process
.
env
.
NEXT_RUNTIME
===
'edge'
?
process
.
env
.
__NEXT_EDGE_PROJECT_DIR
?
process
.
env
.
__NEXT_EDGE_PROJECT_DIR
...
@@ -669,13 +670,15 @@ async function createComponentTreeInternal(
...
@@ -669,13 +670,15 @@ async function createComponentTreeInternal(
)
)
// Convert the parallel route map into an object after all promises have been resolved.
// 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
parallelRouteProps
:
{
[
key
:
string
]:
React
.
ReactNode
}
=
{}
let
parallelRouteCacheNodeSeedData
:
{
let
parallelRouteCacheNodeSeedData
:
{
[
key
:
string
]:
CacheNodeSeedData
|
null
[
key
:
string
]:
CacheNodeSeedData
|
null
}
=
{}
}
=
{}
for
(
const
parallelRoute
of
parallelRouteMap
)
{
for
(
const
parallelRoute
of
parallelRouteMap
)
{
const
[
parallelRouteKey
,
parallelRouteProp
,
flightData
]
=
parallelRoute
const
[
parallelRouteKey
,
parallelRouteProp
,
flightData
]
=
parallelRoute
parallelRouteProps
[
parallelRouteKey
]
=
parallelRouteProp
parallelRouteProps
[
parallelRouteKey
]
=
isMarkdownRender
&&
flightData
?
flightData
[
0
]
:
parallelRouteProp
parallelRouteCacheNodeSeedData
[
parallelRouteKey
]
=
flightData
parallelRouteCacheNodeSeedData
[
parallelRouteKey
]
=
flightData
}
}
...
@@ -1234,7 +1237,8 @@ async function createBoundaryConventionElement({
...
@@ -1234,7 +1237,8 @@ async function createBoundaryConventionElement({
const
{
const
{
componentMod
:
{
createElement
,
Fragment
},
componentMod
:
{
createElement
,
Fragment
},
}
=
ctx
}
=
ctx
const
isSegmentViewEnabled
=
!!
process
.
env
.
__NEXT_DEV_SERVER
const
isSegmentViewEnabled
=
!!
process
.
env
.
__NEXT_DEV_SERVER
&&
!
ctx
.
markdownSegmentByComponent
const
dir
=
const
dir
=
(
process
.
env
.
NEXT_RUNTIME
===
'edge'
(
process
.
env
.
NEXT_RUNTIME
===
'edge'
?
process
.
env
.
__NEXT_EDGE_PROJECT_DIR
?
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. */
/* eslint-disable @next/internal/no-ambiguous-jsx -- Test fixtures use concise JSX trees to exercise React-to-Markdown instrumentation. */
import
React
from
'react'
import
React
from
'react'
import
{
renderToStaticMarkup
}
from
'react-dom/server'
import
{
import
{
MARKDOWN_SEGMENT_MARKER_TAG
,
markReactNode
,
markReactNode
,
render
Html
ToMarkdown
,
render
React
ToMarkdown
,
type
MarkdownSegmentDefinition
,
type
MarkdownSegmentDefinition
,
}
from
'../../../../next-markdown/src'
}
from
'../../../../next-markdown/src'
describe
(
'markdown renderer'
,
()
=>
{
describe
(
'markdown renderer'
,
()
=>
{
it
(
'serializes basic markdown blocks'
,
async
()
=>
{
it
(
'serializes basic markdown blocks'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
const
markdown
=
await
renderReactToMarkdown
(
[
<>
'<h1>Hello</h1>'
,
<
h1
>
Hello
</
h1
>
'<p><strong>World</strong> <a href="/docs">docs</a></p>'
,
<
p
>
'<pre><code class="language-ts">const value = 1</code></pre>'
,
<
strong
>
World
</
strong
>
<
a
href=
"/docs"
>
docs
</
a
>
'<ul><li>One</li><li>Two</li></ul>'
,
</
p
>
].
join
(
''
)
<
pre
>
<
code
className=
"language-ts"
>
const value = 1
</
code
>
</
pre
>
<
ul
>
<
li
>
One
</
li
>
<
li
>
Two
</
li
>
</
ul
>
</>
)
)
expect
(
markdown
).
toBe
(
expect
(
markdown
).
toBe
(
...
@@ -24,21 +31,44 @@ describe('markdown renderer', () => {
...
@@ -24,21 +31,44 @@ describe('markdown renderer', () => {
)
)
})
})
it
(
'normalizes full-document html before serializing markdown'
,
async
()
=>
{
it
(
'serializes dangerouslySetInnerHTML without requiring a top-level html render'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
const
markdown
=
await
renderReactToMarkdown
(
'<!DOCTYPE html><html><body><h1>Hello</h1><p>World</p></body></html>'
<
div
dangerouslySetInnerHTML=
{
{
__html
:
'<h1>Hello</h1><p>World</p>'
,
}
}
/>
)
)
expect
(
markdown
).
toBe
(
'# Hello
\
n
\
nWorld'
)
expect
(
markdown
).
toBe
(
'# Hello
\
n
\
nWorld'
)
})
})
it
(
'serializes images, tables, and nested lists'
,
async
()
=>
{
it
(
'serializes images, tables, and nested lists'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
const
markdown
=
await
renderReactToMarkdown
(
[
<>
'<img src="/logo.png" alt="Logo" />'
,
<
img
src=
"/logo.png"
alt=
"Logo"
/>
'<table><tr><th>Name</th><th>Value</th></tr><tr><td>A</td><td>1</td></tr></table>'
,
<
table
>
'<ul><li>One<ul><li>Nested</li></ul></li><li>Two</li></ul>'
,
<
tbody
>
].
join
(
''
)
<
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
(
expect
(
markdown
).
toBe
(
...
@@ -54,11 +84,9 @@ describe('markdown renderer', () => {
...
@@ -54,11 +84,9 @@ describe('markdown renderer', () => {
</
div
>
</
div
>
)
)
const
html
=
renderToStaticMarkup
(
const
marked
=
markReactNode
(<
Interactive
/>)
as
React
.
ReactElement
markReactNode
(<
Interactive
/>)
as
React
.
ReactElement
const
withoutOverride
=
await
renderReactToMarkdown
(
marked
)
)
const
withOverride
=
await
renderReactToMarkdown
(
marked
,
{
const
withoutOverride
=
await
renderHtmlToMarkdown
(
html
)
const
withOverride
=
await
renderHtmlToMarkdown
(
html
,
{
rootComponents
:
{
rootComponents
:
{
div
({
attributes
,
children
,
renderDefault
})
{
div
({
attributes
,
children
,
renderDefault
})
{
if
(
attributes
[
'data-react-markdown-interactive'
]
===
'true'
)
{
if
(
attributes
[
'data-react-markdown-interactive'
]
===
'true'
)
{
...
@@ -82,11 +110,9 @@ describe('markdown renderer', () => {
...
@@ -82,11 +110,9 @@ describe('markdown renderer', () => {
return
<
button
>
Download
</
button
>
return
<
button
>
Download
</
button
>
}
}
const
html
=
renderToStaticMarkup
(
const
markdown
=
await
renderReactToMarkdown
(
markReactNode
(<
FancyButton
/>)
as
React
.
ReactElement
markReactNode
(<
FancyButton
/>),
)
{
const
markdown
=
await
renderHtmlToMarkdown
(
html
,
{
rootComponents
:
{
rootComponents
:
{
button
()
{
button
()
{
return
'tag override'
return
'tag override'
...
@@ -95,7 +121,8 @@ describe('markdown renderer', () => {
...
@@ -95,7 +121,8 @@ describe('markdown renderer', () => {
return
'component override'
return
'component override'
},
},
},
},
})
}
)
expect
(
markdown
).
toBe
(
'component override'
)
expect
(
markdown
).
toBe
(
'component override'
)
})
})
...
@@ -110,16 +137,14 @@ describe('markdown renderer', () => {
...
@@ -110,16 +137,14 @@ describe('markdown renderer', () => {
return
<
p
>
Memo note
</
p
>
return
<
p
>
Memo note
</
p
>
})
})
const
html
=
renderToStaticMarkup
(
const
markdown
=
await
renderReactToMarkdown
(
markReactNode
(
markReactNode
(
<>
<>
<
FancyAction
/>
<
FancyAction
/>
<
MemoNote
/>
<
MemoNote
/>
</>
</>
)
as
React
.
ReactElement
),
)
{
const
markdown
=
await
renderHtmlToMarkdown
(
html
,
{
rootComponents
:
{
rootComponents
:
{
FancyAction
()
{
FancyAction
()
{
return
'forward-ref override'
return
'forward-ref override'
...
@@ -128,14 +153,18 @@ describe('markdown renderer', () => {
...
@@ -128,14 +153,18 @@ describe('markdown renderer', () => {
return
'memo override'
return
'memo override'
},
},
},
},
})
}
)
expect
(
markdown
).
toBe
(
'forward-ref override
\
n
\
nmemo override'
)
expect
(
markdown
).
toBe
(
'forward-ref override
\
n
\
nmemo override'
)
})
})
it
(
'allows overrides to omit output by returning null'
,
async
()
=>
{
it
(
'allows overrides to omit output by returning null'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
const
markdown
=
await
renderReactToMarkdown
(
'<p>Keep me?</p><img src="/x.png" alt="X" />'
,
<>
<
p
>
Keep me?
</
p
>
<
img
src=
"/x.png"
alt=
"X"
/>
</>,
{
{
rootComponents
:
{
rootComponents
:
{
p
()
{
p
()
{
...
@@ -149,8 +178,10 @@ describe('markdown renderer', () => {
...
@@ -149,8 +178,10 @@ describe('markdown renderer', () => {
})
})
it
(
'preserves inline spacing and formatting for paragraph overrides'
,
async
()
=>
{
it
(
'preserves inline spacing and formatting for paragraph overrides'
,
async
()
=>
{
const
markdown
=
await
renderHtmlToMarkdown
(
const
markdown
=
await
renderReactToMarkdown
(
'<p>Hello <strong>wide</strong> world</p>'
,
<
p
>
Hello
<
strong
>
wide
</
strong
>
world
</
p
>,
{
{
rootComponents
:
{
rootComponents
:
{
p
({
children
})
{
p
({
children
})
{
...
@@ -197,16 +228,18 @@ describe('markdown renderer', () => {
...
@@ -197,16 +228,18 @@ describe('markdown renderer', () => {
],
],
])
])
const
markdown
=
await
renderHtmlToMarkdown
(
const
markdown
=
await
renderReactToMarkdown
(
[
React
.
createElement
(
'<react-markdown-segment-marker data-segment-id="layout">'
,
MARKDOWN_SEGMENT_MARKER_TAG
,
'<section>'
,
{
'data-segment-id'
:
'layout'
},
'<react-markdown-segment-marker data-segment-id="page">'
,
<
section
>
'<p>Hello</p>'
,
{
React
.
createElement
(
'</react-markdown-segment-marker>'
,
MARKDOWN_SEGMENT_MARKER_TAG
,
'</section>'
,
{
'data-segment-id'
:
'page'
},
'</react-markdown-segment-marker>'
,
<
p
>
Hello
</
p
>
].
join
(
''
),
)
}
</
section
>
),
{
{
rootComponents
:
{
rootComponents
:
{
p
({
children
})
{
p
({
children
})
{
...
@@ -220,14 +253,14 @@ describe('markdown renderer', () => {
...
@@ -220,14 +253,14 @@ describe('markdown renderer', () => {
expect
(
markdown
).
toBe
(
'layout-start
\
npage:Hello
\
nlayout-end'
)
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
>
=
[]
const
captured
:
Array
<
any
>
=
[]
function
Page
(
props
:
{
title
:
string
})
{
function
Page
(
props
:
{
title
:
string
})
{
return
<
p
>
{
props
.
title
}
</
p
>
return
<
p
>
{
props
.
title
}
</
p
>
}
}
const
html
=
renderToStaticMarkup
(
const
markdown
=
await
renderReactToMarkdown
(
markReactNode
(<
Page
title=
"Hello"
/>,
{
markReactNode
(<
Page
title=
"Hello"
/>,
{
segmentByComponent
:
new
Map
([
segmentByComponent
:
new
Map
([
[
[
...
@@ -244,7 +277,40 @@ describe('markdown renderer', () => {
...
@@ -244,7 +277,40 @@ describe('markdown renderer', () => {
)
)
expect
(
captured
).
toEqual
([{
title
:
'Hello'
}])
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'
,
()
=>
{
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 {
...
@@ -18,14 +18,35 @@ function getMarkdownRuntime(): MarkdownRuntimeModule {
return
runtime
return
runtime
}
}
try
{
let
loaded
:
MarkdownRuntimeModule
runtime
=
// eslint-disable-next-line no-eval -- Deferred so the packaged build doesn't try to bundle the local workspace source path.
require
(
'../../../../next-markdown/src'
)
as
typeof
import
(
'../../../../next-markdown/src'
)
const
dynamicRequire
=
eval
(
'require'
)
as
NodeJS
.
Require
}
catch
{
runtime
=
require
(
'@next/markdown'
)
as
typeof
import
(
'@next/markdown'
)
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'
export
const
MARKDOWN_COMPONENT_MARKER_TAG
=
'react-markdown-component-marker'
...
@@ -44,6 +65,12 @@ export function renderHtmlToMarkdown(
...
@@ -44,6 +65,12 @@ export function renderHtmlToMarkdown(
return
getMarkdownRuntime
().
renderHtmlToMarkdown
(...
args
)
return
getMarkdownRuntime
().
renderHtmlToMarkdown
(...
args
)
}
}
export
function
renderReactToMarkdown
(
...
args
:
Parameters
<
MarkdownRuntimeModule
[
'renderReactToMarkdown'
]
>
)
{
return
getMarkdownRuntime
().
renderReactToMarkdown
(...
args
)
}
export
type
{
export
type
{
MarkdownComponent
,
MarkdownComponent
,
MarkdownComponentContext
,
MarkdownComponentContext
,
...
...
packages/next/src/server/render.tsx
View file @
bfc399e1
...
@@ -111,11 +111,15 @@ import {
...
@@ -111,11 +111,15 @@ import {
acceptsMarkdown
,
acceptsMarkdown
,
appendAcceptVaryHeader
,
appendAcceptVaryHeader
,
}
from
'./markdown/accepts-markdown'
}
from
'./markdown/accepts-markdown'
import
{
getMarkdownRouteConfig
,
loadMarkdownRootComponents
,
type
MarkdownRouteConfig
,
}
from
'./markdown/config'
import
{
import
{
markReactNode
,
markReactNode
,
type
MarkdownComponents
,
type
MarkdownSegmentDefinition
,
type
MarkdownSegmentDefinition
,
render
Html
ToMarkdown
,
render
React
ToMarkdown
,
}
from
'./markdown/runtime'
}
from
'./markdown/runtime'
let
tryGetPreviewData
:
typeof
import
(
'./api-utils/node/try-get-preview-data'
).
tryGetPreviewData
let
tryGetPreviewData
:
typeof
import
(
'./api-utils/node/try-get-preview-data'
).
tryGetPreviewData
...
@@ -139,16 +143,6 @@ if (process.env.NEXT_RUNTIME !== 'edge') {
...
@@ -139,16 +143,6 @@ if (process.env.NEXT_RUNTIME !== 'edge') {
postProcessHTML
=
async
(
html
:
string
)
=>
html
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
()
{
function
noRouter
()
{
const
message
=
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'
'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) {
...
@@ -161,33 +155,90 @@ async function renderToString(element: React.ReactElement) {
return
streamToString
(
renderStream
)
return
streamToString
(
renderStream
)
}
}
function
normalizeMarkdownRouteConfig
(
markdown
:
unknown
):
{
type
PagesMarkdownRouteConfig
=
MarkdownRouteConfig
<
enabled
:
boolean
NonNullable
<
PagesModule
[
'generateMarkdown'
]
>
components
:
MarkdownComponents
>
}
{
if
(
markdown
===
true
)
{
return
{
enabled
:
true
,
components
:
{},
}
}
if
(
markdown
&&
typeof
markdown
===
'object'
)
{
async
function
renderPageToMarkdown
({
return
{
App
,
enabled
:
true
,
Component
,
components
:
props
,
'components'
in
markdown
&&
query
,
markdown
.
components
&&
router
,
typeof
markdown
.
components
===
'object'
metadata
,
?
(
markdown
.
components
as
MarkdownComponents
)
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
{
const
markdownSegmentByComponent
=
new
Map
([
enabled
:
false
,
[
components
:
{},
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
{
class
ServerRouter
implements
NextRouter
{
...
@@ -1284,18 +1335,14 @@ export async function renderToHTMLImpl(
...
@@ -1284,18 +1335,14 @@ export async function renderToHTMLImpl(
// the response might be finished on the getInitialProps call
// the response might be finished on the getInitialProps call
if
(
isResSent
(
res
)
&&
!
isSSG
)
return
RenderResult
.
EMPTY
if
(
isResSent
(
res
)
&&
!
isSSG
)
return
RenderResult
.
EMPTY
const
markdownConfig
=
normalizeMarkdownRouteConfig
(
const
markdownRoute
=
getMarkdownRouteConfig
<
(
renderOpts
.
ComponentMod
as
any
).
markdown
NonNullable
<
PagesModule
[
'generateMarkdown'
]
>
)
>
(
renderOpts
.
ComponentMod
as
any
)
const
generateMarkdown
=
typeof
(
renderOpts
.
ComponentMod
as
any
).
generateMarkdown
===
'function'
?
(
renderOpts
.
ComponentMod
as
any
).
generateMarkdown
:
undefined
const
supportsMarkdownResponse
=
const
supportsMarkdownResponse
=
renderOpts
.
experimental
.
markdown
===
true
&&
renderOpts
.
experimental
.
markdown
===
true
&&
process
.
env
.
NEXT_RUNTIME
!==
'edge'
&&
process
.
env
.
NEXT_RUNTIME
!==
'edge'
&&
(
req
.
method
===
'GET'
||
req
.
method
===
'HEAD'
)
&&
(
req
.
method
===
'GET'
||
req
.
method
===
'HEAD'
)
&&
(
markdownConfig
.
enabled
||
generateMarkdown
)
markdownRoute
.
enabled
const
acceptsMarkdownRequest
=
acceptsMarkdown
(
req
.
headers
.
accept
)
const
acceptsMarkdownRequest
=
acceptsMarkdown
(
req
.
headers
.
accept
)
const
wantsMarkdown
=
supportsMarkdownResponse
&&
acceptsMarkdownRequest
const
wantsMarkdown
=
supportsMarkdownResponse
&&
acceptsMarkdownRequest
...
@@ -1307,66 +1354,15 @@ export async function renderToHTMLImpl(
...
@@ -1307,66 +1354,15 @@ export async function renderToHTMLImpl(
}
}
if
(
wantsMarkdown
)
{
if
(
wantsMarkdown
)
{
const
pageDefinition
:
MarkdownSegmentDefinition
=
{
return
renderPageToMarkdown
({
id
:
'page'
,
App
,
props
:
{},
components
:
markdownConfig
.
components
,
render
:
generateMarkdown
?
async
(
pageProps
,
{
content
})
=>
generateMarkdown
(
{
props
:
pageProps
,
query
,
},
{
content
,
}
)
:
undefined
,
}
const
markdownSegmentByComponent
=
new
Map
([
[
Component
,
Component
,
{
props
,
id
:
pageDefinition
.
id
,
query
,
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
,
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
,
metadata
,
contentType
:
MARKDOWN_CONTENT_TYPE_HEADER
,
res
,
routeConfig
:
markdownRoute
,
})
})
}
}
...
...
pnpm-lock.yaml
View file @
bfc399e1
...
@@ -296,7 +296,7 @@ importers:
...
@@ -296,7 +296,7 @@ importers:
version
:
5.2.1(eslint@9.37.0(jiti@2.5.1))
version
:
5.2.1(eslint@9.37.0(jiti@2.5.1))
eslint-plugin-import
:
eslint-plugin-import
:
specifier
:
2.31.0
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
:
eslint-plugin-jest
:
specifier
:
27.6.3
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)
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:
...
@@ -1864,6 +1864,9 @@ importers:
react
:
react
:
specifier
:
npm:react@19.3.0-canary-3bc2d414-20260304
specifier
:
npm:react@19.3.0-canary-3bc2d414-20260304
version
:
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
:
packages/next-mdx
:
dependencies
:
dependencies
:
...
@@ -15854,6 +15857,12 @@ packages:
...
@@ -15854,6 +15857,12 @@ packages:
react
:
npm:react@19.3.0-canary-3bc2d414-20260304
react
:
npm:react@19.3.0-canary-3bc2d414-20260304
react-dom
:
npm:react-dom@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
:
react-refresh@0.12.0
:
resolution
:
{
integrity
:
sha512-suLIhrU2IHKL5JEKR/fAwJv7bbeq4kJ+pJopf77jHwuR+HmJS/HbrPIGsTBUVfw7tXPOmYv7UJ7PCaN49e8x4A==
}
resolution
:
{
integrity
:
sha512-suLIhrU2IHKL5JEKR/fAwJv7bbeq4kJ+pJopf77jHwuR+HmJS/HbrPIGsTBUVfw7tXPOmYv7UJ7PCaN49e8x4A==
}
engines
:
{
node
:
'
>=0.10.0'
}
engines
:
{
node
:
'
>=0.10.0'
}
...
@@ -27942,10 +27951,11 @@ snapshots:
...
@@ -27942,10 +27951,11 @@ snapshots:
-
bluebird
-
bluebird
-
supports-color
-
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
:
dependencies
:
debug
:
3.2.7
debug
:
3.2.7
optionalDependencies
:
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
:
9.37.0(jiti@2.5.1)
eslint-import-resolver-node
:
0.3.9
eslint-import-resolver-node
:
0.3.9
transitivePeerDependencies
:
transitivePeerDependencies
:
...
@@ -27986,7 +27996,7 @@ snapshots:
...
@@ -27986,7 +27996,7 @@ snapshots:
-
typescript
-
typescript
optional
:
true
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
:
dependencies
:
'
@rtsao/scc'
:
1.1.0
'
@rtsao/scc'
:
1.1.0
array-includes
:
3.1.8
array-includes
:
3.1.8
...
@@ -27997,7 +28007,7 @@ snapshots:
...
@@ -27997,7 +28007,7 @@ snapshots:
doctrine
:
2.1.0
doctrine
:
2.1.0
eslint
:
9.37.0(jiti@2.5.1)
eslint
:
9.37.0(jiti@2.5.1)
eslint-import-resolver-node
:
0.3.9
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
hasown
:
2.0.2
is-core-module
:
2.15.1
is-core-module
:
2.15.1
is-glob
:
4.0.3
is-glob
:
4.0.3
...
@@ -28008,6 +28018,8 @@ snapshots:
...
@@ -28008,6 +28018,8 @@ snapshots:
semver
:
6.3.1
semver
:
6.3.1
string.prototype.trimend
:
1.0.9
string.prototype.trimend
:
1.0.9
tsconfig-paths
:
3.15.0
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
:
transitivePeerDependencies
:
-
eslint-import-resolver-typescript
-
eslint-import-resolver-typescript
-
eslint-import-resolver-webpack
-
eslint-import-resolver-webpack
...
@@ -35647,6 +35659,11 @@ snapshots:
...
@@ -35647,6 +35659,11 @@ snapshots:
react
:
19.3.0-canary-3bc2d414-20260304
react
:
19.3.0-canary-3bc2d414-20260304
react-dom
:
19.3.0-canary-3bc2d414-20260304(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-refresh@0.12.0
:
{}
react-remove-scroll-bar@2.3.6(@types/react@19.2.10)(react@19.3.0-canary-3bc2d414-20260304)
:
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