1- import { parseMarkdown } from '@nuxtjs/mdc/runtime'
1+ import { rehypeHighlight , parseMarkdown } from '@nuxtjs/mdc/runtime'
22import { createOnigurumaEngine } from 'shiki/engine/oniguruma'
33import { createJavaScriptRegexEngine } from 'shiki'
44
@@ -9,27 +9,17 @@ export function createShikiHighlighter({
99 bundledThemes = { } ,
1010 getMdcConfigs,
1111 options : shikiOptions ,
12- engine,
12+ engine
1313} = { } ) {
1414 let shiki
1515 let configs
1616 async function _getShiki ( ) {
17- const {
18- createHighlighterCore,
19- addClassToHast,
20- isSpecialLang,
21- isSpecialTheme,
22- } = await import ( 'shiki/core' )
23- const {
24- transformerNotationDiff,
25- transformerNotationErrorLevel,
26- transformerNotationFocus,
27- transformerNotationHighlight,
28- } = await import ( '@shikijs/transformers' )
17+ const { createHighlighterCore, addClassToHast, isSpecialLang, isSpecialTheme } = await import ( 'shiki/core' )
18+ const { transformerNotationDiff, transformerNotationErrorLevel, transformerNotationFocus, transformerNotationHighlight } = await import ( '@shikijs/transformers' )
2919 const shiki2 = await createHighlighterCore ( {
3020 langs,
3121 themes,
32- engine : engine || createJavaScriptRegexEngine ( ) ,
22+ engine : engine || createJavaScriptRegexEngine ( )
3323 } )
3424 for await ( const config of await getConfigs ( ) ) {
3525 await config . shiki ?. setup ?. ( shiki2 )
@@ -43,8 +33,8 @@ export function createShikiHighlighter({
4333 transformerNotationDiff ( ) ,
4434 transformerNotationErrorLevel ( ) ,
4535 transformerNotationFocus ( ) ,
46- transformerNotationHighlight ( ) ,
47- ] ,
36+ transformerNotationHighlight ( )
37+ ]
4838 }
4939 }
5040 async function getShiki ( ) {
@@ -65,13 +55,13 @@ export function createShikiHighlighter({
6555 addClassToHast,
6656 isSpecialLang,
6757 isSpecialTheme,
68- transformers : baseTransformers ,
58+ transformers : baseTransformers
6959 } = await getShiki ( )
7060 const codeToHastOptions = {
7161 defaultColor : false ,
7262 meta : {
73- __raw : options . meta ,
74- } ,
63+ __raw : options . meta
64+ }
7565 }
7666 if ( lang === 'ts-type' || lang === 'typescript-type' ) {
7767 lang = 'typescript'
@@ -80,51 +70,36 @@ export function createShikiHighlighter({
8070 lang = 'vue'
8171 codeToHastOptions . grammarContextCode = '<template>'
8272 }
83- const themesObject = {
84- ...( typeof theme === 'string' ? { default : theme } : theme || { } ) ,
85- }
73+ const themesObject = { ...typeof theme === 'string' ? { default : theme } : theme || { } }
8674 const loadedThemes = shiki2 . getLoadedThemes ( )
8775 const loadedLanguages = shiki2 . getLoadedLanguages ( )
88- if (
89- typeof lang === 'string' &&
90- ! loadedLanguages . includes ( lang ) &&
91- ! isSpecialLang ( lang )
92- ) {
76+ if ( typeof lang === 'string' && ! loadedLanguages . includes ( lang ) && ! isSpecialLang ( lang ) ) {
9377 if ( bundledLangs [ lang ] ) {
9478 await shiki2 . loadLanguage ( bundledLangs [ lang ] )
9579 } else {
9680 if ( import . meta. dev ) {
97- console . warn (
98- `[@nuxtjs/mdc] Language "${ lang } " is not loaded to the Shiki highlighter, fallback to plain text. Add the language to "mdc.highlight.langs" to fix this.` ,
99- )
81+ console . warn ( `[@nuxtjs/mdc] Language "${ lang } " is not loaded to the Shiki highlighter, fallback to plain text. Add the language to "mdc.highlight.langs" to fix this.` )
10082 }
10183 lang = 'text'
10284 }
10385 }
10486 for ( const [ color , theme2 ] of Object . entries ( themesObject ) ) {
105- if (
106- typeof theme2 === 'string' &&
107- ! loadedThemes . includes ( theme2 ) &&
108- ! isSpecialTheme ( theme2 )
109- ) {
87+ if ( typeof theme2 === 'string' && ! loadedThemes . includes ( theme2 ) && ! isSpecialTheme ( theme2 ) ) {
11088 if ( bundledThemes [ theme2 ] ) {
11189 await shiki2 . loadTheme ( bundledThemes [ theme2 ] )
11290 } else {
11391 if ( import . meta. dev ) {
114- console . warn (
115- `[@nuxtjs/mdc] Theme "${ theme2 } " is not loaded to the Shiki highlighter. Add the theme to "mdc.highlight.themes" to fix this.` ,
116- )
92+ console . warn ( `[@nuxtjs/mdc] Theme "${ theme2 } " is not loaded to the Shiki highlighter. Add the theme to "mdc.highlight.themes" to fix this.` )
11793 }
11894 themesObject [ color ] = 'none'
11995 }
12096 }
12197 }
122- const transformers = [ ...baseTransformers ]
98+ const transformers = [
99+ ...baseTransformers
100+ ]
123101 for ( const config of await getConfigs ( ) ) {
124- const newTransformers =
125- typeof config . shiki ?. transformers === 'function'
126- ? await config . shiki ?. transformers ( code , lang , theme , options )
127- : config . shiki ?. transformers || [ ]
102+ const newTransformers = typeof config . shiki ?. transformers === 'function' ? await config . shiki ?. transformers ( code , lang , theme , options ) : config . shiki ?. transformers || [ ]
128103 transformers . push ( ...newTransformers )
129104 }
130105 const root = shiki2 . codeToHast ( code . trimEnd ( ) , {
@@ -139,52 +114,40 @@ export function createShikiHighlighter({
139114 if ( options . highlights ?. includes ( line ) )
140115 addClassToHast ( node , 'highlight' )
141116 node . properties . line = line
142- } ,
117+ }
143118 } ,
144119 {
145120 name : 'mdc:newline' ,
146121 line ( node ) {
147122 if ( code ?. includes ( '\n' ) ) {
148- if (
149- node . children . length === 0 ||
150- ( node . children . length === 1 &&
151- node . children [ 0 ] . type === 'element' &&
152- node . children [ 0 ] . children . length === 1 &&
153- node . children [ 0 ] . children [ 0 ] . type === 'text' &&
154- node . children [ 0 ] . children [ 0 ] . value === '' )
155- ) {
156- node . children = [
157- {
158- type : 'element' ,
159- tagName : 'span' ,
160- properties : {
161- emptyLinePlaceholder : true ,
162- } ,
163- children : [ { type : 'text' , value : '\n' } ] ,
123+ if ( node . children . length === 0 || ( node . children . length === 1 && node . children [ 0 ] . type === 'element' && node . children [ 0 ] . children . length === 1 && node . children [ 0 ] . children [ 0 ] . type === 'text' && node . children [ 0 ] . children [ 0 ] . value === '' ) ) {
124+ node . children = [ {
125+ type : 'element' ,
126+ tagName : 'span' ,
127+ properties : {
128+ emptyLinePlaceholder : true
164129 } ,
165- ]
130+ children : [ { type : 'text' , value : '\n' } ]
131+ } ]
166132 return
167133 }
168134 const last = node . children . at ( - 1 )
169135 if ( last ?. type === 'element' && last . tagName === 'span' ) {
170136 const text = last . children . at ( - 1 )
171- if ( text ?. type === 'text' ) text . value += '\n'
137+ if ( text ?. type === 'text' )
138+ text . value += '\n'
172139 }
173140 }
174- } ,
175- } ,
176- ] ,
141+ }
142+ }
143+ ]
177144 } )
178145 const preEl = root . children [ 0 ]
179146 const codeEl = preEl . children [ 0 ]
180147 const wrapperStyle = shikiOptions ?. wrapperStyle
181- preEl . properties . style = wrapperStyle
182- ? typeof wrapperStyle === 'string'
183- ? wrapperStyle
184- : preEl . properties . style
185- : ''
148+ preEl . properties . style = wrapperStyle ? typeof wrapperStyle === 'string' ? wrapperStyle : preEl . properties . style : ''
186149 const styles = [ ]
187- Object . keys ( themesObject ) . forEach ( ( color ) => {
150+ for ( const color in themesObject ) {
188151 const colorScheme = color !== 'default' ? `.${ color } ` : ''
189152 styles . push (
190153 wrapperStyle ? `${ colorScheme } .shiki,` : '' ,
@@ -194,7 +157,7 @@ export function createShikiHighlighter({
194157 `font-style: var(--shiki-${ color } -font-style);` ,
195158 `font-weight: var(--shiki-${ color } -font-weight);` ,
196159 `text-decoration: var(--shiki-${ color } -text-decoration);` ,
197- '}' ,
160+ '}'
198161 )
199162 styles . push (
200163 `html${ colorScheme } .shiki span {` ,
@@ -203,16 +166,14 @@ export function createShikiHighlighter({
203166 `font-style: var(--shiki-${ color } -font-style);` ,
204167 `font-weight: var(--shiki-${ color } -font-weight);` ,
205168 `text-decoration: var(--shiki-${ color } -text-decoration);` ,
206- '}' ,
169+ '}'
207170 )
208- } )
171+ }
209172 return {
210173 tree : codeEl . children ,
211- className : Array . isArray ( preEl . properties . class )
212- ? preEl . properties . class . join ( ' ' )
213- : preEl . properties . class ,
174+ className : Array . isArray ( preEl . properties . class ) ? preEl . properties . class . join ( ' ' ) : preEl . properties . class ,
214175 inlineStyle : preEl . properties . style ,
215- style : styles . join ( '' ) ,
176+ style : styles . join ( '' )
216177 }
217178 }
218179 return highlighter
@@ -228,24 +189,14 @@ const bundledLangs = {
228189 ts : ( ) => import ( 'shiki/langs/ts.mjs' ) ,
229190 tsx : ( ) => import ( 'shiki/langs/tsx.mjs' ) ,
230191 jsx : ( ) => import ( 'shiki/langs/jsx.mjs' ) ,
231- json : ( ) => import ( 'shiki/langs/json.mjs' ) ,
232- python : ( ) => import ( 'shiki/langs/python.mjs' ) ,
233- shell : ( ) => import ( 'shiki/langs/shell.mjs' ) ,
234- toml : ( ) => import ( 'shiki/langs/toml.mjs' ) ,
192+ json : ( ) => import ( 'shiki/langs/json.mjs' )
193+
235194}
236195const bundledThemes = {
237- 'material-theme-lighter' : ( ) =>
238- import ( 'shiki/themes/material-theme-lighter.mjs' ) . then ( ( r ) => r . default ) ,
239- 'material-theme-palenight' : ( ) =>
240- import ( 'shiki/themes/material-theme-palenight.mjs' ) . then ( ( r ) => r . default ) ,
241- }
242- const options = {
243- theme : {
244- light : 'material-theme-lighter' ,
245- default : 'material-theme-lighter' ,
246- dark : 'material-theme-palenight' ,
247- } ,
196+ 'material-theme-lighter' : ( ) => import ( 'shiki/themes/material-theme-lighter.mjs' ) . then ( r => r . default ) ,
197+ 'material-theme-palenight' : ( ) => import ( 'shiki/themes/material-theme-palenight.mjs' ) . then ( r => r . default )
248198}
199+ const options = { theme : { light : 'material-theme-lighter' , default : 'material-theme-lighter' , dark : 'material-theme-palenight' } }
249200let configs
250201export function getMdcConfigs ( ) {
251202 if ( ! configs ) {
@@ -255,31 +206,24 @@ export function getMdcConfigs() {
255206 return configs
256207}
257208const engine = createOnigurumaEngine ( ( ) => import ( 'shiki/wasm' ) )
258- const highlighter = createShikiHighlighter ( {
259- bundledLangs,
260- bundledThemes,
261- options,
262- getMdcConfigs,
263- engine,
264- } )
209+ const highlighter = createShikiHighlighter ( { bundledLangs, bundledThemes, options, getMdcConfigs, engine } )
265210
266211export async function parseMdc ( content ) {
267- const { default : rehypeHighlight } = await import ( '@nuxtjs/mdc/runtime/highlighter/rehype-nuxt' )
268212 return await parseMarkdown ( content , {
269213 rehype : {
270214 plugins : {
271215 highlight : {
272- instance : rehypeHighlight ,
273- } ,
274- } ,
216+ instance : rehypeHighlight
217+ }
218+ }
275219 } ,
276220 highlight : {
277221 highlighter,
278- langs : [ 'js' , 'vue' , 'python' , 'shell' , 'toml' ] ,
222+ langs : [ 'js' , 'vue' , 'python' , 'shell' , 'toml' ] ,
279223 theme : {
280224 default : 'material-theme-lighter' ,
281- dark : 'material-theme-palenight' ,
282- } ,
283- } ,
225+ dark : 'material-theme-palenight'
226+ }
227+ }
284228 } )
285229}
0 commit comments