All Projects → MurhafSousli → Ngx Highlightjs

MurhafSousli / Ngx Highlightjs

Licence: mit
Angular syntax highlighting module

Projects that are alternatives of or similar to Ngx Highlightjs

Angular Truffle Starter Dapp
Angular CLI + Truffle Starter Dapp; write, compile & deploy smart contracts on Ethereum blockchains
Stars: ✭ 174 (-6.95%)
Mutual labels:  angular2, angular4
Ngx Popper
An angular wrapper for popper.js, great for tooltips and positioning popping elements
Stars: ✭ 183 (-2.14%)
Mutual labels:  angular2, angular4
Ng2 Pdfjs Viewer
An angular 8 component for PDFJS and ViewerJS (Supports angular 2/4/5/6/7)
Stars: ✭ 150 (-19.79%)
Mutual labels:  angular2, angular4
Ionic2 Pokedex
🎮 Pokédex sample app developed with Ionic 2, Angular 2 and Apache Cordova. Using Pokéapi as source for data.
Stars: ✭ 143 (-23.53%)
Mutual labels:  angular2, angular4
Angular Google Maps
Angular 2+ Google Maps Components
Stars: ✭ 1,982 (+959.89%)
Mutual labels:  angular2, angular4
Angular5 Iot Dashboard
Multipurpose dashboard admin for IoT softwares, remote control, user interface. Develop your client dashboards in Angular 5 with vast variety of components available.
Stars: ✭ 148 (-20.86%)
Mutual labels:  angular2, angular4
Ngx Gauge
A highly customizable Gauge component for Angular 9+ apps and dashboards
Stars: ✭ 158 (-15.51%)
Mutual labels:  angular2, angular4
Ngx Config
Configuration utility for Angular
Stars: ✭ 135 (-27.81%)
Mutual labels:  angular2, angular4
Ngx Socket Io
Socket.IO module for Angular
Stars: ✭ 178 (-4.81%)
Mutual labels:  angular2, angular4
Ngx Qrcode
An Angular 9/10 Component Library for Generating QR (Quick Response) Codes
Stars: ✭ 161 (-13.9%)
Mutual labels:  angular2, angular4
Amazing Time Picker
Timepicker (Clock Picker) for Angular 2, Angular 4 and Angular 5, Angular 6, Angular 7 - Compatible with Angular Material
Stars: ✭ 142 (-24.06%)
Mutual labels:  angular2, angular4
Ngx Inline Editor
Native UI Inline-editor Angular (4.0+) component
Stars: ✭ 172 (-8.02%)
Mutual labels:  angular2, angular4
Ionic3 Components
A project full of ionic 3 components and samples - to make life easier :)
Stars: ✭ 1,689 (+803.21%)
Mutual labels:  angular2, angular4
Angular Notifier
A well designed, fully animated, highly customizable, and easy-to-use notification library for your Angular application.
Stars: ✭ 175 (-6.42%)
Mutual labels:  angular2, angular4
Ng2 Search Filter
Angular 2 / Angular 4 / Angular 5 custom pipe npm module to make a search filter on any input, 🔥 100K+ downloads
Stars: ✭ 137 (-26.74%)
Mutual labels:  angular2, angular4
Angular2 Crud Rest
Sample Angular (2.x and 4.x) app: CRUD example + routing
Stars: ✭ 152 (-18.72%)
Mutual labels:  angular2, angular4
Webapiclientgen
Strongly Typed Client API Generators generate strongly typed client APIs in C# .NET and in TypeScript for jQuery and Angular 2+ from ASP.NET Web API and .NET Core Web API
Stars: ✭ 134 (-28.34%)
Mutual labels:  angular2, angular4
Coreui Free Bootstrap Admin Template
CoreUI is free bootstrap admin template
Stars: ✭ 11,038 (+5802.67%)
Mutual labels:  angular2, angular4
Angular Development With Primeng
Code samples from the book "Angular UI Development with PrimeNG"
Stars: ✭ 159 (-14.97%)
Mutual labels:  angular2, angular4
Ngx Daterangepicker Material
Pure Angular 2+ date range picker with material design theme, a demo here:
Stars: ✭ 169 (-9.63%)
Mutual labels:  angular2, angular4

Angular Highlight.js

Demo Stackblitz npm tests 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, HIGHLIGHT_OPTIONS } from 'ngx-highlightjs';

@NgModule({
  imports: [
    HighlightModule
  ],
  providers: [
    {
      provide: HIGHLIGHT_OPTIONS,
      useValue: {
        fullLibraryLoader: () => import('highlight.js'),
      }
    }
  ],
})
export class AppModule { }

Note: This will add highlight.js library including all languages to your bundle.

To avoid import everything from highlight.js library, you should import each language you want to highlight manually.

Import only the core library and the needed highlighting languages

import { HighlightModule, HIGHLIGHT_OPTIONS } from 'ngx-highlightjs';
 
@NgModule({
  imports: [
    HighlightModule
  ],
  providers: [
    {
      provide: HIGHLIGHT_OPTIONS,
      useValue: {
        coreLibraryLoader: () => import('highlight.js/lib/core'),
        lineNumbersLoader: () => import('highlightjs-line-numbers.js'), // Optional, only if you want the line numbers
        languages: {
          typescript: () => import('highlight.js/lib/languages/typescript'),
          css: () => import('highlight.js/lib/languages/css'),
          xml: () => import('highlight.js/lib/languages/xml')
        }
      }
    }
  ],
})
export class AppModule { }

HighlightOptions API

Name Description
fullLibraryLoader A function that returns a promise that loads highlight.js full script
coreLibraryLoader A function that returns a promise that loads highlight.js core script
lineNumbersLoader A function that returns a promise that loads line-numbers script which adds line numbers to the highlight code
languages The set of languages to register.
config Set highlight.js config, see configure-options

NOTE: Since the update of [email protected], should use coreLibraryLoader: () => import('highlight.js/lib/core') instead of coreLibraryLoader: () => import('highlight.js/lib/highlight')

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

Name Type Description
[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) HighlightResult Stream that emits the result object when element is highlighted

NOTE

In Angular 10, when building your project, you might get a warning WARNING in ... CommonJS or AMD dependencies can cause optimization bailouts.

To avoid this warning, add the following in your angular.json

{
  "projects": {
    "project-name": {
      "architect": {
        "build": {
          "options": {
            "allowedCommonJsDependencies": [
              "highlight.js"
            ]
          }
        }
      }
    }
  }
}

Read more about CommonJS dependencies configuration

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].