-
-
Notifications
You must be signed in to change notification settings - Fork 320
/
code-to-tokens.ts
129 lines (113 loc) · 3.98 KB
/
code-to-tokens.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { codeToTokensBase } from './code-to-tokens-base'
import { codeToTokensWithThemes } from './code-to-tokens-themes'
import { ShikiError } from './error'
import type { CodeToTokensOptions, ShikiInternal, ThemedToken, ThemedTokenWithVariants, TokensResult } from './types'
import { applyColorReplacements, getTokenStyleObject, stringifyTokenStyle } from './utils'
/**
* High-level code-to-tokens API.
*
* It will use `codeToTokensWithThemes` or `codeToTokensBase` based on the options.
*/
export function codeToTokens(
internal: ShikiInternal,
code: string,
options: CodeToTokensOptions,
): TokensResult {
let bg: string
let fg: string
let tokens: ThemedToken[][]
let themeName: string
let rootStyle: string | undefined
if ('themes' in options) {
const {
defaultColor = 'light',
cssVariablePrefix = '--shiki-',
colorReplacements,
} = options
const themes = Object.entries(options.themes)
.filter(i => i[1])
.map(i => ({ color: i[0], theme: i[1]! }))
.sort((a, b) => a.color === defaultColor ? -1 : b.color === defaultColor ? 1 : 0)
if (themes.length === 0)
throw new ShikiError('`themes` option must not be empty')
const themeTokens = codeToTokensWithThemes(
internal,
code,
options,
)
if (defaultColor && !themes.find(t => t.color === defaultColor))
throw new ShikiError(`\`themes\` option must contain the defaultColor key \`${defaultColor}\``)
const themeRegs = themes.map(t => internal.getTheme(t.theme))
const themesOrder = themes.map(t => t.color)
tokens = themeTokens
.map(line => line.map(token => mergeToken(token, themesOrder, cssVariablePrefix, defaultColor)))
fg = themes.map((t, idx) => (idx === 0 && defaultColor
? ''
: `${cssVariablePrefix + t.color}:`) + (applyColorReplacements(themeRegs[idx].fg, colorReplacements) || 'inherit')).join(';')
bg = themes.map((t, idx) => (idx === 0 && defaultColor
? ''
: `${cssVariablePrefix + t.color}-bg:`) + (applyColorReplacements(themeRegs[idx].bg, colorReplacements) || 'inherit')).join(';')
themeName = `shiki-themes ${themeRegs.map(t => t.name).join(' ')}`
rootStyle = defaultColor ? undefined : [fg, bg].join(';')
}
else if ('theme' in options) {
const {
colorReplacements,
} = options
tokens = codeToTokensBase(
internal,
code,
options,
)
const _theme = internal.getTheme(options.theme)
bg = applyColorReplacements(_theme.bg, colorReplacements)
fg = applyColorReplacements(_theme.fg, colorReplacements)
themeName = _theme.name
}
else {
throw new ShikiError('Invalid options, either `theme` or `themes` must be provided')
}
return {
tokens,
fg,
bg,
themeName,
rootStyle,
}
}
function mergeToken(
merged: ThemedTokenWithVariants,
variantsOrder: string[],
cssVariablePrefix: string,
defaultColor: string | boolean,
) {
const token: ThemedToken = {
content: merged.content,
explanation: merged.explanation,
offset: merged.offset,
}
const styles = variantsOrder.map(t => getTokenStyleObject(merged.variants[t]))
// Get all style keys, for themes that missing some style, we put `inherit` to override as needed
const styleKeys = new Set(styles.flatMap(t => Object.keys(t)))
const mergedStyles = styles.reduce((acc, cur, idx) => {
for (const key of styleKeys) {
const value = cur[key] || 'inherit'
if (idx === 0 && defaultColor) {
acc[key] = value
}
else {
const keyName = key === 'color' ? '' : key === 'background-color' ? '-bg' : `-${key}`
const varKey = cssVariablePrefix + variantsOrder[idx] + (key === 'color' ? '' : keyName)
if (acc[key])
acc[key] += `;${varKey}:${value}`
else
acc[key] = `${varKey}:${value}`
}
}
return acc
}, {} as Record<string, string>)
token.htmlStyle = defaultColor
? stringifyTokenStyle(mergedStyles)
: Object.values(mergedStyles).join(';')
return token
}