1 | // Type definitions for prismjs 1.16
|
2 | // Project: http://prismjs.com/, https://github.com/leaverou/prism
|
3 | // Definitions by: Michael Schmidt <https://github.com/RunDevelopment>
|
4 | // ExE Boss <https://github.com/ExE-Boss>
|
5 | // Erik Lieben <https://github.com/eriklieben>
|
6 | // Andre Wiggins <https://github.com/andrewiggins>
|
7 | // Michał Miszczyszyn <https://github.com/mmiszy>
|
8 | // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
9 | // TypeScript Version: 2.8
|
10 |
|
11 | export as namespace Prism;
|
12 | export const languages: Languages;
|
13 | export const plugins: Record<string, any>;
|
14 |
|
15 | /**
|
16 | * A function which will be invoked after an element was successfully highlighted.
|
17 | *
|
18 | * @param element The element successfully highlighted.
|
19 | */
|
20 | export type HighlightCallback = (element: Element) => void;
|
21 |
|
22 | /**
|
23 | * This is the most high-level function in Prism’s API.
|
24 | * It fetches all the elements that have a `.language-xxxx` class and then calls {@link Prism.highlightElement} on
|
25 | * each one of them.
|
26 | *
|
27 | * This is equivalent to `Prism.highlightAllUnder(document, async, callback)`.
|
28 | *
|
29 | * @param [async=false] Same as in {@link Prism.highlightAllUnder}.
|
30 | * @param [callback] Same as in {@link Prism.highlightAllUnder}.
|
31 | */
|
32 | export function highlightAll(
|
33 | async?: boolean,
|
34 | callback?: HighlightCallback
|
35 | ): void;
|
36 |
|
37 | /**
|
38 | * Fetches all the descendants of `container` that have a `.language-xxxx` class and then calls
|
39 | * {@link Prism.highlightElement} on each one of them.
|
40 | *
|
41 | * The following hooks will be run:
|
42 | * 1. `before-highlightall`
|
43 | * 2. All hooks of {@link Prism.highlightElement} for each element.
|
44 | *
|
45 | * @param container The root element, whose descendants that have a `.language-xxxx` class will be highlighted.
|
46 | * @param [async=false] Whether each element is to be highlighted asynchronously using Web Workers.
|
47 | * @param [callback] An optional callback to be invoked on each element after its highlighting is done.
|
48 | */
|
49 | export function highlightAllUnder(
|
50 | container: ParentNode,
|
51 | async?: boolean,
|
52 | callback?: HighlightCallback
|
53 | ): void;
|
54 |
|
55 | /**
|
56 | * Highlights the code inside a single element.
|
57 | *
|
58 | * The following hooks will be run:
|
59 | * 1. `before-sanity-check`
|
60 | * 2. `before-highlight`
|
61 | * 3. All hooks of {@link Prism.highlightElement}. These hooks will only be run by the current worker if `async` is `true`.
|
62 | * 4. `before-insert`
|
63 | * 5. `after-highlight`
|
64 | * 6. `complete`
|
65 | *
|
66 | * @param element The element containing the code.
|
67 | * It must have a class of `language-xxxx` to be processed, where `xxxx` is a valid language identifier.
|
68 | * @param [async=false] Whether the element is to be highlighted asynchronously using Web Workers
|
69 | * to improve performance and avoid blocking the UI when highlighting very large chunks of code. This option is
|
70 | * [disabled by default](https://prismjs.com/faq.html#why-is-asynchronous-highlighting-disabled-by-default).
|
71 | *
|
72 | * Note: All language definitions required to highlight the code must be included in the main `prism.js` file for
|
73 | * asynchronous highlighting to work. You can build your own bundle on the
|
74 | * [Download page](https://prismjs.com/download.html).
|
75 | * @param [callback] An optional callback to be invoked after the highlighting is done.
|
76 | * Mostly useful when `async` is `true`, since in that case, the highlighting is done asynchronously.
|
77 | */
|
78 | export function highlightElement(
|
79 | element: Element,
|
80 | async?: boolean,
|
81 | callback?: HighlightCallback
|
82 | ): void;
|
83 |
|
84 | /**
|
85 | * Low-level function, only use if you know what you’re doing. It accepts a string of text as input
|
86 | * and the language definitions to use, and returns a string with the HTML produced.
|
87 | *
|
88 | * The following hooks will be run:
|
89 | * 1. `before-tokenize`
|
90 | * 2. `after-tokenize`
|
91 | * 3. `wrap`: On each {@link Prism.Token}.
|
92 | *
|
93 | * @param text A string with the code to be highlighted.
|
94 | * @param grammar An object containing the tokens to use.
|
95 | *
|
96 | * Usually a language definition like `Prism.languages.markup`.
|
97 | * @param language The name of the language definition passed to `grammar`.
|
98 | * @returns The highlighted HTML.
|
99 | *
|
100 | * @example
|
101 | * Prism.highlight('var foo = true;', Prism.languages.js, 'js');
|
102 | */
|
103 | export function highlight(
|
104 | text: string,
|
105 | grammar: Grammar,
|
106 | language: string
|
107 | ): string;
|
108 |
|
109 | /**
|
110 | * This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input
|
111 | * and the language definitions to use, and returns an array with the tokenized code.
|
112 | *
|
113 | * When the language definition includes nested tokens, the function is called recursively on each of these tokens.
|
114 | *
|
115 | * This method could be useful in other contexts as well, as a very crude parser.
|
116 | *
|
117 | * @param text A string with the code to be highlighted.
|
118 | * @param grammar An object containing the tokens to use.
|
119 | *
|
120 | * Usually a language definition like `Prism.languages.markup`.
|
121 | * @returns An array of strings, tokens and other arrays.
|
122 | */
|
123 | export function tokenize(
|
124 | text: string,
|
125 | grammar: Grammar
|
126 | ): Array<string | Token>;
|
127 |
|
128 | export interface Environment extends Record<string, any> {
|
129 | selector?: string | undefined;
|
130 | element?: Element | undefined;
|
131 | language?: string | undefined;
|
132 | grammar?: Grammar | undefined;
|
133 | code?: string | undefined;
|
134 | highlightedCode?: string | undefined;
|
135 | type?: string | undefined;
|
136 | content?: string | undefined;
|
137 | tag?: string | undefined;
|
138 | classes?: string[] | undefined;
|
139 | attributes?: Record<string, string> | undefined;
|
140 | parent?: Array<string | Token> | undefined;
|
141 | }
|
142 |
|
143 | export namespace util {
|
144 | interface Identifier {
|
145 | value: number;
|
146 | }
|
147 |
|
148 | /** Encode raw strings in tokens in preparation to display as HTML */
|
149 | function encode(tokens: TokenStream): TokenStream;
|
150 |
|
151 | /** Determine the type of the object */
|
152 | function type(o: null): "Null";
|
153 | function type(o: undefined): "Undefined";
|
154 | // tslint:disable:ban-types
|
155 | function type(o: boolean | Boolean): "Boolean";
|
156 | function type(o: number | Number): "Number";
|
157 | function type(o: string | String): "String";
|
158 | function type(o: Function): "Function";
|
159 | // tslint:enable:ban-types
|
160 | function type(o: RegExp): "RegExp";
|
161 | function type(o: any[]): "Array";
|
162 | function type(o: any): string;
|
163 |
|
164 | /** Get the unique id of this object or give it one if it does not have one */
|
165 | function objId(obj: any): Identifier;
|
166 |
|
167 | /** Deep clone a language definition (e.g. to extend it) */
|
168 | function clone<T>(o: T): T;
|
169 | }
|
170 |
|
171 | export type GrammarValue = RegExp | TokenObject | Array<RegExp | TokenObject>;
|
172 | export type Grammar = GrammarRest & Record<string, GrammarValue>;
|
173 | export interface GrammarRest {
|
174 | keyword?: GrammarValue | undefined;
|
175 | number?: GrammarValue | undefined;
|
176 | function?: GrammarValue | undefined;
|
177 | string?: GrammarValue | undefined;
|
178 | boolean?: GrammarValue | undefined;
|
179 | operator?: GrammarValue | undefined;
|
180 | punctuation?: GrammarValue | undefined;
|
181 | atrule?: GrammarValue | undefined;
|
182 | url?: GrammarValue | undefined;
|
183 | selector?: GrammarValue | undefined;
|
184 | property?: GrammarValue | undefined;
|
185 | important?: GrammarValue | undefined;
|
186 | style?: GrammarValue | undefined;
|
187 | comment?: GrammarValue | undefined;
|
188 | "class-name"?: GrammarValue | undefined;
|
189 |
|
190 | /**
|
191 | * An optional grammar object that will appended to this grammar.
|
192 | */
|
193 | rest?: Grammar | undefined;
|
194 | }
|
195 |
|
196 | /**
|
197 | * The expansion of a simple `RegExp` literal to support additional properties.
|
198 | */
|
199 | export interface TokenObject {
|
200 | /**
|
201 | * The regular expression of the token.
|
202 | */
|
203 | pattern: RegExp;
|
204 |
|
205 | /**
|
206 | * If `true`, then the first capturing group of `pattern` will (effectively) behave as a lookbehind
|
207 | * group meaning that the captured text will not be part of the matched text of the new token.
|
208 | */
|
209 | lookbehind?: boolean | undefined;
|
210 |
|
211 | /**
|
212 | * Whether the token is greedy.
|
213 | *
|
214 | * @default false
|
215 | */
|
216 | greedy?: boolean | undefined;
|
217 |
|
218 | /**
|
219 | * An optional alias or list of aliases.
|
220 | */
|
221 | alias?: string | string[] | undefined;
|
222 |
|
223 | /**
|
224 | * The nested tokens of this token.
|
225 | *
|
226 | * This can be used for recursive language definitions.
|
227 | *
|
228 | * Note that this can cause infinite recursion.
|
229 | */
|
230 | inside?: Grammar | undefined;
|
231 | }
|
232 | export type Languages = LanguageMapProtocol & LanguageMap;
|
233 | export interface LanguageMap {
|
234 | /**
|
235 | * Get a defined language's definition.
|
236 | */
|
237 | [language: string]: Grammar;
|
238 | }
|
239 | export interface LanguageMapProtocol {
|
240 | /**
|
241 | * Creates a deep copy of the language with the given id and appends the given tokens.
|
242 | *
|
243 | * If a token in `redef` also appears in the copied language, then the existing token in the copied language
|
244 | * will be overwritten at its original position.
|
245 | *
|
246 | * @param id The id of the language to extend. This has to be a key in `Prism.languages`.
|
247 | * @param redef The new tokens to append.
|
248 | * @returns The new language created.
|
249 | * @example
|
250 | * Prism.languages['css-with-colors'] = Prism.languages.extend('css', {
|
251 | * 'color': /\b(?:red|green|blue)\b/
|
252 | * });
|
253 | */
|
254 | extend(id: string, redef: Grammar): Grammar;
|
255 |
|
256 | /**
|
257 | * Inserts tokens _before_ another token in a language definition or any other grammar.
|
258 | *
|
259 | * As this needs to recreate the object (we cannot actually insert before keys in object literals),
|
260 | * we cannot just provide an object, we need an object and a key.
|
261 | *
|
262 | * If the grammar of `inside` and `insert` have tokens with the same name, the tokens in `inside` will be ignored.
|
263 | *
|
264 | * All references of the old object accessible from `Prism.languages` or `insert` will be replace with the new one.
|
265 | *
|
266 | * @param inside The property of `root` that contains the object to be modified.
|
267 | *
|
268 | * This is usually a language id.
|
269 | * @param before The key to insert before.
|
270 | * @param insert An object containing the key-value pairs to be inserted.
|
271 | * @param [root] The object containing `inside`, i.e. the object that contains the object that will be modified.
|
272 | *
|
273 | * Defaults to `Prism.languages`.
|
274 | * @returns The new grammar created.
|
275 | * @example
|
276 | * Prism.languages.insertBefore('markup', 'cdata', {
|
277 | * 'style': { ... }
|
278 | * });
|
279 | */
|
280 | insertBefore(
|
281 | inside: string,
|
282 | before: string,
|
283 | insert: Grammar,
|
284 | root?: LanguageMap
|
285 | ): Grammar;
|
286 | }
|
287 |
|
288 | export namespace hooks {
|
289 | /**
|
290 | * @param env The environment variables of the hook.
|
291 | */
|
292 | type HookCallback = (env: Environment) => void;
|
293 | type HookTypes = keyof HookEnvironmentMap;
|
294 |
|
295 | interface HookEnvironmentMap {
|
296 | "before-highlightall": RequiredEnvironment<"selector">;
|
297 |
|
298 | "before-sanity-check": ElementEnvironment;
|
299 | "before-highlight": ElementEnvironment;
|
300 |
|
301 | "before-insert": ElementHighlightedEnvironment;
|
302 | "after-highlight": ElementHighlightedEnvironment;
|
303 | complete: ElementHighlightedEnvironment;
|
304 |
|
305 | "before-tokenize": TokenizeEnvironment;
|
306 | "after-tokenize": TokenizeEnvironment;
|
307 |
|
308 | wrap: RequiredEnvironment<
|
309 | "type" | "content" | "tag" | "classes" | "attributes" | "language"
|
310 | >;
|
311 | }
|
312 |
|
313 | type RequiredEnvironment<
|
314 | T extends keyof Environment,
|
315 | U extends Environment = Environment
|
316 | > = U & Required<Pick<U, T>>;
|
317 | type ElementEnvironment = RequiredEnvironment<
|
318 | "element" | "language" | "grammar" | "code"
|
319 | >;
|
320 | type ElementHighlightedEnvironment = RequiredEnvironment<
|
321 | "highlightedCode",
|
322 | ElementEnvironment
|
323 | >;
|
324 | type TokenizeEnvironment = RequiredEnvironment<
|
325 | "code" | "grammar" | "language"
|
326 | >;
|
327 |
|
328 | interface RegisteredHooks {
|
329 | [hook: string]: HookCallback[];
|
330 | }
|
331 |
|
332 | const all: RegisteredHooks;
|
333 |
|
334 | /**
|
335 | * Adds the given callback to the list of callbacks for the given hook.
|
336 | *
|
337 | * The callback will be invoked when the hook it is registered for is run.
|
338 | * Hooks are usually directly run by a highlight function but you can also run hooks yourself.
|
339 | *
|
340 | * One callback function can be registered to multiple hooks and the same hook multiple times.
|
341 | *
|
342 | * @param name The name of the hook.
|
343 | * @param callback The callback function which is given environment variables.
|
344 | */
|
345 | function add<K extends keyof HookEnvironmentMap>(
|
346 | name: K,
|
347 | callback: (env: HookEnvironmentMap[K]) => void
|
348 | ): void;
|
349 | function add(name: string, callback: HookCallback): void;
|
350 |
|
351 | /**
|
352 | * Runs a hook invoking all registered callbacks with the given environment variables.
|
353 | *
|
354 | * Callbacks will be invoked synchronously and in the order in which they were registered.
|
355 | *
|
356 | * @param name The name of the hook.
|
357 | * @param env The environment variables of the hook passed to all callbacks registered.
|
358 | */
|
359 | function run<K extends keyof HookEnvironmentMap>(
|
360 | name: K,
|
361 | env: HookEnvironmentMap[K]
|
362 | ): void;
|
363 | function run(name: string, env: Environment): void;
|
364 | }
|
365 |
|
366 | export type TokenStream = string | Token | Array<string | Token>;
|
367 |
|
368 | export class Token {
|
369 | /**
|
370 | * Creates a new token.
|
371 | *
|
372 | * @param type See {@link Prism.Token#type type}
|
373 | * @param content See {@link Prism.Token#content content}
|
374 | * @param [alias] The alias(es) of the token.
|
375 | * @param [matchedStr=""] A copy of the full string this token was created from.
|
376 | * @param [greedy=false] See {@link Prism.Token#greedy greedy}
|
377 | */
|
378 | constructor(
|
379 | type: string,
|
380 | content: TokenStream,
|
381 | alias?: string | string[],
|
382 | matchedStr?: string,
|
383 | greedy?: boolean
|
384 | );
|
385 |
|
386 | /**
|
387 | * The type of the token.
|
388 | *
|
389 | * This is usually the key of a pattern in a { Grammar}.
|
390 | */
|
391 | type: string;
|
392 |
|
393 | /**
|
394 | * The strings or tokens contained by this token.
|
395 | *
|
396 | * This will be a token stream if the pattern matched also defined an `inside` grammar.
|
397 | */
|
398 | content: TokenStream;
|
399 |
|
400 | /**
|
401 | * The alias(es) of the token.
|
402 | *
|
403 | * @see TokenObject
|
404 | */
|
405 | alias: string | string[];
|
406 |
|
407 | /**
|
408 | * The length of the matched string or 0.
|
409 | */
|
410 | length: number;
|
411 |
|
412 | /**
|
413 | * Whether the pattern that created this token is greedy or not.
|
414 | *
|
415 | * @see TokenObject
|
416 | */
|
417 | greedy: boolean;
|
418 |
|
419 | /**
|
420 | * Converts the given token or token stream to an HTML representation.
|
421 | *
|
422 | * The following hooks will be run:
|
423 | * 1. `wrap`: On each {@link Prism.Token}.
|
424 | *
|
425 | * @param token The token or token stream to be converted.
|
426 | * @param language The name of current language.
|
427 | * @param [parent] The parent token stream, if any.
|
428 | * @return The HTML representation of the token or token stream.
|
429 | */
|
430 | static stringify(
|
431 | token: TokenStream,
|
432 | language: string,
|
433 | parent?: Array<string | Token>
|
434 | ): string;
|
435 | }
|