Core API

Highlight.js exports a few functions as methods of thehljs object.

highlight

highlight(code,{language,ignoreIllegals})

Core highlighting function. Accepts the code to highlight (string) and a list of options (object).Thelanguage parameter must be present and specify the language name or aliasof the grammar to be used for highlighting.TheignoreIllegals is an optional parameter that whentrue forces highlightingto finish even in case of detecting illegal syntax for thelanguage instead of throwing an exception.

Returns an object with the following properties:

  • language: language name, same as the name passed inlanguageName, returned for consistency withhighlightAuto

  • relevance: integer value representing the relevance score

  • value: HTML string with highlighting markup

  • top: top of the current mode stack

  • illegal: boolean representing whether any illegal matches were found

  • code: the original raw code

highlightAuto

highlightAuto(code,languageSubset)

Highlighting with language detection.Accepts a string with the code to highlight and an optional array of language names and aliases restricting detection to only those languages. The subset can also be set withconfigure, but the local parameter overrides the option if set.

Returns an object with the following properties:

  • language: detected language

  • relevance: integer value representing the relevance score

  • value: HTML string with highlighting markup

  • secondBest: object with the same structure for second-best heuristically detected language (may be absent)

highlightElement

highlightElement(element)

Applies highlighting to a DOM node containing code.

This function is the one to use to apply highlighting dynamically after page loador within initialization code of third-party JavaScript frameworks.

The function uses language detection by default but you can specify the languagein theclass attribute of the DOM node. See thescopes reference for all available language names and scopes.

highlightAll

Applies highlighting to all elements on a page matching the configuredcssSelector.The defaultcssSelector value is'precode', which highlights all code blocks.This can be called before or after the page’sonload event has fired.

newInstance

Returns a new instance of the highlighter with default configuration.

configure

configure(options)

Configures global options:

  • classPrefix: a string prefix added before class names in the generated markup, used for backwards compatibility with stylesheets.

  • languages: an array of language names and aliases restricting auto detection to only these languages.

  • languageDetectRe: a regex to configure how CSS class names map to language (allows class names like saycolor-as-php vs the default oflanguage-php, etc.)

  • noHighlightRe: a regex to configure which CSS classes are to be skipped completely.

  • cssSelector: a CSS selector to configure which elements are affected byhljs.highlightAll. Defaults to'precode'.

  • ignoreUnescapedHTML: do not log warnings to console about unescaped HTML in code blocks

  • throwUnescapedHTML: throw aHTMLInjectionError whenhighlightElement is asked to highlight content that includes unescaped HTML

Accepts an object representing options with the values to updated. Other options don’t change

hljs.configure({  noHighlightRe: /^do-not-highlightme$/i,  languageDetectRe: /\bgrammar-([\w-]+)\b/i, // for `grammar-swift` style CSS naming  classPrefix: ''     // don't append class prefix                      // … other options aren't changed});

registerLanguage

registerLanguage(languageName,languageDefinition)

Adds new language to the library under the specified name. Used mostly internally.

  • languageName: a string with the name of the language being registered

  • languageDefinition: a function that returns an object which represents thelanguage definition. The function is passed thehljs object to be ableto use common regular expressions defined within it.

unregisterLanguage

unregisterLanguage(languageName)

Removes a language and its aliases from the library. Used mostly internally.

  • languageName: a string with the name of the language being removed.

registerAliases

registerAliases(alias|aliases,{languageName})

Adds new language alias or aliases to the library for the specified language name defined underlanguageName key.

  • alias|aliases: a string or array with the name of alias being registered

  • languageName: the language name as specified byregisterLanguage.

listLanguages

Returns the languages names list.

getLanguage

getLanguage(name)

Looks up a language by name or alias.

Returns the language object if found,undefined otherwise.

versionString

versionString

Returns the full Highlight.js version as a string, ie:"11.0.1".

safeMode

safeMode()

Enables safe mode. This is the default mode, providing the most reliable experience for production usage.

debugMode

debugMode()

Enablesdebug/development mode.

Warning

This mode purposely makes Highlight.js more fragile! It should only be used for testing and local development (of languages or the library itself).

For example, if a new version suddenly had a serious bug (or breaking change) that affected only a single language:

  • In Safe Mode all other languages would continue to highlight just fine. The broken language would appear as a code block, but without any highlighting (as if it were plaintext).

  • In Debug Mode all highlighting would stop and a JavaScript error would be thrown.

addPlugin

addPlugin(plugin)

Add a plugin to this instance of Highlight.js. See theplugin api for additional plugin information.

removePlugin

removePlugin(plugin)

Remove the specified plugin from this instance.plugin must be exactly the same object that was passed toaddPlugin.

Deprecated API

highlight

Deprecated since version 10.7:This will be removed entirely in v12.

highlight(languageName,code)

Please see thenewer API shown above.

highlightBlock

Deprecated since version 11.0:Please usehighlightElement() instead.

initHighlighting

Deprecated since version 10.6:Please usehighlightAll() instead.

initHighlightingOnLoad

Deprecated since version 10.6:Please usehighlightAll() instead.