Prism API
    Preparing search index...

    Class Prism

    Index

    Constructors

    Properties

    components: Registry = ...
    hooks: Hooks = ...
    plugins: Partial<Record<string, unknown> & KnownPlugins> = {}

    Methods

    • 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 Token.

      Parameters

      • text: string

        A string with the code to be highlighted.

      • language: string

        The name of the language definition passed to grammar.

      • Optionaloptions: HighlightOptions

        An object containing the tokens to use.

        Usually a language definition like Prism.languages.markup.

      Returns string

      The highlighted HTML.

      Prism.highlight('var foo = true;', 'javascript');
      
    • 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#highlight. These hooks will be run by an asynchronous worker if async is true.
      4. before-insert
      5. after-highlight
      6. complete

      Some the above hooks will be skipped if the element doesn’t contain any text or there is no grammar loaded for the element’s language.

      Parameters

      • 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.

      • options: HighlightElementOptions = {}

      Returns void

    • 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.

      Parameters

      • text: string

        A string with the code to be highlighted.

      • grammar: Grammar

        An object containing the tokens to use.

        Usually a language definition like Prism.languages.markup.

      Returns TokenStream

      An array of strings and tokens, a token stream.

      let code = `var foo = 0;`;
      let tokens = Prism.tokenize(code, Prism.getLanguage('javascript'));
      tokens.forEach(token => {
      if (token instanceof Token && token.type === 'number') {
      console.log(`Found numeric literal: ${token.content}`);
      }
      });