npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ngx-syntax-highlighter

v0.0.8

Published

Angular port of Highlight.js. No CDN - runtime support for all languages and syntax styles.

Downloads

31

Readme

Installing

$ npm install --save ngx-syntax-highlighter

Brief description on Highlight.js

Highlight.js will give you syntax highlighting but will not format your code. When adding syntax styling Highlight.js will auto detect the language based on the current array of languages in it's memory. This array of languages is configurable. It is not recommended you load ALL languages as there are 185 supported lanuages; however if you don't care about the binary size you can do this. Currently only one syntax style can be loaded at a single time, however you can change these on the fly.

More info here on Highlight.js

Start

Import NgxHighlightJsModule module in app.module or core.module.

import { NgxHighlightJsModule } from 'ngx-syntax-highlighter'

(...)

@NgModule({
  (...)
  imports: [
    NgxHighlightJsModule.forRoot()
  ]
  (...)
})

Or specify options

import {
  HighlightJsConfig,
  Language,
  NgxHighlightJsModule,
  SyntaxStyle
  } from 'ngx-syntax-highlighter';

const highlightJsConfig: HighlightJsConfig = {
  style: SyntaxStyle['AN-OLD-HOPE'],
  languages: [Language.TYPESCRIPT, Language.JAVASCRIPT]
  // languages: 'all'
};

@NgModule({
  (...)
  imports: [
    NgxHighlightJsModule.forRoot(highlightJsConfig)
  ]
  (...)
})

HighlightJsConfig

| property | value | description | | --------- | -------------------: | :------------------------------------------------------------------------------------------------------------------ | | style | null | No syntax style specified will use default (GITHUB) | | | SyntaxStyle['STYLE'] | All supported styles listed in enum. Intellisense will ensure you select a valid style. | | | | ~ | | languages | null | None specified means you will supply a language later in the component. | | | Language[] | Array of languages which will be loaded on init. Can still specify languages later in component. | | | 'all' | Will load Highlight.js with all languages. This is the simplest solution but also will generate the largest binary. |

More Config Examples

Note: Configuring in the Module import is optional.

// Style and Language Configured
const highlightJsConfig: HighlightJsConfig = {
  style: SyntaxStyle['AN-OLD-HOPE'],
  languages: [Language.TYPESCRIPT]
};

// Only configure language
const highlightJsConfig: HighlightJsConfig = {
  languages: [Language.TYPESCRIPT]
};

// Only configure style
const highlightJsConfig: HighlightJsConfig = {
  style: SyntaxStyle['AN-OLD-HOPE'],
};

// Load all languages and style
const highlightJsConfig: HighlightJsConfig = {
  style: SyntaxStyle.GITHUB,
  languages: 'all'
};

// In your App.Module or Core.Module
imports: [
  NgxHighlightJsModule.forRoot(highlightJsConfig)
]

Syntax Highlighter Component

To highlight a block of code use the below component in your component.html

<ngx-syntax-highlighter
    [language]="languages.CS"
    [style]="syntaxStyle.VS"
    [code]="csharp"
  ></ngx-syntax-highlighter>

OR if you setup the language and style in the module import you can just pass in the code

<ngx-syntax-highlighter [code]="csharp"></ngx-syntax-highlighter>

In your component.ts


import { Language, SyntaxStyle } from 'ngx-syntax-highlighter';
export class AppComponent {
  syntaxStyle = SyntaxStyle;
  languages = Language;
  csharp = `class Program
      {
          static void Main()
          {
              string[] pets = { "dog", "cat", "bird" };

              // ... Loop with the foreach keyword.
              foreach (var value in pets)
              {
                  Console.WriteLine(value);
              }
          }
      }
    `;
}

Force Language

If Highlight.js isn't recognizing the correct language or incorrectly recognizing the language, then use the [forceLanguage] attribute. Make sure you also specify the [language] too.

<ngx-syntax-highlighter 
  [language]="languages.BASH" 
  [code]="bash" 
  [forceLanguage]="true">
</ngx-syntax-highlighter>