11import path from 'path' ;
22import * as shiki from 'shiki' ;
33import { FormattedString } from './formattedString' ;
4- import { parseColorDefinition , ThemeColor } from './themes' ;
4+ import { ColorModifier , parseColorDefinition , ThemeColor } from './themes' ;
55export type HighlightedText = [ string , ThemeColor | null ] ;
66
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+
733export async function highlightSyntaxInLine (
834 line : FormattedString ,
935 fileName : string ,
@@ -23,14 +49,11 @@ export async function highlightSyntaxInLine(
2349 theme,
2450 } ) ;
2551
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+ ) ;
3558 }
3659}
0 commit comments