@@ -22,8 +22,11 @@ type HighlightedNode = { type: 'element', properties?: Record<string, string | u
2222let highlightPlugin : {
2323 instance : unknown
2424 key ?: string
25- theme ?: Record < string , unknown >
26- highlighter : Highlighter
25+ options ?: {
26+ theme ?: Record < string , unknown >
27+ highlighter : Highlighter
28+ }
29+
2730}
2831let highlightPluginPromise : Promise < typeof highlightPlugin >
2932async function getHighlightPluginInstance ( options : HighlighterOptions ) {
@@ -61,32 +64,34 @@ async function _getHighlightPlugin(options: HighlighterOptions) {
6164 key,
6265 instance : rehypeHighlight ,
6366 ...options ,
64- highlighter : async ( code , lang , theme , opts ) => {
65- const result = await highlighter ( code , lang , theme , opts )
66- const visitTree = {
67- type : 'element' ,
68- children : result . tree as Array < unknown > ,
69- }
70- if ( options . compress ) {
71- const stylesMap : Record < string , string > = { }
72- visit (
73- visitTree ,
74- node => ! ! ( node as HighlightedNode ) . properties ?. style ,
75- ( _node ) => {
76- const node = _node as HighlightedNode
77- const style = node . properties ! . style !
78- stylesMap [ style ] = stylesMap [ style ] || 's' + hash ( style ) . substring ( 0 , 4 )
79- node . properties ! . class = `${ node . properties ! . class || '' } ${ stylesMap [ style ] } ` . trim ( )
80- node . properties ! . style = undefined
81- } ,
82- )
67+ options : {
68+ highlighter : async ( code , lang , theme , opts ) => {
69+ const result = await highlighter ( code , lang , theme , opts )
70+ const visitTree = {
71+ type : 'element' ,
72+ children : result . tree as Array < unknown > ,
73+ }
74+ if ( options . compress ) {
75+ const stylesMap : Record < string , string > = { }
76+ visit (
77+ visitTree ,
78+ node => ! ! ( node as HighlightedNode ) . properties ?. style ,
79+ ( _node ) => {
80+ const node = _node as HighlightedNode
81+ const style = node . properties ! . style !
82+ stylesMap [ style ] = stylesMap [ style ] || 's' + hash ( style ) . substring ( 0 , 4 )
83+ node . properties ! . class = `${ node . properties ! . class || '' } ${ stylesMap [ style ] } ` . trim ( )
84+ node . properties ! . style = undefined
85+ } ,
86+ )
8387
84- result . style = Object . entries ( stylesMap ) . map ( ( [ style , cls ] ) => `.${ cls } {${ style } }` ) . join ( '' ) + result . style
85- }
88+ result . style = Object . entries ( stylesMap ) . map ( ( [ style , cls ] ) => `.${ cls } {${ style } }` ) . join ( '' ) + result . style
89+ }
8690
87- return result
91+ return result
92+ } ,
93+ theme : Object . fromEntries ( bundledThemes ) ,
8894 } ,
89- theme : Object . fromEntries ( bundledThemes ) ,
9095 }
9196 }
9297 return highlightPlugin
0 commit comments