@@ -72,7 +72,27 @@ function rehypeShikiFromHighlighter(
7272 code = code . slice ( 0 , - 1 )
7373
7474 try {
75+ // NOTE: per the `@shikijs/types` contract the `postprocess` hook is only
76+ // invoked when producing HTML strings (i.e. `codeToHtml`). Rehype
77+ // integrations operate on HAST. Running `postprocess` here would require
78+ // converting HAST -> HTML -> run postprocess -> parse back to HAST which
79+ // changes the semantics and surprises users expecting HAST-only
80+ // transformers.
81+ //
82+ // If you need to run HTML-based postprocessing with rehype, apply a
83+ // root transformer that converts the HAST fragment to HTML using
84+ // `hast-util-to-html`, runs your HTML transformers, then converts back
85+ // with `hast-util-from-html`.
86+ //
87+ // Example (inside a root transformer):
88+ // const html = toHtml(fragment)
89+ // const newHtml = myPostprocess(html)
90+ // fragment = fromHtml(newHtml, { fragment: true })
91+ //
92+ // We therefore keep the HAST-only path here.
93+
7594 const fragment = highlighter . codeToHast ( code , codeOptions )
95+
7696 cache ?. set ( cacheKey , fragment )
7797 return fragment
7898 }
@@ -85,13 +105,11 @@ function rehypeShikiFromHighlighter(
85105 }
86106
87107 return ( tree ) => {
88- // use this queue if lazy is enabled
89108 const queue : Promise < void > [ ] = [ ]
90109
91110 visit ( tree , 'element' , ( node , index , parent ) => {
92111 let handler : RehypeShikiHandler | undefined
93112
94- // needed for hast node replacement
95113 if ( ! parent || index == null )
96114 return
97115
@@ -148,7 +166,6 @@ function rehypeShikiFromHighlighter(
148166
149167 if ( lazyLoad ) {
150168 try {
151- // passed language is checked in sync, promise `.catch()` wouldn't work
152169 queue . push ( highlighter . loadLanguage ( lang ) . then ( ( ) => processNode ( lang ) ) )
153170 }
154171 catch ( error ) {
@@ -163,7 +180,6 @@ function rehypeShikiFromHighlighter(
163180 processNode ( lang )
164181 }
165182
166- // don't visit processed nodes
167183 return 'skip'
168184 } )
169185
0 commit comments