@@ -18,6 +18,10 @@ import type {
1818 Awaitable ,
1919 RegexEngine ,
2020 BundledTheme ,
21+ CodeToTokensBaseOptions ,
22+ CodeToTokensOptions ,
23+ TokensResult ,
24+ ThemedToken ,
2125} from 'shiki' ;
2226
2327import type { ShikiLanguageRegistration } from './extended-types' ;
@@ -61,7 +65,7 @@ export const useShikiHighlighter = (
6165 ) => Promise < Highlighter | HighlighterCore >
6266) => {
6367 const [ highlightedCode , setHighlightedCode ] = useState <
64- ReactNode | string | null
68+ ReactNode | string | ThemedToken [ ] [ ] | null
6569 > ( null ) ;
6670
6771 // Stabilize options, language and theme inputs to prevent unnecessary
@@ -108,8 +112,10 @@ export const useShikiHighlighter = (
108112 theme : singleTheme || DEFAULT_THEMES . dark ,
109113 } as CodeOptionsSingleTheme < BundledTheme > ) ;
110114
111- const transformers = restOptions . transformers || [ ] ;
112- if ( showLineNumbers ) {
115+ const isTokensOutput = stableOpts . outputFormat === 'tokens' ;
116+ const transformers = [ ...( restOptions . transformers || [ ] ) ] ;
117+
118+ if ( showLineNumbers && ! isTokensOutput ) {
113119 transformers . push ( lineNumbersTransformer ( startingLineNumber ) ) ;
114120 }
115121
@@ -142,15 +148,32 @@ export const useShikiHighlighter = (
142148 const finalOptions = { ...shikiOptions , lang : langToUse } ;
143149
144150 if ( isMounted ) {
145- const output =
146- stableOpts . outputFormat === 'html'
147- ? highlighter . codeToHtml ( code , finalOptions )
148- : toJsxRuntime ( highlighter . codeToHast ( code , finalOptions ) , {
149- jsx,
150- jsxs,
151- Fragment,
152- } ) ;
153- setHighlightedCode ( output ) ;
151+ if ( stableOpts . outputFormat === 'tokens' ) {
152+ const tokensOutput = isMultiTheme
153+ ? highlighter . codeToTokens (
154+ code ,
155+ finalOptions as CodeToTokensOptions
156+ )
157+ : highlighter . codeToTokensBase (
158+ code ,
159+ finalOptions as CodeToTokensBaseOptions
160+ ) ;
161+
162+ const tokens = ( ( tokensOutput as TokensResult ) . tokens ??
163+ tokensOutput ) as ThemedToken [ ] [ ] ;
164+
165+ setHighlightedCode ( tokens ) ;
166+ } else {
167+ const output =
168+ stableOpts . outputFormat === 'html'
169+ ? highlighter . codeToHtml ( code , finalOptions )
170+ : toJsxRuntime ( highlighter . codeToHast ( code , finalOptions ) , {
171+ jsx,
172+ jsxs,
173+ Fragment,
174+ } ) ;
175+ setHighlightedCode ( output ) ;
176+ }
154177 }
155178 } ;
156179
0 commit comments