Code Monkey home page Code Monkey logo

ngx-highlightjs's Introduction

Angular Highlight.js

npm npm Build Status npm

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


Table of Contents

  1. Install it with npm
$ 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/lib/hljs and extract the downloaded zip file there.

Import HighlightModule library from any module:

import { HighlightModule } from 'ngx-highlightjs';

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

The function forRoot accepts 1 optional parameters, forRoot(options?: HighlightOptions)

With options parameter you can set:

  • theme: select the theme, use theme's name without the extension, default: 'github'
  • path: hljs library location, default: 'assets/lib/hljs'
  • auto: Enable observer mutation to auto-highlight on text changes, default: true
  • config: Configures global options, see configure-options, default: null.

Choose highlighting theme:

HighlightModule.forRoot({ theme: 'agate'});

List of all available themes from highlight.js

Import highlight.js library from a custom path

const options: HighlightOptions = {
 theme: 'agate',
 path: 'assets/js/highlight-js'
};

HighlightModule.forRoot(options);

Now you can use the directive highlight, you can:

  • Highlight a code element
<!-- Highlight directly -->
<pre><code highlight [code]="someCode"></code></pre>
<!-- Or -->
<pre><code highlight [textContent]="someCode"></code></pre>

Check this stackblitz

  • Highlight all child code elements
<!-- Highlight child elements with 'pre code' selector -->
<div highlight="all">
  <pre><code [textContent]="htmlCode"></code></pre>
  <pre><code [textContent]="tsCode"></code></pre>
  <pre><code [textContent]="cssCode"></code></pre>
</div>

Check this stackblitz

  • Highlight custom elements
<!-- Highlight child elements with custom selector -->
<div highlight="section code">
  <section><code [textContent]="pythonCode"></code></section>
  <section><code [textContent]="swiftCode"></code></section>
</div>

Options

  • [highlight]: (string), default null

    • Use just highlight without a value to highlight the element.
    • Use highlight="all" to highlight child elements with 'pre code' selector.
    • Use highlight="{selector}" to highlight child elements with custom selector.
  • [code]: (string), code content, default null

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

  • (highlighted): Stream that emits highlight result, you can use HighlightResult interface for the response

Use highlight.js functions (version >= 2.1.0)

If you wish to use highlight.js functions, check the official API for thw usage.

Example:

import { HighlightJS } from 'ngx-highlightjs';

export class AppComponent implements OnInit {

  constructor(private hljs: HighlightJS) {
  }

  ngOnInit() {
    this.hljs.isReady.subscribe(() => {
      console.log(this.hljs.listLanguages());
    });
  }
}

This project uses Angular CLI 6 to build the package.

$ ng build ngx-highlightjs --prod

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

Murhaf Sousli

ngx-highlightjs's People

Contributors

murhafsousli avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.