JSPM

  • Created
  • Published
  • Downloads 59395
  • Score
    100M100P100Q156987F
  • License MIT

Angular Highlight.js

Package Exports

  • ngx-highlightjs

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ngx-highlightjs) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Angular Highlight.js

Super easy to use, auto highlight code and auto-detect language


npm npm Build Status npm

Installation

  1. To install this library, run:
$ npm install --save ngx-highlightjs
  1. Head to highlight.js download page and get your custom package bundle including only the languages you need.

  2. Create new folder in src/assets/highlight.js and extract the downloaded zip file.

  3. import highlight.js script in index.html

<script src="assets/highlight.js/highlight.pack.js"></script>
  1. import highlight.js theme in global style style.scss
@import 'assets/highlight.js/styles/monokai-sublime.css';

List of all available themes from highlight.js

SystemJS

If you are using SystemJS, you should also adjust your configuration to point to the UMD bundle.

In your systemjs config file, map needs to tell the System loader where to look for ngx-highlightjs:

map: {
  'ngx-highlightjs': 'node_modules/ngx-highlightjs/bundles/ngx-highlightjs.umd.js',
}

Here is a working plunker.

Usage

Import HighlightModule library from any module:

import { HighlightModule } from 'ngx-highlightjs';

@NgModule({
  imports: [
    // ...
    HighlightModule
  ]
})
export class AppModule { }
  • Highlight a code element
<!-- Highlight target element -->
<pre><code highlight [textContent]="someCode"></code></pre>
<div [textContent]="anotherCode"></div>
  • Highlight all code elements
<!-- Highlight child elements of type <pre><code> -->
<div highlight="all">
  <pre><code [textContent]="htmlCode"></code></pre>
  <pre><code [textContent]="tsCode"></code></pre>
  <pre><code [textContent]="cssCode"></code></pre>
</div>
  • Highlight custom elements
<!-- Highlight custom child elements -->
<div highlight="section code">
  <section><code [textContent]="pythonCode"></code></section>
  <section><code [textContent]="swiftCode"></code></section>
</div>

Options

  • Highlight element(s)

[highlight]: string, default ''

if you just use highlight on the element, it will highlight it if you use highlight="all": highlights all child code elements. if you use highlight="p code": highlights custom child elements.

  • Auto-highlight on changes

[hlAuto]: boolean, default true;

  • Highlight delay

[hlDelay]: number, default 200 ms;

Development

To generate all *.js, *.d.ts and *.metadata.json files:

$ npm run build

To lint all *.ts files:

$ npm run lint

Issues

If you identify any errors in the library, or have an idea for an improvement, please open an issue. I am excited to see what the community thinks of this project, and I would love your input!

Author

Murhaf Sousli