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

angular-highlightjs

v0.7.1

Published

AngularJS directive for syntax highlighting with highlight.js

Downloads

27,360

Readme

angular-highlightjs npm Bower Join the chat at https://gitter.im/pc035860/angular-highlightjs

AngularJS directive for syntax highlighting with highlight.js.

Demos

Requirements

  • Highlight.js v7.0.0+
  • AngularJS v1.0.1+

Getting started

Follow the instructions here to setup highlight.js.

Using a prebuilt version of highlight.js hosted at cdnjs here.

<!-- personal preference: github theme -->
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.9.1/styles/github.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.9.1/highlight.min.js"></script>

Include angular-highlightjs module script with AngularJS script on your page.

<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script src="https://pc035860.github.io/angular-highlightjs/angular-highlightjs.min.js"></script>

Add hljs to your app module's dependency.

angular.module('myApp', ['hljs']);

Install with npm

npm install angular-highlightjs

Install with Bower

There's currently no official bower package of highlight.js (see here). You should either build highlight.js yourself or use the pre-built one on cdnjs.

bower install angular-highlightjs

Configuration

Configuration works with highlight.js >= 8.0

In configuration phase, call hljsServiceProvider.setOptions() to configure with highlight.js options.

myApp.config(function (hljsServiceProvider) {
  hljsServiceProvider.setOptions({
    // replace tab with 4 spaces
    tabReplace: '    '
  });
});

Directive usage

hljs

This is a required directive. Without any other supportive directives, it provides basic inline highlight function. For better understanding, some notes about using it are specified in the live example page.

The directive automatically escapes its content HTML entities by default. Can be turned off with explicitly configuration hljs-escape="{expression evaled to false}" or hljs-no-escape, and they're only applicable for "just-hljs" usage.

Live example

<!-- hljs start -->
<div hljs>
<!-- put your codes here -->
</div>
<!-- hljs end -->

<!-- Will get the same result as above -->
<div hljs
    hljs-no-escape>
&lt;!-- put your codes here --&gt;
</div>

Frequently Asked Question

Since the code inside hljs will be parsed by browser even before AngularJS bootstraped, it sometimes demonstrates strange highlight result when the code you put inside hljs have HTML-tag-like syntax (<blah>).
To deal with the issue, use hljs-no-escape option with manually escaped code or switch to hljs-source or hljs-include for highlighting.

hljs-source (optional)

Type: expression Default: undefined

If hljs-source is presented, the hljs directive will evaluate the expression and highlight the result string. This is pretty useful for dynamically changed content.

Live example

Dynamically changed content.

<!-- buttons for put/clear $scope.subSource content -->
<button class="btn btn-primary show-source" 
        ng-click="toggleSource('subSource')"
        ng-show="!subSource">put $scope.subSource</button>
<button class="btn btn-primary show-source" 
        ng-click="toggleSource('subSource')"
        ng-show="subSource">clear $scope.subSource</button>
<div ng-show="subSource">
  <br>
  <!-- hljs connected with $scope.subSource -->
  <div hljs
    hljs-source="subSource"></div>
</div>

The expression. Beware of single-quotes.

<!-- hljs connected with independent string -->
<div hljs
    hljs-source="'<html><head><body></body></head></html>'"></div>

hljs-include (optional)

Type: expression Default: undefined

Works as the built-in ng-include directive, utilizes $templateCache and $http to retrieve content from text/ng-template scripts or from XHR.

Live example

From text/ng-template script localOne. Beware of single-quotes in the expression.

<!-- load text/ng-template named 'localOne' -->
<div hljs
    hljs-include="'localOne'"></div>

From partials/lang-perl XHR. Again, beware of single-quotes.

<!-- load "partials/lang-perl" -->
<div hljs
    hljs-include="'partials/lang-perl'"></div>

hljs-language (optional)

Type: string Default: undefined

Tells the highlight.js which language syntax should be used to highlight the codes. If not specified, highlight.js will highlight with built-in language detection.

Live example

<!-- PHP codes highlight with language detection -->
<div hljs 
    hljs-include="'partials/lang-php'"></div>

<!-- PHP codes highlight with specified language: perl -->
<div hljs 
    hljs-include="'partials/lang-php'"
    hljs-language="perl"></div>

hljs-interpolate (optional)

Type: expression Default: undefined

Interpolates the highlighted code and links it with current scope.

The attribute works with all methods of highlighting: hljs, hljs-source and hljs-include.

Live example

<div hljs
    hljs-include="'interpolate-me'"
    hljs-interpolate="true"></div>

Happy highlighting!!!