|
1 | 1 | angular |
2 | | - .module('gcloud.subpage', ['gcloud.header']) |
| 2 | + .module('gcloud.subpage', ['gcloud.header', 'hljs']) |
| 3 | + |
3 | 4 | .directive('subpage', function($rootScope, $location, $http, $parse, getLinks, versions) { |
4 | 5 | 'use strict'; |
5 | 6 |
|
@@ -34,4 +35,62 @@ angular |
34 | 35 | } |
35 | 36 | } |
36 | 37 | }; |
| 38 | + }) |
| 39 | + |
| 40 | + // A special class used in our MD files to tell the front end to syntax |
| 41 | + // highlight the code blocks. |
| 42 | + .directive('hljsClass', function() { |
| 43 | + 'use strict'; |
| 44 | + |
| 45 | + return { |
| 46 | + // Concept borrowed from: |
| 47 | + // http://www.bennadel.com/blog/2748-compiling-transcluded-content-in-angularjs-directives.htm |
| 48 | + // |
| 49 | + // <div class="hljs-class"> |
| 50 | + // var hi = 'there'; <!-- We want this! |
| 51 | + // </div> |
| 52 | + // |
| 53 | + // We want to replace that with: |
| 54 | + // |
| 55 | + // <div hljs language="javascript" source="var hi = 'there';"></div> |
| 56 | + // |
| 57 | + // To get the content we want, we have to use a higher priority than |
| 58 | + // another same-named directive that uses transclude. This is our only |
| 59 | + // chance to get it before it's clobbered. |
| 60 | + priority: 1500.1, |
| 61 | + restrict: 'C', |
| 62 | + compile: function(element, attrs) { |
| 63 | + attrs._contents = element.text(); |
| 64 | + } |
| 65 | + }; |
| 66 | + }) |
| 67 | + |
| 68 | + .directive('hljsClass', function() { |
| 69 | + 'use strict'; |
| 70 | + |
| 71 | + return { |
| 72 | + priority: 1500, |
| 73 | + restrict: 'C', |
| 74 | + transclude: true, |
| 75 | + template: '<div hljs language="javascript" source="contents"></div>', |
| 76 | + compile: function(element) { |
| 77 | + return function($scope, element, attrs) { |
| 78 | + $scope.contents = attrs._contents; |
| 79 | + }; |
| 80 | + } |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + // Re-compile the Markdown, this time having the "hljs-class" blocks picked |
| 85 | + // up. |
| 86 | + .directive('highlightMarkdown', function($compile) { |
| 87 | + 'use strict'; |
| 88 | + |
| 89 | + return { |
| 90 | + link: function(scope, element, attrs) { |
| 91 | + scope.$watch(attrs.ngInclude, function() { |
| 92 | + $compile(element.contents())(scope); |
| 93 | + }, true); |
| 94 | + } |
| 95 | + }; |
37 | 96 | }); |
0 commit comments