Usage
Use provideHighlightOptions
to provide highlight.js options in app.config.ts
import { provideHighlightOptions } from 'ngx-highlightjs';
export const appConfig: ApplicationConfig = {
providers: [
provideHighlightOptions({
fullLibraryLoader: () => import('highlight.js')
})
]
};
Note: This includes the entire Highlight.js library with all languages.
You can also opt to load only the core script and the necessary languages.
Importing Core Library and Languages
import { provideHighlightOptions } from 'ngx-highlightjs';
export const appConfig: ApplicationConfig = {
providers: [
provideHighlightOptions({
coreLibraryLoader: () => import('highlight.js/lib/core'),
lineNumbersLoader: () => import('ngx-highlightjs/line-numbers'),
languages: {
typescript: () => import('highlight.js/lib/languages/typescript'),
css: () => import('highlight.js/lib/languages/css'),
xml: () => import('highlight.js/lib/languages/xml')
},
themePath: 'path-to-theme.css'
})
]
};
HighlightOptions API
Import highlighting theme
Dynamic Approach
Set the theme path in the global configuration to enable dynamic theme changes:
providers: [
{
provide: HIGHLIGHT_OPTIONS,
useValue: {
themePath: 'assets/styles/solarized-dark.css'
}
}
]
Alternatively, import the theme from the app's distribution folder or use a CDN link.
When switching between app themes, call the setTheme(path)
method from the HighlightLoader
service.
import { HighlightLoader } from 'ngx-highlightjs';
export class AppComponent {
private hljsLoader: HighlightLoader = inject(HighlightLoader);
onAppThemeChange(appTheme: 'dark' | 'light') {
this.hljsLoader.setTheme(appTheme === 'dark' ? 'assets/styles/solarized-dark.css' : 'assets/styles/solarized-light.css');
}
}
Traditional Approach
In angular.json
:
"styles": [
"styles.css",
"../node_modules/highlight.js/styles/github.css",
]
Or directly in src/style.scss
:
@import 'highlight.js/styles/github.css';
Highlight Directive Usage
To apply code highlighting, use the highlight
directive. It requires setting the target language, with an optional feature to ignore illegal syntax.
import { Highlight } from 'ngx-highlightjs';
@Component({
selector: 'app-root',
template: `
<pre><code [highlight]="code" language="html"></code></pre>
`,
imports: [Highlight]
})
export class AppComponent {
}
Options
Line Numbers Directive Usage
The lineNumbers
directive extends highlighted code with line numbers. It functions in conjunction with the highlight
and highlightAuto
directives.
import { HighlightAuto } from 'ngx-highlightjs';
import { HighlightLineNumbers } from 'ngx-highlightjs/line-numbers';
@Component({
selector: 'app-root',
template: `
<pre><code [highlightAuto]="code" lineNumbers></code></pre>
`,
imports: [HighlightAuto, HighlightLineNumbers]
})
export class AppComponent {
}
Options
To address this warning, include the following configuration in your angular.json file:
Plus package
This package provides the following features:
- Utilizes the gists API to highlight code snippets directly from GitHub gists.
- Supports direct code highlighting from URLs.
Usage
To integrate this addon into your project, ensure the presence of HttpClient
by importing it into your main.ts
file.
import { provideHttpClient } from '@angular/common/http';
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient()
]
};
Highlight a gist file
- Use
[gist]
directive, passing the gist ID as its attribute, to retrieve the response through the (gistLoaded)
output event. - Upon the emission of
(gistLoaded)
, gain access to the gist response. - Use
gistContent
pipe to extract the file's content from the gist response based on the specified file name.
Example:
import { HighlightPlusModule } from 'ngx-highlightjs';
@Component({
selector: 'app-root',
template: `
<pre [gist]="gistId" (gistLoaded)="gist = $event">
<code [highlight]="gist | gistContent: 'main.js'"></code>
</pre>
`,
imports: [HighlightPlusModule]
})
export class AppComponent {
}
Highlight all gist files
To loop over gist?.files
, use keyvalue
pipe to pass file name into gistContent
pipe.
To highlight all files within a gist, iterate through gist.files
and utilize the keyvalue
pipe to pass the file name into the gistContent
pipe.
Example:
import { HighlightPlusModule } from 'ngx-highlightjs';
@Component({
selector: 'app-root',
template: `
<ng-container [gist]="gistId" (gistLoaded)="gist = $event">
@for (file of gist?.files | keyvalue; track file.key) {
<pre><code [highlight]="gist | gistContent: file.key"></code></pre>
}
</ng-container>
`,
imports: [HighlightPlusModule, CommonModule]
})
export class AppComponent {
}
Highlight code from URL directly
Use the pipe codeFromUrl
with the async
pipe to get the code text from a raw URL.
Example:
import { HighlightPlusModule } from 'ngx-highlightjs';
@Component({
selector: 'app-root',
template: `
<pre>
<code [highlight]="codeUrl | codeFromUrl | async"></code>
</pre>
`,
imports: [HighlightPlusModule, CommonModule]
})
export class AppComponent {
}
Providing Gist API secret (Optional)
The package offers the provideHighlightOptions
function, allowing you to set your clientId
and clientSecret
for the gist HTTP requests.
You can provide these options in your app.config.ts
file as demonstrated below:
import { provideHttpClient } from '@angular/common/http';
import { provideHighlightOptions } from 'ngx-highlightjs/plus'
export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(),
provideGistOptions({
clientId: 'CLIENT_ID',
clientSecret: 'CLIENT_SECRET'
})
]
};