Instant code highlighting, auto-detect language, super easy to use
Table of Contents
Install with NPM
npm i ngx-highlightjs
HighlightModule in your app
Note: By default this will lazy-load highlight.js bundle library including all languages.
To avoid import everything from highlight.js library, you should import each language you want to highlight manually.
Import highlighting languages
To do so, use the injection token
HIGHLIGHT_OPTIONS to provide options:
;/*** Import specific languages to avoid importing everything* The following will lazy load highlight.js core script (~9.6KB) + the selected languages bundle (each lang. ~1kb)*/
- languages: The set of languages to register.
- lineNumber: Lazy-load lines numbers library which adds line numbers to the highlighted code element.
- config: Set highlight.js config, see configure-options.
Import highlighting theme
Import highlight.js theme from the node_modules directory in
"styles": [ "styles.css", "../node_modules/highlight.js/styles/github.css", ]
Or import it in
Use highlight directive
The following line will highlight the given code and append it to the host element
[highlight]: (string), Accept code string to highlight, default
[languages]: (string), An array of language names and aliases restricting auto detection to only these languages, default:
[lineNumbers]: (boolean), A flag that indicates adding line numbers to highlighted code element
(highlighted): Stream that emits
HighlightResultobject when element is highlighted.
In version >= 4, a new sub-package were added with the following features:
- Highlight gists using gists API
- Highlight code directly from URL
Highlight a gist file
[gist]directive with the gist id to get the response through the output
(gistLoaded)emits, you will get access to the gist response.
gistContentpipe to extract the file content from gist response using gist file name.
Highlight all gist files
To loop over
keyvalue pipe to pass file name into
Highlight code from URL directly
Use the pipe
codeFromUrl with the
async pipe together to get the code text from a raw URL.
This project uses Angular CLI to build the package.
$ ng build ngx-highlightjs
If you identify any errors in the library, or have an idea for an improvement, please open an issue.