Core API — highlight.js 11.9.0 documentation (original) (raw)

Highlight.js exports a few functions as methods of the hljs object.

highlight

highlight(code, {language, ignoreIllegals})

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

Returns an object with the following properties:

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 with configure, but the local parameter overrides the option if set.

Returns an object with the following properties:

highlightElement

highlightElement(element)

Applies highlighting to a DOM node containing code.

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

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

highlightAll

Applies highlighting to all elements on a page matching the configured cssSelector. The default cssSelector value is 'pre code', which highlights all code blocks. This can be called before or after the page’s onload event has fired.

newInstance

Returns a new instance of the highlighter with default configuration.

configure

Configures global options:

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.

unregisterLanguage

unregisterLanguage(languageName)

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

registerAliases

registerAliases(alias|aliases, {languageName})

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

listLanguages

Returns the languages names list.

getLanguage

Looks up a language by name or alias.

Returns the language object if found, undefined otherwise.

versionString

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

safeMode

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

debugMode

Enables debug/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:

addPlugin

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

removePlugin

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

Deprecated API

highlight

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

highlight(languageName, code)

Please see the newer API shown above.

highlightBlock

Deprecated since version 11.0: Please use highlightElement() instead.

initHighlighting

Deprecated since version 10.6: Please use highlightAll() instead.

initHighlightingOnLoad

Deprecated since version 10.6: Please use highlightAll() instead.