diff --git a/prismjs/index.d.ts b/prismjs/index.d.ts index c3bbffae02..f95cabe990 100644 --- a/prismjs/index.d.ts +++ b/prismjs/index.d.ts @@ -1,164 +1,210 @@ -// Type definitions for prism v1.4.4 +// Type definitions for prism 1.6 // Project: http://prismjs.com/ -// Definitions by: Erik Lieben +// Definitions by: Erik Lieben , Andre Wiggins // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -declare namespace PrismJS { +export as namespace Prism; - /** - * The Prism object - */ - interface Prism { +/** + * @deprecated + * Use the Prism namespace directly directly + */ +export as namespace PrismJS; - util: Util; - languages: Languages; - plugins: any; +export const util: Util; +export const languages: Languages; +export const plugins: any; +export const hooks: Hooks; - /** - * This is the most high-level function in Prism’s API. It fetches all the elements that have a .language-xxxx class and then calls Prism.highlightElement() on each one of them. - * - * @param {boolean} async - Whether to use Web Workers to improve performance and avoid blocking the UI when highlighting very large chunks of code. False by default. - * @param {(element:Element)=>void} callback? - An optional callback to be invoked after the highlighting is done. Mostly useful when async is true, since in that case, the highlighting is done asynchronously. - * @returns void - */ - highlightAll(async: boolean, callback?: (element: Element) => void): void; +/** + * This is the most high-level function in Prism’s API. It fetches all the elements that have a .language-xxxx class and then calls Prism.highlightElement() on each one of them. + * + * @param {boolean} async - Whether to use Web Workers to improve performance and avoid blocking the UI when highlighting very large chunks of code. False by default. + * @param {(element:Element)=>void} callback? - An optional callback to be invoked after the highlighting is done. Mostly useful when async is true, since in that case, the highlighting is done asynchronously. + * @returns void + */ +export function highlightAll(async: boolean, callback?: (element: Element) => void): void; - /** - * Highlights the code inside a single element. - * - * @param {Element} element - The element containing the code. It must have a class of language-xxxx to be processed, where xxxx is a valid language identifier. - * @param {boolean} async - Whether to use Web Workers to improve performance and avoid blocking the UI when highlighting very large chunks of code. False by default. - * @param {(element:Element)=>void} callback? - An optional callback to be invoked after the highlighting is done. Mostly useful when async is true, since in that case, the highlighting is done asynchronously. - * @returns void - */ - highlightElement(element: Element, async: boolean, callback?: (element: Element) => void): void; +/** + * Highlights the code inside a single element. + * + * @param {Element} element - The element containing the code. It must have a class of language-xxxx to be processed, where xxxx is a valid language identifier. + * @param {boolean} async - Whether to use Web Workers to improve performance and avoid blocking the UI when highlighting very large chunks of code. False by default. + * @param {(element:Element)=>void} callback? - An optional callback to be invoked after the highlighting is done. Mostly useful when async is true, since in that case, the highlighting is done asynchronously. + * @returns void + */ +export function highlightElement(element: Element, async: boolean, callback?: (element: Element) => void): void; - /** - * Low-level function, only use if you know what you’re doing. It accepts a string of text as input and the language definitions to use, and returns a string with the HTML produced. - * - * @param {string} text - A string with the code to be highlighted. - * @param {LanguageDefinition} grammer - An object containing the tokens to use. Usually a language definition like Prism.languages.markup - * @param {LanguageDefinition} language - * @returns string - The highlighted HTML - */ - highlight(text: string, grammer: LanguageDefinition, language?: LanguageDefinition): string; +/** + * Low-level function, only use if you know what you’re doing. It accepts a string of text as input and the language definitions to use, and returns a string with the HTML produced. + * + * @param {string} text - A string with the code to be highlighted. + * @param {LanguageDefinition} grammer - An object containing the tokens to use. Usually a language definition like Prism.languages.markup + * @param {LanguageDefinition} language + * @returns string - The highlighted HTML + */ +export function highlight(text: string, grammer: LanguageDefinition, language?: LanguageDefinition): string; - /** - * This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input and the language definitions to use, - * and returns an array with the tokenized code. When the language definition includes nested tokens, the function is called recursively on each of - * these tokens. This method could be useful in other contexts as well, as a very crude parser. - * - * @param {string} text - A string with the code to be highlighted. - * @param {LanguageDefinition} grammar - An object containing the tokens to use. Usually a language definition like Prism.languages.markup - * @param {LanguageDefinition} language - * @returns Array - An array of strings, tokens (class Prism.Token) and other arrays. - */ - tokenize(text: string, grammar: LanguageDefinition, language?: LanguageDefinition): Array; +/** + * This is the heart of Prism, and the most low-level function you can use. It accepts a string of text as input and the language definitions to use, + * and returns an array with the tokenized code. When the language definition includes nested tokens, the function is called recursively on each of + * these tokens. This method could be useful in other contexts as well, as a very crude parser. + * + * @param {string} text - A string with the code to be highlighted. + * @param {LanguageDefinition} grammar - An object containing the tokens to use. Usually a language definition like Prism.languages.markup + * @returns Array - An array of strings, tokens (class Prism.Token) and other arrays. + */ +export function tokenize(text: string, grammar: LanguageDefinition): Array; - fileHighlight(): void; +export function fileHighlight(): void; - hooks: Hooks; - } - - interface Environment { - element?: Element; - language?: LanguageDefinition; - grammer?: any; - code?: any; - highlightedCode?: any; - type?: string; - content?:string; - tag?: string; - classes?: Array; - attributes?: Array | Object; - parent?: Element; - } - - interface Identifier { - value: number; - } - - interface Util { - encode(tokens:Token | Array | string): Token | Array | string; - type(o: Object): string; - objId(obj: Object): Identifier; - clone(o: LanguageDefinition): LanguageDefinition; - } - - interface LanguageDefinition { - keyword?: RegExp | LanguageDefinition; - number?: RegExp | LanguageDefinition; - function?: RegExp | LanguageDefinition; - string?: RegExp | LanguageDefinition; - boolean?: RegExp | LanguageDefinition; - operator?: RegExp | LanguageDefinition; - punctuation?: RegExp | LanguageDefinition; - atrule?: RegExp | LanguageDefinition; - url?: RegExp | LanguageDefinition; - selector?: RegExp | LanguageDefinition; - property?: RegExp | LanguageDefinition; - important?: RegExp | LanguageDefinition; - style?: RegExp | LanguageDefinition; - - /** - * This option can be used to define one or more aliases for the matched token. The result will be, that the styles - * of the token and its aliases are combined. This can be useful, to combine the styling of a well known token, - * which is already supported by most of the themes, with a semantically correct token name. The option can be - * set to a string literal or an array of string literals. In the following example the token name latex-equation - * is not supported by any theme, but it will be highlighted the same as a string. - */ - alias?: string; - - pattern?: RegExp; - - /** - * This option mitigates JavaScript’s lack of lookbehind. When set to true, the first capturing group in the regex - * pattern is discarded when matching this token, so it effectively behaves as if it was lookbehind - */ - lookbehind?: boolean; - - /** - * This property accepts another object literal, with tokens that are allowed to be nested in this token. - * This makes it easier to define certain languages. However, keep in mind that they’re slower and if coded poorly, - * can even result in infinite recursion. For an example of nested tokens, check out the Markup language definition - */ - inside?: LanguageDefinition; - - /** - * Accepts an object literal with tokens and appends them to the end of the current object literal. - */ - rest?: Array; - } - - interface Languages extends Array { - - extend(id: string, redef: LanguageDefinition): LanguageDefinition; - - /** - * Insert a token before another token in a language literal - * As this needs to recreate the object (we cannot actually insert before keys in object literals), - * we cannot just provide an object, we need anobject and a key. - * @param inside The key (or language id) of the parent - * @param before The key to insert before. If not provided, the function appends instead. - * @param insert Object with the key/value pairs to insert - * @param root The object that contains `inside`. If equal to Prism.languages, it can be omitted. - */ - insertBefore(inside: string, before: string, insert: LanguageDefinition, root: Object): any; - } - - interface Hooks { - all: Array void>>; - add(name: string, callback: (env: Environment) => void):void; - run(name: string, env: Environment): void; - } - - interface Token { - type: string; - content: Token | Array | string; - alias: string; - stringify(o:string| Array, language: LanguageDefinition, parent: HTMLPreElement): string; - } +interface Environment { + element?: Element; + language?: LanguageDefinition; + grammar?: any; + code?: any; + highlightedCode?: any; + type?: string; + content?: string; + tag?: string; + classes?: string[]; + attributes?: any; + parent?: Element; } -declare var Prism : PrismJS.Prism; \ No newline at end of file +interface Identifier { + value: number; +} + +interface Util { + /** Encode raw strings in tokens in preparation to display as HTML */ + encode(tokens: TokenNode): TokenNode; + + /** Determine the type of the object */ + type(o: any): string; + + /** Get the unique id of this object or give it one if it does not have one */ + objId(obj: any): Identifier; + + /** Deep clone a language definition (e.g. to extend it) */ + clone(o: LanguageDefinition): LanguageDefinition; +} + +interface LanguageDefinition { + keyword?: RegExp | LanguageDefinition; + number?: RegExp | LanguageDefinition; + function?: RegExp | LanguageDefinition; + string?: RegExp | LanguageDefinition; + boolean?: RegExp | LanguageDefinition; + operator?: RegExp | LanguageDefinition; + punctuation?: RegExp | LanguageDefinition; + atrule?: RegExp | LanguageDefinition; + url?: RegExp | LanguageDefinition; + selector?: RegExp | LanguageDefinition; + property?: RegExp | LanguageDefinition; + important?: RegExp | LanguageDefinition; + style?: RegExp | LanguageDefinition; + + /** + * This option can be used to define one or more aliases for the matched token. The result will be, that the styles + * of the token and its aliases are combined. This can be useful, to combine the styling of a well known token, + * which is already supported by most of the themes, with a semantically correct token name. The option can be + * set to a string literal or an array of string literals. In the following example the token name latex-equation + * is not supported by any theme, but it will be highlighted the same as a string. + */ + alias?: string; + + pattern?: RegExp; + + /** + * This option mitigates JavaScript’s lack of lookbehind. When set to true, the first capturing group in the regex + * pattern is discarded when matching this token, so it effectively behaves as if it was lookbehind + */ + lookbehind?: boolean; + + /** + * This property accepts another object literal, with tokens that are allowed to be nested in this token. + * This makes it easier to define certain languages. However, keep in mind that they’re slower and if coded poorly, + * can even result in infinite recursion. For an example of nested tokens, check out the Markup language definition + */ + inside?: LanguageDefinition; + + /** + * Accepts an object literal with tokens and appends them to the end of the current object literal. + */ + rest?: Token[]; +} + +interface Languages { + + /** Get a defined language's definition */ + [key: string]: LanguageDefinition; + + /** + * Extend a language definition + * @param id The language definition to extend + * @param redef The new language definition to extend the original + */ + extend(id: string, redef: LanguageDefinition): LanguageDefinition; + + /** + * Insert a token before another token in a language literal + * As this needs to recreate the object (we cannot actually insert before keys in object literals), + * we cannot just provide an object, we need anobject and a key. + * @param inside The key (or language id) of the parent + * @param before The key to insert before. If not provided, the function appends instead. + * @param insert Object with the key/value pairs to insert + * @param root The object that contains `inside`. If equal to Prism.languages, it can be omitted. + */ + insertBefore(inside: string, before: string, insert: LanguageDefinition, root: LanguageDefinition): any; +} + +type HookCallback = (env: Environment) => void; +type AvailableHooks = "before-highlightall" + | "before-sanity-check" + | "before-highlight" + | "before-insert" + | "after-highlight" + | "complete" + | "wrap"; + +interface Hooks { + all: Array void>>; + + add(name: AvailableHooks | string, callback: HookCallback): void; + + run(name: AvailableHooks | string, env: Environment): void; +} + +type TokenNode = Token | string | Array; + +export class Token { + /** + * Given a token or string input, convert it to highlighted HTML + * @param type The token(s) to stringify + * @param language The language definition that understands the given token(s) + * @param parent The parent element element to pass into hook callbacks + * @returns The HTML that represents the given token(s) + */ + static stringify(token: TokenNode, language: LanguageDefinition, parent: HTMLPreElement): string; + + /** + * The constructor for the Prism Token class + * @param type The type of the token + * @param content The contents of the token + * @param alias Aliases for the current token (added as classes to this token's HTML) + * @param matchedStr The matched string that generated this token + * @param greedy Value that determines whether or not this token was generated using a greedy parsing algorithm + */ + constructor(type: string, content: TokenNode, alias: any, matchedStr: string, greedy: boolean); + + /** The type of the token */ + type: string; + + /** The content of the token */ + content: TokenNode; + + /** Other names for this token (added as classes to this token's HTML) */ + alias: string; +} diff --git a/prismjs/prismjs-module-tests.ts b/prismjs/prismjs-module-tests.ts new file mode 100644 index 0000000000..3c48e476c5 --- /dev/null +++ b/prismjs/prismjs-module-tests.ts @@ -0,0 +1,26 @@ +import * as Prism from "prismjs"; + +var element = document.createElement("code"); +var callback = (element: Element) => console.log(element); + +Prism.highlightElement(element, false, callback); +Prism.highlightElement(element, false); +Prism.highlightAll(true, callback); +Prism.highlightAll(true); + +const hookCallback: Prism.HookCallback = env => null; +Prism.hooks.add("before-highlightall", hookCallback); +Prism.hooks.add("future-hook", hookCallback); + +var language = "js"; +var tokens = Prism.tokenize("var n = 1;", Prism.languages[language]); +(function visit(token: Prism.TokenNode): Prism.TokenNode { + if (typeof token === "string") { + return token; + } else if (Array.isArray(token)) { + return token.map(visit) as Prism.TokenNode; + } else { + token.alias += "visited"; + return token; + } +})(tokens); diff --git a/prismjs/prismjs-tests.ts b/prismjs/prismjs-tests.ts index 6d87b9b217..feadde4c51 100644 --- a/prismjs/prismjs-tests.ts +++ b/prismjs/prismjs-tests.ts @@ -1,5 +1,3 @@ - - var element = document.createElement("code"); var callback = (element: Element) => console.log(element); @@ -8,3 +6,19 @@ Prism.highlightElement(element, false); Prism.highlightAll(true, callback); Prism.highlightAll(true); +const hookCallback: PrismJS.HookCallback = env => null; +Prism.hooks.add("before-highlightall", hookCallback); +Prism.hooks.add("future-hook", hookCallback); + +var language = "js"; +var tokens = Prism.tokenize("var n = 1;", Prism.languages[language]); +(function visit(token: PrismJS.TokenNode): PrismJS.TokenNode { + if (typeof token === "string") { + return token; + } else if (Array.isArray(token)) { + return token.map(visit) as PrismJS.TokenNode; + } else { + token.alias += "visited"; + return token; + } +})(tokens); diff --git a/prismjs/tsconfig.json b/prismjs/tsconfig.json index b01a635b30..5a528ed4a4 100644 --- a/prismjs/tsconfig.json +++ b/prismjs/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -18,6 +18,7 @@ }, "files": [ "index.d.ts", - "prismjs-tests.ts" + "prismjs-tests.ts", + "prismjs-module-tests.ts" ] -} \ No newline at end of file +} diff --git a/prismjs/tslint.json b/prismjs/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/prismjs/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }