-
Notifications
You must be signed in to change notification settings - Fork 9
/
angular.markdown.js
87 lines (73 loc) · 2.42 KB
/
angular.markdown.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
'use strict';
//----------------------------------------------------------------------------------------------------------------------
// A directive for rendering markdown in AngularJS.
// https://bitbucket.org/morgul/angular-markdown
//
// Written by John Lindquist (original author). Modified by Jonathan Rowny (ngModel support).
// Adapted by Christopher S. Case
//
// Taken from: http://blog.angularjs.org/2012/05/custom-components-part-1.html
//
// @module angular.markdown.js
//----------------------------------------------------------------------------------------------------------------------
var MarkdownModule = angular.module('angular-markdown', []);
MarkdownModule.directive('markdown', function () {
var converter = new Showdown.converter();
return {
restrict: 'E',
require: '?ngModel',
link: function (scope, element, attrs, model) {
// Check for extensions
var extAttr = attrs['extensions'];
var callPrettyPrint = false;
if (extAttr) {
var extensions = [];
// Convert the comma separated string into a list.
extAttr.split(',').forEach(function (val) {
// Strip any whitespace from the beginning or end.
extensions.push(val.replace(/^\s+|\s+$/g, ''));
});
if (extensions.indexOf('prettify') >= 0) {
callPrettyPrint = true;
} // end if
// Create a new converter.
converter = new Showdown.converter({
extensions: extensions
});
} // end if
// Check for option to strip whitespace
var stripWS = attrs['strip'];
if (String(stripWS).toLowerCase() == 'true') {
stripWS = true;
} else {
stripWS = false;
} // end if
var render = function () {
var htmlText = "";
var val = "";
// Check to see if we're using a model.
if (attrs['ngModel']) {
if (model.$modelValue) {
val = model.$modelValue;
} // end if
} else {
val = element.text();
} // end if
if (stripWS) {
val = val.replace(/^[ /t]+/g, '').replace(/\n[ /t]+/g, '\n');
} // end stripWS
// Compile the markdown, and set it.
htmlText = converter.makeHtml(val);
element.html(htmlText);
if (callPrettyPrint) {
prettyPrint();
} // end if
};
if (attrs['ngModel']) {
scope.$watch(attrs['ngModel'], render);
} // end if
render();
} // end link
}
}); // end markdown directive
//----------------------------------------------------------------------------------------------------------------------