@types/prismjs
- Version 1.26.4
- Published
- 19.2 kB
- No dependencies
- MIT license
Install
npm i @types/prismjs
yarn add @types/prismjs
pnpm add @types/prismjs
Overview
TypeScript definitions for prismjs
Index
Variables
Functions
Classes
Interfaces
Type Aliases
Namespaces
Variables
variable disableWorkerMessageHandler
let disableWorkerMessageHandler: boolean;
By default, if Prism is in a web worker, it assumes that it is in a worker it created itself, so it uses
addEventListener
to communicate with its parent instance. However, if you're using Prism manually in your own worker, you don't want it to do this.By setting this value to
true
, Prism will not add its own listeners to the worker.You obviously have to change this value before Prism executes. To do this, you can add an empty Prism object into the global scope before loading the Prism script like this:
false
variable languages
const languages: Languages;
variable manual
let manual: boolean;
By default, Prism will attempt to highlight all code elements (by calling Prism.highlightAll) on the current page after the page finished loading. This might be a problem if e.g. you wanted to asynchronously load additional languages or plugins yourself.
By setting this value to
true
, Prism will not automatically highlight all code elements on the page.You obviously have to change this value before the automatic highlighting started. To do this, you can add an empty Prism object into the global scope before loading the Prism script like this:
false
variable plugins
const plugins: Record<string, any>;
Functions
function highlight
highlight: (text: string, grammar: Grammar, language: string) => 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.
The following hooks will be run: 1.
before-tokenize
2.after-tokenize
3.wrap
: On each Prism.Token.Parameter text
A string with the code to be highlighted.
Parameter grammar
An object containing the tokens to use.
Usually a language definition like
Prism.languages.markup
.Parameter language
The name of the language definition passed to
grammar
.Returns
The highlighted HTML.
Example 1
Prism.highlight('var foo = true;', Prism.languages.js, 'js');
function highlightAll
highlightAll: (async?: boolean, callback?: HighlightCallback) => 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.This is equivalent to
Prism.highlightAllUnder(document, async, callback)
.Parameter async
Same as in Prism.highlightAllUnder.
Parameter callback
Same as in Prism.highlightAllUnder.
function highlightAllUnder
highlightAllUnder: ( container: ParentNode, async?: boolean, callback?: HighlightCallback) => void;
Fetches all the descendants of
container
that have a.language-xxxx
class and then calls Prism.highlightElement on each one of them.The following hooks will be run: 1.
before-highlightall
2. All hooks of Prism.highlightElement for each element.Parameter container
The root element, whose descendants that have a
.language-xxxx
class will be highlighted.Parameter async
Whether each element is to be highlighted asynchronously using Web Workers.
Parameter callback
An optional callback to be invoked on each element after its highlighting is done.
function highlightElement
highlightElement: ( element: Element, async?: boolean, callback?: HighlightCallback) => void;
Highlights the code inside a single element.
The following hooks will be run: 1.
before-sanity-check
2.before-highlight
3. All hooks of Prism.highlightElement. These hooks will only be run by the current worker ifasync
istrue
. 4.before-insert
5.after-highlight
6.complete
Parameter element
The element containing the code. It must have a class of
language-xxxx
to be processed, wherexxxx
is a valid language identifier.Parameter async
Whether the element is to be highlighted asynchronously using Web Workers to improve performance and avoid blocking the UI when highlighting very large chunks of code. This option is [disabled by default](https://prismjs.com/faq.html#why-is-asynchronous-highlighting-disabled-by-default).
Note: All language definitions required to highlight the code must be included in the main
prism.js
file for asynchronous highlighting to work. You can build your own bundle on the [Download page](https://prismjs.com/download.html).Parameter callback
An optional callback to be invoked after the highlighting is done. Mostly useful when
async
istrue
, since in that case, the highlighting is done asynchronously.
function tokenize
tokenize: (text: string, grammar: Grammar) => Array<string | Token>;
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.
Parameter text
A string with the code to be highlighted.
Parameter grammar
An object containing the tokens to use.
Usually a language definition like
Prism.languages.markup
.Returns
An array of strings, tokens and other arrays.
Classes
class Token
class Token {}
constructor
constructor( type: string, content: TokenStream, alias?: string | string[], matchedStr?: string, greedy?: boolean);
Creates a new token.
Parameter type
See
Parameter content
See
Parameter alias
The alias(es) of the token.
Parameter matchedStr
A copy of the full string this token was created from.
Parameter greedy
See
property alias
alias: string | string[];
The alias(es) of the token.
See Also
TokenObject
property content
content: TokenStream;
The strings or tokens contained by this token.
This will be a token stream if the pattern matched also defined an
inside
grammar.
property greedy
greedy: boolean;
Whether the pattern that created this token is greedy or not.
See Also
TokenObject
property length
length: number;
The length of the matched string or 0.
property type
type: string;
The type of the token.
This is usually the key of a pattern in a Grammar.
method stringify
static stringify: ( token: TokenStream, language: string, parent?: Array<string | Token>) => string;
Converts the given token or token stream to an HTML representation.
The following hooks will be run: 1.
wrap
: On each Prism.Token.Parameter token
The token or token stream to be converted.
Parameter language
The name of current language.
Parameter parent
The parent token stream, if any. The HTML representation of the token or token stream.
Interfaces
interface Environment
interface Environment extends Record<string, any> {}
property attributes
attributes?: Record<string, string> | undefined;
property classes
classes?: string[] | undefined;
property code
code?: string | undefined;
property content
content?: string | undefined;
property element
element?: Element | undefined;
property grammar
grammar?: Grammar | undefined;
property highlightedCode
highlightedCode?: string | undefined;
property language
language?: string | undefined;
property parent
parent?: Array<string | Token> | undefined;
property selector
selector?: string | undefined;
property tag
tag?: string | undefined;
property type
type?: string | undefined;
interface GrammarRest
interface GrammarRest {}
property "class-name"
'class-name'?: GrammarValue | undefined;
property atrule
atrule?: GrammarValue | undefined;
property boolean
boolean?: GrammarValue | undefined;
property comment
comment?: GrammarValue | undefined;
property function
function?: GrammarValue | undefined;
property important
important?: GrammarValue | undefined;
property keyword
keyword?: GrammarValue | undefined;
property number
number?: GrammarValue | undefined;
property operator
operator?: GrammarValue | undefined;
property property
property?: GrammarValue | undefined;
property punctuation
punctuation?: GrammarValue | undefined;
property rest
rest?: Grammar | undefined;
An optional grammar object that will appended to this grammar.
property selector
selector?: GrammarValue | undefined;
property string
string?: GrammarValue | undefined;
property style
style?: GrammarValue | undefined;
property url
url?: GrammarValue | undefined;
interface LanguageMap
interface LanguageMap {}
index signature
[language: string]: Grammar;
Get a defined language's definition.
interface LanguageMapProtocol
interface LanguageMapProtocol {}
method extend
extend: (id: string, redef: Grammar) => Grammar;
Creates a deep copy of the language with the given id and appends the given tokens.
If a token in
redef
also appears in the copied language, then the existing token in the copied language will be overwritten at its original position.Parameter id
The id of the language to extend. This has to be a key in
Prism.languages
.Parameter redef
The new tokens to append.
Returns
The new language created.
Example 1
Prism.languages['css-with-colors'] = Prism.languages.extend('css', { 'color': /\b(?:red|green|blue)\b/ });
method insertBefore
insertBefore: ( inside: string, before: string, insert: Grammar, root?: LanguageMap) => Grammar;
Inserts tokens _before_ another token in a language definition or any other grammar.
As this needs to recreate the object (we cannot actually insert before keys in object literals), we cannot just provide an object, we need an object and a key.
If the grammar of
inside
andinsert
have tokens with the same name, the tokens ininside
will be ignored.All references of the old object accessible from
Prism.languages
orinsert
will be replace with the new one.Parameter inside
The property of
root
that contains the object to be modified.This is usually a language id.
Parameter before
The key to insert before.
Parameter insert
An object containing the key-value pairs to be inserted.
Parameter root
The object containing
inside
, i.e. the object that contains the object that will be modified.Defaults to
Prism.languages
.Returns
The new grammar created.
Example 1
Prism.languages.insertBefore('markup', 'cdata', { 'style': { ... } });
interface TokenObject
interface TokenObject {}
The expansion of a simple
RegExp
literal to support additional properties.
property alias
alias?: string | string[] | undefined;
An optional alias or list of aliases.
property greedy
greedy?: boolean | undefined;
Whether the token is greedy.
false
property inside
inside?: Grammar | undefined;
The nested tokens of this token.
This can be used for recursive language definitions.
Note that this can cause infinite recursion.
property lookbehind
lookbehind?: boolean | undefined;
If
true
, then the first capturing group ofpattern
will (effectively) behave as a lookbehind group meaning that the captured text will not be part of the matched text of the new token.
property pattern
pattern: RegExp;
The regular expression of the token.
Type Aliases
type Grammar
type Grammar = GrammarRest | Record<string, GrammarValue>;
type GrammarValue
type GrammarValue = RegExp | TokenObject | Array<RegExp | TokenObject>;
type HighlightCallback
type HighlightCallback = (element: Element) => void;
A function which will be invoked after an element was successfully highlighted.
Parameter element
The element successfully highlighted.
type Languages
type Languages = LanguageMapProtocol & LanguageMap;
type TokenStream
type TokenStream = string | Token | Array<string | Token>;
Namespaces
namespace hooks
namespace hooks {}
variable all
const all: RegisteredHooks;
function add
add: { <K extends keyof HookEnvironmentMap>( name: K, callback: (env: HookEnvironmentMap[K]) => void ): void; (name: string, callback: HookCallback): void;};
Adds the given callback to the list of callbacks for the given hook.
The callback will be invoked when the hook it is registered for is run. Hooks are usually directly run by a highlight function but you can also run hooks yourself.
One callback function can be registered to multiple hooks and the same hook multiple times.
Parameter name
The name of the hook.
Parameter callback
The callback function which is given environment variables.
function run
run: { <K extends keyof HookEnvironmentMap>(name: K, env: HookEnvironmentMap[K]): void; (name: string, env: Environment): void;};
Runs a hook invoking all registered callbacks with the given environment variables.
Callbacks will be invoked synchronously and in the order in which they were registered.
Parameter name
The name of the hook.
Parameter env
The environment variables of the hook passed to all callbacks registered.
interface HookEnvironmentMap
interface HookEnvironmentMap {}
property "after-highlight"
'after-highlight': ElementHighlightedEnvironment;
property "after-tokenize"
'after-tokenize': TokenizeEnvironment;
property "before-highlight"
'before-highlight': ElementEnvironment;
property "before-highlightall"
'before-highlightall': RequiredEnvironment<'selector'>;
property "before-insert"
'before-insert': ElementHighlightedEnvironment;
property "before-sanity-check"
'before-sanity-check': ElementEnvironment;
property "before-tokenize"
'before-tokenize': TokenizeEnvironment;
property complete
complete: ElementHighlightedEnvironment;
property wrap
wrap: RequiredEnvironment< 'type' | 'content' | 'tag' | 'classes' | 'attributes' | 'language'>;
interface RegisteredHooks
interface RegisteredHooks {}
index signature
[hook: string]: HookCallback[];
type ElementEnvironment
type ElementEnvironment = RequiredEnvironment< 'element' | 'language' | 'grammar' | 'code'>;
type ElementHighlightedEnvironment
type ElementHighlightedEnvironment = RequiredEnvironment< 'highlightedCode', ElementEnvironment>;
type HookCallback
type HookCallback = (env: Environment) => void;
Parameter env
The environment variables of the hook.
type HookTypes
type HookTypes = keyof HookEnvironmentMap;
type RequiredEnvironment
type RequiredEnvironment< T extends keyof Environment, U extends Environment = Environment> = U & Required<Pick<U, T>>;
type TokenizeEnvironment
type TokenizeEnvironment = RequiredEnvironment<'code' | 'grammar' | 'language'>;
namespace util
namespace util {}
function clone
clone: <T>(o: T) => T;
Deep clone a language definition (e.g. to extend it)
function encode
encode: (tokens: TokenStream) => TokenStream;
Encode raw strings in tokens in preparation to display as HTML
function objId
objId: (obj: any) => Identifier;
Get the unique id of this object or give it one if it does not have one
function type
type: { (o: null): 'Null'; (o: undefined): 'Undefined'; (o: boolean | Boolean): 'Boolean'; (o: number | Number): 'Number'; (o: string | String): 'String'; (o: Function): 'Function'; (o: RegExp): 'RegExp'; (o: any[]): 'Array'; (o: any): string;};
Determine the type of the object
interface Identifier
interface Identifier {}
property value
value: number;
Package Files (1)
Dependencies (0)
No dependencies.
Dev Dependencies (0)
No dev dependencies.
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/@types/prismjs
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/@types/prismjs)
- HTML<a href="https://www.jsdocs.io/package/@types/prismjs"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3541 ms. - Missing or incorrect documentation? Open an issue for this package.