Skip to content

Commit 941286d

Browse files
committed
chore: wip
1 parent e8a9fa6 commit 941286d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+861
-750
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,10 @@ const langFromExt = getLanguageByExtension('.jsx') // Returns JavaScript languag
180180

181181
```typescript
182182
interface Token {
183-
type: string // Token scope (e.g., 'keyword.control.js', 'string.quoted.double.ts')
184-
content: string // The actual text content
185-
line: number // Line number (0-indexed)
186-
startIndex: number // Character position in the line
183+
type: string // Token scope (e.g., 'keyword.control.js', 'string.quoted.double.ts')
184+
content: string // The actual text content
185+
line: number // Line number (0-indexed)
186+
startIndex: number // Character position in the line
187187
}
188188

189189
interface LineTokens {

docs/advanced/batch-processing.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ When processing multiple files, batch processing provides:
1616
### Processing Multiple Files
1717

1818
```typescript
19-
import { Tokenizer } from 'ts-syntax-highlighter'
2019
import fs from 'node:fs/promises'
20+
import { Tokenizer } from 'ts-syntax-highlighter'
2121

2222
async function processFiles(files: string[], language: string) {
2323
const tokenizer = new Tokenizer(language)
@@ -44,7 +44,7 @@ async function processFilesParallel(files: string[], language: string) {
4444
const tokenizer = new Tokenizer(language)
4545

4646
const results = await Promise.all(
47-
files.map(async file => {
47+
files.map(async (file) => {
4848
const code = await fs.readFile(file, 'utf-8')
4949
const tokens = await tokenizer.tokenizeAsync(code)
5050
return { file, tokens }
@@ -74,13 +74,15 @@ async function processWithConcurrency(
7474

7575
async function processNext(): Promise<void> {
7676
const file = queue.shift()
77-
if (!file) return
77+
if (!file)
78+
return
7879

7980
try {
8081
const code = await fs.readFile(file, 'utf-8')
8182
const tokens = await tokenizer.tokenizeAsync(code)
8283
results.set(file, tokens)
83-
} catch (error) {
84+
}
85+
catch (error) {
8486
console.error(`Failed to process ${file}:`, error)
8587
results.set(file, null)
8688
}
@@ -129,13 +131,14 @@ async function batchProcess(
129131
const batch = files.slice(i, i + config.concurrency)
130132

131133
const batchResults = await Promise.all(
132-
batch.map(async file => {
134+
batch.map(async (file) => {
133135
try {
134136
const code = await fs.readFile(file, 'utf-8')
135137
const tokens = await tokenizer.tokenizeAsync(code)
136138
config.onSuccess?.(file)
137139
return { file, tokens, error: null }
138-
} catch (error) {
140+
}
141+
catch (error) {
139142
config.onError?.(file, error as Error)
140143
return { file, tokens: null, error: error as Error }
141144
}
@@ -183,12 +186,13 @@ async function processWithRetry(
183186
try {
184187
const code = await fs.readFile(file, 'utf-8')
185188
return await tokenizer.tokenizeAsync(code)
186-
} catch (error) {
189+
}
190+
catch (error) {
187191
lastError = error as Error
188192
if (attempt < maxRetries - 1) {
189193
// Wait before retry (exponential backoff)
190194
await new Promise(resolve =>
191-
setTimeout(resolve, Math.pow(2, attempt) * 100)
195+
setTimeout(resolve, 2 ** attempt * 100)
192196
)
193197
}
194198
}
@@ -205,11 +209,12 @@ async function batchProcessWithRetry(
205209
const tokenizer = new Tokenizer(language)
206210
const results = new Map()
207211

208-
const promises = files.map(async file => {
212+
const promises = files.map(async (file) => {
209213
try {
210214
const tokens = await processWithRetry(file, tokenizer, maxRetries)
211215
results.set(file, { success: true, tokens })
212-
} catch (error) {
216+
}
217+
catch (error) {
213218
results.set(file, { success: false, error: error as Error })
214219
}
215220
})
@@ -315,7 +320,7 @@ async function processInChunks(
315320
const chunk = files.slice(i, i + chunkSize)
316321

317322
const chunkResults = await Promise.all(
318-
chunk.map(async file => {
323+
chunk.map(async (file) => {
319324
const code = await fs.readFile(file, 'utf-8')
320325
return {
321326
file,
@@ -327,7 +332,8 @@ async function processInChunks(
327332
allResults.push(...chunkResults)
328333

329334
// Force garbage collection between chunks
330-
if (global.gc) global.gc()
335+
if (global.gc)
336+
global.gc()
331337
}
332338

333339
return allResults
@@ -352,7 +358,7 @@ async function processProject(
352358

353359
// Group by language
354360
const byLanguage = new Map<string, string[]>()
355-
files.forEach(file => {
361+
files.forEach((file) => {
356362
const ext = file.split('.').pop()
357363
const lang = ext === 'ts' || ext === 'tsx' ? 'typescript' : 'javascript'
358364

@@ -413,8 +419,8 @@ async function generateStats(files: string[], language: string): Promise<Stats>
413419
stats.totalFiles++
414420
stats.totalLines += tokens.length
415421

416-
tokens.forEach(line => {
417-
line.tokens.forEach(token => {
422+
tokens.forEach((line) => {
423+
line.tokens.forEach((token) => {
418424
stats.totalTokens++
419425

420426
// Count by type

docs/advanced/custom-themes.md

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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
139139
function 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
213225
const 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
260272
const 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

420435
function 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 {
436455
function 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
454476
import {
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

Comments
 (0)