Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ngx-highlightjs

Package Overview
Dependencies
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ngx-highlightjs

Instant code highlighting, auto-detect language, super easy to use.

  • 4.1.0-beta
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

Angular Highlight.js

Demo Stackblitz npm Build Status Downloads Monthly Downloads npm bundle size (minified + gzip) License

Instant code highlighting, auto-detect language, super easy to use


Table of Contents

Installation

Install with NPM

npm i ngx-highlightjs

Usage

Import HighlightModule in your app

import { HighlightModule } from 'ngx-highlightjs';

@NgModule({
  imports: [
    HighlightModule
  ]
})
export class AppModule { }

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 { HighlightModule, HIGHLIGHT_OPTIONS } from 'ngx-highlightjs';

/**
 * 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)
 */
export function getHighlightLanguages() {
  return {
    typescript: () => import('highlight.js/lib/languages/typescript'),
    css: () => import('highlight.js/lib/languages/css'),
    xml: () => import('highlight.js/lib/languages/xml')
  };
}
 
@NgModule({
  imports: [
    HighlightModule
  ],
  providers: [
    {
      provide: HIGHLIGHT_OPTIONS,
      useValue: {
        languages: getHighlightLanguages()
      }
    }
  ],
})
export class AppModule {
}

Import highlighting theme

Import highlight.js theme from the node_modules directory in angular.json

"styles": [
  "styles.css",
  "../node_modules/highlight.js/styles/github.css",
]

Or import it in src/style.scss

@import '~highlight.js/styles/github.css';

List of all available themes from highlight.js

Use highlight directive

The following line will highlight the given code and append it to the host element

<pre><code [highlight]="code"></code></pre>

Demo stackblitz

Options

  • [highlight]: (string), Accept code string to highlight, default null

  • [languages]: (string[]), An array of language names and aliases restricting auto detection to only these languages, default: null

  • [lineNumbers]: (boolean), A flag that indicates adding line numbers to highlighted code element

  • (highlighted): Stream that emits HighlightResult object when element is highlighted.

Plus package

In version >= 4, a new sub-package were added with the following features:

  • Highlight gists using gists API
  • Highlight code directly from URL

Usage

import { HighlightPlusModule } from 'ngx-highlightjs/plus';
 
@NgModule({
  imports: [
    HighlightPlusModule
  ]
})
export class AppModule {
}

Highlight a gist file

  1. Use [gist] directive with the gist id to get the response through the output (gistLoaded).
  2. Once (gistLoaded) emits, you will get access to the gist response.
  3. Use gistContent pipe to extract the file content from gist response using gist file name.

Example:

<pre [gist]="gistId" (gistLoaded)="gist = $event">
  <code [highlight]="gist | gistContent: 'main.js'"></code>
</pre>

Highlight all gist files

To loop over gist?.files, use keyvalue pipe to pass file name into gistContent pipe.

Example:

<ng-container [gist]="gistId" (gistLoaded)="gist = $event">
  <pre *ngFor="let file of gist?.files | keyvalue">
    <code [highlight]="gist | gistContent: file.key"></code>
  </pre>
</ng-container>

Highlight code from URL directly

Use the pipe codeFromUrl with the async pipe together to get the code text from a raw URL.

Example:

<pre>
  <code [highlight]="codeUrl | codeFromUrl | async"></code>
</pre>

Development

This project uses Angular CLI to build the package.

$ ng build ngx-highlightjs

Issues

If you identify any errors in the library, or have an idea for an improvement, please open an issue.

Author

Murhaf Sousli

More plugins

Keywords

FAQs

Package last updated on 13 Mar 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc