@@ -8,12 +8,12 @@ ts-syntax-highlighter uses TextMate-style scope names to classify tokens:
88
99``` typescript
1010// Example token scopes
11- ' keyword.control.js' // if, for, while, return
12- ' storage.type.js' // const, let, var, function
13- ' string.quoted.double.js' // "string"
14- ' constant.numeric.js' // 42, 3.14
15- ' entity.name.function.js' // function names
16- ' comment.line.double-slash.js' // // comments
11+ ' keyword.control.js' // if, for, while, return
12+ ' storage.type.js' // const, let, var, function
13+ ' string.quoted.double.js' // "string"
14+ ' constant.numeric.js' // 42, 3.14
15+ ' entity.name.function.js' // function names
16+ ' comment.line.double-slash.js' // // comments
1717```
1818
1919## Scope Hierarchy
@@ -53,8 +53,8 @@ function extractScopes(code: string, language: string): Set<string> {
5353 const tokens = tokenizer .tokenize (code )
5454 const scopes = new Set <string >()
5555
56- tokens .forEach (line => {
57- line .tokens .forEach (token => {
56+ tokens .forEach (( line ) => {
57+ line .tokens .forEach (( token ) => {
5858 scopes .add (token .type )
5959 })
6060 })
@@ -138,34 +138,46 @@ const lightPalette: ThemePalette = {
138138``` typescript
139139function scopeToColor(scope : string , palette : ThemePalette ): string {
140140 // Keyword scopes
141- if (scope .includes (' keyword.control' )) return palette .keyword
142- if (scope .includes (' keyword.operator' )) return palette .operator
141+ if (scope .includes (' keyword.control' ))
142+ return palette .keyword
143+ if (scope .includes (' keyword.operator' ))
144+ return palette .operator
143145
144146 // Storage scopes
145- if (scope .includes (' storage.type' )) return palette .storage
147+ if (scope .includes (' storage.type' ))
148+ return palette .storage
146149
147150 // String scopes
148- if (scope .includes (' string.quoted' )) return palette .string
149- if (scope .includes (' string.template' )) return palette .string
151+ if (scope .includes (' string.quoted' ))
152+ return palette .string
153+ if (scope .includes (' string.template' ))
154+ return palette .string
150155
151156 // Number scopes
152- if (scope .includes (' constant.numeric' )) return palette .number
157+ if (scope .includes (' constant.numeric' ))
158+ return palette .number
153159
154160 // Function scopes
155- if (scope .includes (' entity.name.function' )) return palette .function
161+ if (scope .includes (' entity.name.function' ))
162+ return palette .function
156163
157164 // Comment scopes
158- if (scope .includes (' comment' )) return palette .comment
165+ if (scope .includes (' comment' ))
166+ return palette .comment
159167
160168 // Type scopes
161- if (scope .includes (' entity.name.type' )) return palette .type
162- if (scope .includes (' storage.type' )) return palette .type
169+ if (scope .includes (' entity.name.type' ))
170+ return palette .type
171+ if (scope .includes (' storage.type' ))
172+ return palette .type
163173
164174 // Constant scopes
165- if (scope .includes (' constant.language' )) return palette .constant
175+ if (scope .includes (' constant.language' ))
176+ return palette .constant
166177
167178 // Variable scopes
168- if (scope .includes (' variable' )) return palette .variable
179+ if (scope .includes (' variable' ))
180+ return palette .variable
169181
170182 // Default
171183 return palette .foreground
@@ -185,7 +197,7 @@ function generateThemeCSS(palette: ThemePalette, scopes: Set<string>): string {
185197 css += ` }\n\n `
186198
187199 // Generate rules for each scope
188- Array .from (scopes ).sort ().forEach (scope => {
200+ Array .from (scopes ).sort ().forEach (( scope ) => {
189201 const className = scope .replace (/ \. / g , ' -' )
190202 const color = scopeToColor (scope , palette )
191203
@@ -211,8 +223,8 @@ fs.writeFileSync('theme.css', css)
211223
212224``` typescript
213225const vscDarkTheme = {
214- background: ' #1e1e1e' ,
215- foreground: ' #d4d4d4' ,
226+ ' background' : ' #1e1e1e' ,
227+ ' foreground' : ' #d4d4d4' ,
216228
217229 // Comments
218230 ' comment.line' : ' #6a9955' ,
@@ -258,8 +270,8 @@ const vscDarkTheme = {
258270
259271``` typescript
260272const githubLightTheme = {
261- background: ' #ffffff' ,
262- foreground: ' #24292e' ,
273+ ' background' : ' #ffffff' ,
274+ ' foreground' : ' #24292e' ,
263275
264276 // Comments
265277 ' comment.line' : ' #6a737d' ,
@@ -320,14 +332,15 @@ function highlightWithTheme(
320332 html += ` border-radius: 4px; `
321333 html += ' "><code>'
322334
323- tokens .forEach (line => {
324- line .tokens .forEach (token => {
335+ tokens .forEach (( line ) => {
336+ line .tokens .forEach (( token ) => {
325337 const color = findColorForScope (token .type , theme )
326338 const escaped = escapeHtml (token .content )
327339
328340 if (color ) {
329341 html += ` <span style="color: ${color }">${escaped }</span> `
330- } else {
342+ }
343+ else {
331344 html += escaped
332345 }
333346 })
@@ -343,13 +356,15 @@ function findColorForScope(
343356 theme : Record <string , string >
344357): string | null {
345358 // Exact match
346- if (theme [scope ]) return theme [scope ]
359+ if (theme [scope ])
360+ return theme [scope ]
347361
348362 // Partial match (most specific first)
349363 const parts = scope .split (' .' )
350364 for (let i = parts .length ; i > 0 ; i -- ) {
351365 const partial = parts .slice (0 , i ).join (' .' )
352- if (theme [partial ]) return theme [partial ]
366+ if (theme [partial ])
367+ return theme [partial ]
353368 }
354369
355370 return null
@@ -373,8 +388,8 @@ function highlightWithClasses(code: string, language: string): string {
373388
374389 let html = ' <pre><code>'
375390
376- tokens .forEach (line => {
377- line .tokens .forEach (token => {
391+ tokens .forEach (( line ) => {
392+ line .tokens .forEach (( token ) => {
378393 const className = token .type .replace (/ \. / g , ' -' )
379394 const escaped = escapeHtml (token .content )
380395 html += ` <span class="${className }">${escaped }</span> `
@@ -419,12 +434,16 @@ const advancedTheme: Record<string, TokenStyle> = {
419434
420435function applyAdvancedStyle(scope : string ): string {
421436 const style = advancedTheme [scope ]
422- if (! style ) return ' '
437+ if (! style )
438+ return ' '
423439
424440 let css = ` color: ${style .color }; `
425- if (style .fontWeight ) css += ` font-weight: ${style .fontWeight }; `
426- if (style .fontStyle ) css += ` font-style: ${style .fontStyle }; `
427- if (style .textDecoration ) css += ` text-decoration: ${style .textDecoration }; `
441+ if (style .fontWeight )
442+ css += ` font-weight: ${style .fontWeight }; `
443+ if (style .fontStyle )
444+ css += ` font-style: ${style .fontStyle }; `
445+ if (style .textDecoration )
446+ css += ` text-decoration: ${style .textDecoration }; `
428447
429448 return css
430449}
@@ -436,9 +455,12 @@ function applyAdvancedStyle(scope: string): string {
436455function getSemanticColor(token : Token , context : Context ): string {
437456 // Color based on context
438457 if (token .type .includes (' variable' )) {
439- if (context .isParameter ) return ' #9cdcfe'
440- if (context .isConstant ) return ' #4fc1ff'
441- if (context .isGlobal ) return ' #4ec9b0'
458+ if (context .isParameter )
459+ return ' #9cdcfe'
460+ if (context .isConstant )
461+ return ' #4fc1ff'
462+ if (context .isGlobal )
463+ return ' #4ec9b0'
442464 return ' #9cdcfe'
443465 }
444466
@@ -452,12 +474,12 @@ function getSemanticColor(token: Token, context: Context): string {
452474
453475``` typescript
454476import {
455- vscDarkTheme ,
456- githubLightTheme ,
457477 draculaTheme ,
478+ githubLightTheme ,
458479 monokaiTheme ,
459480 solarizedDarkTheme ,
460- solarizedLightTheme
481+ solarizedLightTheme ,
482+ vscDarkTheme
461483} from ' ts-syntax-highlighter/themes'
462484
463485// Apply theme
0 commit comments