1
1
import path from 'path' ;
2
2
import * as shiki from 'shiki' ;
3
3
import { FormattedString } from './formattedString' ;
4
- import { parseColorDefinition , ThemeColor } from './themes' ;
4
+ import { ColorModifier , parseColorDefinition , ThemeColor } from './themes' ;
5
5
export type HighlightedText = [ string , ThemeColor | null ] ;
6
6
7
+ function parseShikiColor ( token : shiki . ThemedToken ) : ThemeColor {
8
+ let modifiers : ColorModifier [ ] | undefined ;
9
+ if (
10
+ token . fontStyle !== undefined &&
11
+ token . fontStyle !== shiki . FontStyle . NotSet &&
12
+ token . fontStyle !== shiki . FontStyle . None
13
+ ) {
14
+ modifiers = [ ] ;
15
+ if ( token . fontStyle & shiki . FontStyle . Bold ) {
16
+ modifiers . push ( 'bold' ) ;
17
+ }
18
+ if ( token . fontStyle & shiki . FontStyle . Italic ) {
19
+ modifiers . push ( 'italic' ) ;
20
+ }
21
+ if ( token . fontStyle & shiki . FontStyle . Underline ) {
22
+ modifiers . push ( 'underline' ) ;
23
+ }
24
+ }
25
+ const themeColor = parseColorDefinition ( {
26
+ color : token . color ,
27
+ backgroundColor : token . bgColor ,
28
+ modifiers,
29
+ } ) ;
30
+ return themeColor ;
31
+ }
32
+
7
33
export async function highlightSyntaxInLine (
8
34
line : FormattedString ,
9
35
fileName : string ,
@@ -23,14 +49,11 @@ export async function highlightSyntaxInLine(
23
49
theme,
24
50
} ) ;
25
51
26
- let index = 0 ;
27
- for ( const { content, color } of tokens . flat ( ) ) {
28
- if ( color ) {
29
- const syntaxColor = parseColorDefinition ( {
30
- color : color ,
31
- } ) ;
32
- line . addSpan ( index , index + content . length , syntaxColor ) ;
33
- index += content . length ;
34
- }
52
+ for ( const token of tokens . flat ( ) ) {
53
+ line . addSpan (
54
+ token . offset ,
55
+ token . offset + token . content . length ,
56
+ parseShikiColor ( token )
57
+ ) ;
35
58
}
36
59
}
0 commit comments