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