AngularJS directive for syntax highlighting with highlight.js.
- Highlight.js (.js & .css)
- AngularJS v1.0.1+
Follow the instructions here to setup highlight.js.
Using a prebuilt version of highlight.js hosted at Yandex here.
<!-- personal preference: github theme -->
<link rel="stylesheet" href="http://yandex.st/highlightjs/8.0/styles/github.min.css">
<script src="http://yandex.st/highlightjs/8.0/highlight.min.js"></script>
Include angular-highlightjs
module script with AngularJS script on your page.
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
<script src="http://pc035860.github.io/angular-highlightjs/angular-highlightjs.min.js"></script>
Add hljs
to your app module's dependency.
angular.module('myApp', ['hljs']);
Note that the angular-highlightjs
bower package contains no AngularJS dependency.
# install AngularJS (stable)
bower install angular
# or (unstable)
bower install PatternConsulting/bower-angular
# install angular-highlightjs & highlightjs
bower install angular-highlightjs
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: ' '
});
});
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.
<!-- hljs start -->
<div hljs>
<!-- put your codes here -->
</div>
<!-- hljs end -->
Type: expression
Default: undefined
If source
is presented, the hljs
directive will evaluate the expression and highlight the result string. This is pretty useful for dynamically changed content.
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 source="subSource"></div>
</div>
The expression. Beware of single-quotes.
<!-- hljs connected with independent string -->
<div hljs source="'<html><head><body></body></head></html>'"></div>
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.
From text/ng-template
script localOne
. Beware of single-quotes in the expression.
<!-- load text/ng-template named 'localOne' -->
<div hljs include="'localOne'"></div>
From partials/lang-perl
XHR. Again, beware of single-quotes.
<!-- load "partials/lang-perl" -->
<div hljs include="'partials/lang-perl'"></div>
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.
<!-- PHP codes highlight with language detection -->
<div hljs include="'partials/lang-php'"></div>
<!-- PHP codes highlight with specified language: perl -->
<div hljs include="'partials/lang-php'" language="perl"></div>