forked from Hypercubed/angular-marked
-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-marked.js
319 lines (268 loc) · 8.57 KB
/
angular-marked.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
* angular-marked
* (c) 2014 J. Harshbarger
* Licensed MIT
*/
/* jshint undef: true, unused: true */
/* global angular:true */
/* global marked:true */
/* global module */
/* global require */
(function () {
'use strict';
/**
* @ngdoc overview
* @name index
*
* @description
* AngularJS Markdown using [marked](https://github.com/chjj/marked).
*
* ## Why?
*
* I wanted to use [marked](https://github.com/chjj/marked) instead of [showdown](https://github.com/coreyti/showdown) as used in [angular-markdown-directive](https://github.com/btford/angular-markdown-directive) as well as expose the option to globally set defaults.
*
* ## How?
*
* - {@link hc.marked.directive:marked As a directive}
* - {@link hc.marked.service:marked As a service}
* - {@link hc.marked.service:markedProvider Set default options}
*
* @example
Convert markdown to html at run time. For example:
<example module="hc.marked">
<file name=".html">
<form ng-controller="MainController">
Markdown:<br />
<textarea ng-model="my_markdown" cols="60" rows="5" class="span8" /><br />
Output:<br />
<div marked="my_markdown" />
</form>
</file>
<file name=".js">
function MainController($scope) {
$scope.my_markdown = "*This* **is** [markdown](https://daringfireball.net/projects/markdown/)";
}
</file>
</example>
*
*/
/**
* @ngdoc overview
* @name hc.marked
* @description # angular-marked (core module)
# Installation
First include angular-marked.js in your HTML:
```js
<script src="angular-marked.js">
```
Then load the module in your application by adding it as a dependency:
```js
angular.module('yourApp', ['hc.marked']);
```
With that you're ready to get started!
*/
if (typeof module !== 'undefined' && typeof exports === 'object') {
module.exports = 'hc.marked';
}
angular.module('hc.marked', [])
/**
* @ngdoc service
* @name hc.marked.service:marked
* @requires $window
* @description
* A reference to the [marked](https://github.com/chjj/marked) parser.
*
* @example
<example module="hc.marked">
<file name=".html">
<div ng-controller="MainController">
html: {{html}}
</div>
</file>
<file name=".js">
function MainController($scope, marked) {
$scope.html = marked('#TEST');
}
</file>
</example>
**/
/**
* @ngdoc service
* @name hc.marked.service:markedProvider
* @description
* Use `markedProvider` to change the default behavior of the {@link hc.marked.service:marked marked} service.
*
* @example
## Example using [google-code-prettify syntax highlighter](https://code.google.com/p/google-code-prettify/) (must include google-code-prettify.js script). Also works with [highlight.js Javascript syntax highlighter](http://highlightjs.org/).
<example module="myApp">
<file name=".js">
angular.module('myApp', ['hc.marked'])
.config(['markedProvider', function(markedProvider) {
markedProvider.setOptions({
gfm: true,
tables: true,
highlight: function (code) {
return prettyPrintOne(code);
}
});
}]);
</file>
<file name=".html">
<marked>
```js
angular.module('myApp', ['hc.marked'])
.config(['markedProvider', function(markedProvider) {
markedProvider.setOptions({
gfm: true,
tables: true,
highlight: function (code) {
return prettyPrintOne(code);
}
});
}]);
```
</marked>
</file>
</example>
**/
.provider('marked', function () {
var self = this;
/**
* @ngdoc method
* @name markedProvider#setOptions
* @methodOf hc.marked.service:markedProvider
*
* @param {object} opts Default options for [marked](https://github.com/chjj/marked#options-1).
*/
self.setRenderer = function (opts) {
this.renderer = opts;
};
self.setOptions = function(opts) { // Store options for later
this.defaults = opts;
};
self.$get = ['$window', '$log', function ($window, $log) {
var m = (function() {
if (typeof module !== 'undefined' && typeof exports === 'object') {
return require('marked');
} else {
return $window.marked || marked;
}
})();
if (angular.isUndefined(m)) {
$log.error('angular-marked Error: marked not loaded. See installation instructions.');
return;
}
// override rendered markdown html
// with custom definitions if defined
if (self.renderer) {
var r = new m.Renderer();
var o = Object.keys(self.renderer),
l = o.length;
while (l--) {
r[o[l]] = self.renderer[o[l]];
}
// add the new renderer to the options if need be
self.defaults = self.defaults ? self.defaults.renderer = r : self.defaults = {renderer: r };
}
m.setOptions(self.defaults);
return m;
}];
})
// TODO: filter tests */
//app.filter('marked', ['marked', function(marked) {
// return marked;
//}]);
/**
* @ngdoc directive
* @name hc.marked.directive:marked
* @restrict AE
* @element any
*
* @description
* Compiles source test into HTML.
*
* @param {expression} marked The source text to be compiled. If blank uses content as the source.
* @param {expression=} opts Hash of options that override defaults.
*
* @example
## A simple block of text
<example module="hc.marked">
<file name="exampleA.html">
* <marked>
* ### Markdown directive
*
* *It works!*
*
* *This* **is** [markdown](https://daringfireball.net/projects/markdown/) in the view.
* </marked>
</file>
</example>
## Bind to a scope variable
<example module="hc.marked">
<file name="exampleB.html">
<form ng-controller="MainController">
Markdown:<br />
<textarea ng-model="my_markdown" class="span8" cols="60" rows="5"></textarea><br />
Output:<br />
<blockquote marked="my_markdown"></blockquote>
</form>
</file>
<file name="exampleB.js">
* function MainController($scope) {
* $scope.my_markdown = '*This* **is** [markdown](https://daringfireball.net/projects/markdown/)';
* $scope.my_markdown += ' in a scope variable';
* }
</file>
</example>
## Include a markdown file:
<example module="hc.marked">
<file name="exampleC.html">
<div marked ng-include="'include.html'" />
</file>
* <file name="include.html">
* *This* **is** [markdown](https://daringfireball.net/projects/markdown/) in a include file.
* </file>
</example>
*/
.directive('marked', ['marked', function (marked) {
return {
restrict: 'AE',
replace: true,
scope: {
opts: '=',
marked: '='
},
link: function (scope, element, attrs) {
set(scope.marked || element.text() || '');
function unindent(text) {
if (!text) return text;
var lines = text
.replace(/\t/g, ' ')
.split(/\r?\n/);
var i, l, min = null, line, len = lines.length;
for (i = 0; i < len; i++) {
line = lines[i];
l = line.match(/^(\s*)/)[0].length;
if (l === line.length) { continue; }
min = (l < min || min === null) ? l : min;
}
if (min !== null && min > 0) {
for (i = 0; i < len; i++) {
lines[i] = lines[i].substr(min);
}
}
return lines.join('\n');
}
function set(text) {
text = unindent(text || '');
element.html(marked(text, scope.opts || null));
}
if (attrs.marked) {
scope.$watch('marked', function (marked) {
if (marked) set(marked);
});
}
}
};
}]);
}());