@@ -19,17 +19,34 @@ export type TokenInfo = {
19
19
// Assumes identical code blocks should match the same lanugage
20
20
export const TOKEN_INFO_CACHE : Map < string , TokenInfo > = new Map ( ) ;
21
21
22
+ function makeCacheKey ( {
23
+ code,
24
+ language,
25
+ theme,
26
+ } : {
27
+ code : string ,
28
+ language : ?string ,
29
+ theme : string ,
30
+ } ) {
31
+ return `${ code } :${ language || '' } :${ theme } ` ;
32
+ }
33
+
22
34
export function registerTokenInfo ( {
23
35
code,
36
+ language,
37
+ theme,
24
38
tokenInfo,
25
39
} : {
26
40
code : string ,
41
+ language : ?string ,
42
+ theme : string ,
27
43
tokenInfo : TokenInfo ,
28
44
} ) {
29
- if ( TOKEN_INFO_CACHE . get ( code ) ) {
45
+ const cacheKey = makeCacheKey ( { code, language, theme} ) ;
46
+ if ( TOKEN_INFO_CACHE . has ( cacheKey ) ) {
30
47
return ;
31
48
}
32
- TOKEN_INFO_CACHE . set ( code , tokenInfo ) ;
49
+ TOKEN_INFO_CACHE . set ( cacheKey , tokenInfo ) ;
33
50
if ( TOKEN_INFO_CACHE . size > 5000 ) {
34
51
const firstKey = TOKEN_INFO_CACHE . keys ( ) . next ( ) . value ;
35
52
if ( firstKey ) {
@@ -41,17 +58,20 @@ export function registerTokenInfo({
41
58
export function fetchTokenInfo ( {
42
59
code,
43
60
language,
61
+ theme,
44
62
} : {
45
63
code : string ,
46
- language ?: ?string ,
64
+ language : ?string ,
65
+ theme : string ,
47
66
} ) : TokenInfo | Promise < TokenInfo > {
48
- const fromCache = TOKEN_INFO_CACHE . get ( code ) ;
67
+ const cacheKey = makeCacheKey ( { code, language, theme} ) ;
68
+ const fromCache = TOKEN_INFO_CACHE . get ( cacheKey ) ;
49
69
if ( fromCache ) {
50
70
return fromCache ;
51
71
}
52
72
const getUrl = `https://sourcecodeshots.com/api/token-info?code=${ encodeURIComponent (
53
73
code ,
54
- ) } &theme=${ Config . codeTheme } &language=${ language ? language : '' } `;
74
+ ) } &theme=${ theme } &language=${ language ? language : '' } `;
55
75
const resp =
56
76
getUrl . length < 2083
57
77
? fetch ( getUrl , {
@@ -67,13 +87,13 @@ export function fetchTokenInfo({
67
87
body : JSON . stringify ( {
68
88
code,
69
89
language : language ,
70
- theme : Config . codeTheme ,
90
+ theme,
71
91
} ) ,
72
92
} ) ;
73
93
return resp
74
94
. then ( ( r ) => r . json ( ) )
75
95
. then ( ( tokenInfo ) => {
76
- registerTokenInfo ( { code, tokenInfo} ) ;
96
+ registerTokenInfo ( { code, theme , language , tokenInfo} ) ;
77
97
return tokenInfo ;
78
98
} ) ;
79
99
}
@@ -100,17 +120,26 @@ export async function tokenInfosFromMarkdowns({
100
120
markdowns,
101
121
} : {
102
122
markdowns : Array < string > ,
103
- } ) : { [ code : string ] : TokenInfo } {
123
+ } ) : Array < {
124
+ code : string ,
125
+ language : ?string ,
126
+ theme : string ,
127
+ tokenInfo : TokenInfo ,
128
+ } > {
129
+ const theme = Config . codeTheme ;
104
130
const nodes = findCodeNodes ( markdowns . map ( parseMarkdown ) ) ;
105
- const entries = await Promise . all (
131
+ return await Promise . all (
106
132
nodes . map ( async ( node ) => {
107
133
const code = node . value ;
108
134
const language = node . lang ;
109
- const tokenInfo = await fetchTokenInfo ( { code : node . value , language} ) ;
110
- return [ code , tokenInfo ] ;
135
+ const tokenInfo = await fetchTokenInfo ( {
136
+ code : node . value ,
137
+ language,
138
+ theme,
139
+ } ) ;
140
+ return { code, language, theme, tokenInfo} ;
111
141
} ) ,
112
142
) ;
113
- return Object . fromEntries ( entries ) ;
114
143
}
115
144
116
145
export const defaultThemeColors = {
@@ -131,6 +160,16 @@ export const defaultThemeColors = {
131
160
'kimbie-dark' : { backgroundColor : '#221a0f' , foregroundColor : '#d3af86' } ,
132
161
'dimmed-monokai' : { backgroundColor : '#1e1e1e' , foregroundColor : '#c5c8c6' } ,
133
162
monokai : { backgroundColor : '#272822' , foregroundColor : '#f8f8f2' } ,
163
+ 'night-owl' : { backgroundColor : '#011627' , foregroundColor : '#d6deeb' } ,
164
+ 'night-owl-no-italic' : {
165
+ backgroundColor : '#011627' ,
166
+ foregroundColor : '#d6deeb' ,
167
+ } ,
168
+ 'night-owl-light' : { backgroundColor : '#FBFBFB' , foregroundColor : '#403f53' } ,
169
+ 'night-owl-light-no-italic' : {
170
+ backgroundColor : '#FBFBFB' ,
171
+ foregroundColor : '#403f53' ,
172
+ } ,
134
173
quietlight : { backgroundColor : '#F5F5F5' , foregroundColor : 'white' } ,
135
174
red : { backgroundColor : '#390000' , foregroundColor : '#F8F8F8' } ,
136
175
'solarized-dark' : { backgroundColor : '#002B36' , foregroundColor : 'white' } ,
0 commit comments