@@ -10,8 +10,6 @@ import { bundledLanguagesInfo, getHighlighter } from 'shiki/bundle/web'
10
10
import type { Element } from 'hast'
11
11
import style from './codeBlockShiki.css?raw'
12
12
13
- const languages = bundledLanguagesInfo . map ( item => [ item . id , item . name , ...( item . aliases ?? [ ] ) ] ) . flat ( )
14
-
15
13
export interface CodeBlockShikiOptions extends CodeBlockOptions {
16
14
theme : StringLiteralUnion < BundledTheme , string >
17
15
}
@@ -55,6 +53,9 @@ export const codeBlockShiki = CodeBlock.extend<CodeBlockShikiOptions, CodeBlockS
55
53
if (
56
54
transaction . docChanged
57
55
&& ( [ oldNodeName , newNodeName ] . includes ( this . name )
56
+ // only toggle language for code block
57
+ // @ts -expect-error attr
58
+ || transaction . steps . find ( item => item . attr === 'language' )
58
59
)
59
60
) {
60
61
return getDecorations ( {
@@ -132,6 +133,13 @@ export const codeBlockShiki = CodeBlock.extend<CodeBlockShikiOptions, CodeBlockS
132
133
} ,
133
134
} )
134
135
136
+ function formatLanguage ( lang : string ) {
137
+ if ( ! lang )
138
+ return null
139
+
140
+ return bundledLanguagesInfo . find ( item => [ item . id , item . name , ...( item . aliases ?? [ ] ) ] ) ?. id
141
+ }
142
+
135
143
function getDecorations ( {
136
144
doc,
137
145
name,
@@ -144,16 +152,17 @@ function getDecorations({
144
152
theme : CodeBlockShikiOptions [ 'theme' ]
145
153
} ) {
146
154
let decorations : Decoration [ ] = [ ]
155
+ if ( ! highlighter )
156
+ return decorations
147
157
148
158
findChildren ( doc , node => node . type . name === name ) . forEach ( ( block ) => {
149
159
// @ts -expect-error language
150
- const language = block . node . attrs . language || 'text'
151
- if ( ! languages . includes ( language ) || highlighter )
152
- return
160
+ block . node . attrs . language = formatLanguage ( block . node . attrs . language ) ?? 'text'
153
161
154
162
const preNode = highlighter ! . codeToHast ( block . node . textContent , {
155
163
theme,
156
- lang : language ,
164
+ // @ts -expect-error language
165
+ lang : block . node . attrs . language ,
157
166
} ) . children [ 0 ] as Element
158
167
159
168
decorations . push ( Decoration . node ( block . pos , block . pos + block . node . nodeSize , {
0 commit comments