Highlight.js
highlight.js for Nim
Types
HighlightJs = JsObject
HighlightResult = object of JsObject language*: cstring relevance*: int value*: cstring code*: cstring illegal*: bool
Vars
hljs {.importc, nodecl.}: HighlightJs
Procs
proc configure(self: HighlightJs; classPrefix: cstring = ""; cssSelector: cstring = "pre code"; ignoreUnescapedHTML: bool = true; throwUnescapedHTML: bool = false) {.importjs: """#.configure({ classPrefix: #, cssSelector: #, ignoreUnescapedHTML: #, throwUnescapedHTML: #, })""", ...raises: [], tags: [], forbids: [].}
-
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 say color-as-php vs the default of language-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 by hljs.highlightAll. Defaults to 'pre code'.
- ignoreUnescapedHTML: do not log warnings to console about unescaped HTML in code blocks
- throwUnescapedHTML: throw a HTMLInjectionError when highlightElement 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( classPrefix = "" )
proc debugMode(self: HighlightJs) {.importjs: "#.debugMode()", ...raises: [], tags: [], forbids: [].}
-
Enables debug/development mode.
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.
proc highlight(self: HighlightJs; code: cstring; language: cstring = "nim"; ignoreIllegals: bool = false): HighlightResult {.importjs: """#.highlight(#, { language: #, ignoreIllegals: # })""", ...raises: [], tags: [], forbids: [].}
-
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:
- language: language name, same as the name passed in languageName, returned for consistency with highlightAuto
- 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
proc highlightAll(self: HighlightJs) {.importjs: "#.highlightAll()", ...raises: [], tags: [], forbids: [].}
- 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.
proc highlightElement(self: HighlightJs; elem: Element) {. importjs: "#.highlightElement(#)", ...raises: [], tags: [], forbids: [].}
- 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.
proc newInstance(self: HighlightJs): HighlightJs {.importjs: "#.newInstance()", ...raises: [], tags: [], forbids: [].}
- Returns a new instance of the highlighter with default configuration.