-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.ts
251 lines (213 loc) · 5.41 KB
/
types.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import { IRawGrammar, IRawTheme, IRawThemeSetting } from "./deps.ts";
import { Lang } from "./languages.ts";
import { IThemedToken } from "./themed_tokenizer.ts";
import { Theme } from "./themes.ts";
export interface HighlighterOptions {
/**
* The theme to load upfront.
*/
theme?: IThemeRegistration;
/**
* A list of themes to load upfront.
*
* Default to: `['dark-plus', 'light-plus']`
*/
themes?: IThemeRegistration[];
/**
* A list of languages to load upfront.
*
* Default to all the bundled languages.
*/
langs?: (Lang | ILanguageRegistration)[];
/**
* Paths for loading themes and langs. Relative to the package's root.
*/
paths?: IHighlighterPaths;
}
export interface Highlighter {
/**
* Convert code to HTML tokens.
* `lang` and `theme` must have been loaded.
* @deprecated Please use the `codeToHtml(code, options?)` overload instead.
*/
codeToHtml(
code: string,
lang?: StringLiteralUnion<Lang>,
theme?: StringLiteralUnion<Theme>,
options?: HtmlOptions,
): string;
/**
* Convert code to HTML tokens.
* `lang` and `theme` must have been loaded.
*/
codeToHtml(code: string, options?: HtmlOptions): string;
/**
* Convert code to themed tokens for custom processing.
* `lang` and `theme` must have been loaded.
* You may customize the bundled HTML / SVG renderer or write your own
* renderer for another render target.
*/
codeToThemedTokens(
code: string,
lang?: StringLiteralUnion<Lang>,
theme?: StringLiteralUnion<Theme>,
options?: ThemedTokenizerOptions,
): IThemedToken[][];
/**
* Get the loaded theme
*/
getTheme(theme?: IThemeRegistration): IShikiTheme;
/**
* Load a theme
*/
loadTheme(theme: IThemeRegistration): Promise<void>;
/**
* Load a language
*/
loadLanguage(lang: ILanguageRegistration | Lang): Promise<void>;
/**
* Get all loaded themes
*/
getLoadedThemes(): Theme[];
/**
* Get all loaded languages
*/
getLoadedLanguages(): Lang[];
/**
* Get the foreground color for theme. Can be used for CSS `color`.
*/
getForegroundColor(theme?: StringLiteralUnion<Theme>): string;
/**
* Get the background color for theme. Can be used for CSS `background-color`.
*/
getBackgroundColor(theme?: StringLiteralUnion<Theme>): string;
setColorReplacements(map: Record<string, string>): void;
// codeToRawHtml?(code: string): string
// getRawCSS?(): string
// codeToImage?(): string
}
export interface IHighlighterPaths {
/**
* @default 'themes/'
*/
themes?: string;
/**
* @default 'languages/'
*/
languages?: string;
}
export type ILanguageRegistration =
& {
id: string;
scopeName: string;
aliases?: string[];
samplePath?: string;
/**
* A list of languages the current language embeds.
* If manually specifying languages to load, make sure to load the embedded
* languages for each parent language.
*/
embeddedLangs?: Lang[];
}
& (
| {
path: string;
grammar?: IRawGrammar;
}
| {
path?: string;
grammar: IRawGrammar;
}
);
export type IThemeRegistration = IShikiTheme | StringLiteralUnion<Theme>;
export interface IShikiTheme extends IRawTheme {
/**
* @description theme name
*/
name: string;
/**
* @description light/dark theme
*/
type: "light" | "dark" | "css";
/**
* @description tokenColors of the theme file
*/
settings: IRawThemeSetting[];
/**
* @description text default foreground color
*/
fg: string;
/**
* @description text default background color
*/
bg: string;
/**
* @description relative path of included theme
*/
include?: string;
/**
* @description color map of the theme file
*/
colors?: Record<string, string>;
}
/**
* type StringLiteralUnion<'foo'> = 'foo' | string
* This has auto completion whereas `'foo' | string` doesn't
* Adapted from https://github.com/microsoft/TypeScript/issues/29729
*/
// deno-lint-ignore ban-types
export type StringLiteralUnion<T extends U, U = string> = T | (U & {});
export interface HtmlOptions {
lang?: StringLiteralUnion<Lang>;
theme?: StringLiteralUnion<Theme>;
lineOptions?: LineOption[];
}
export interface HtmlRendererOptions {
langId?: string;
fg?: string;
bg?: string;
lineOptions?: LineOption[];
elements?: ElementsOptions;
}
export interface LineOption {
/**
* 1-based line number.
*/
line: number;
classes?: string[];
}
interface ElementProps {
children: string;
[key: string]: unknown;
}
interface PreElementProps extends ElementProps {
className: string;
style: string;
}
// deno-lint-ignore no-empty-interface
interface CodeElementProps extends ElementProps {}
interface LineElementProps extends ElementProps {
className: string;
lines: IThemedToken[][];
line: IThemedToken[];
index: number;
}
interface TokenElementProps extends ElementProps {
style: string;
tokens: IThemedToken[];
token: IThemedToken;
index: number;
}
export interface ElementsOptions {
pre?: (props: PreElementProps) => string;
code?: (props: CodeElementProps) => string;
line?: (props: LineElementProps) => string;
token?: (props: TokenElementProps) => string;
}
export interface ThemedTokenizerOptions {
/**
* Whether to include explanation of each token's matching scopes and
* why it's given its color. Default to false to reduce output verbosity.
*/
includeExplanation?: boolean;
}