forked from PatrickHeneise/angular-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangular.markdown.js
137 lines (116 loc) · 4.2 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
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
//----------------------------------------------------------------------------------------------------------------------
// A directive for rendering markdown in AngularJS.
//
// 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
// Check for option to allow html
var allowHtml = attrs['allowHtml'];
if(String(allowHtml).toLowerCase() == 'true')
{
allowHtml = true;
}
else
{
allowHtml = false;
} // end if
// Check for option to translate line breaks
var lineBreaks = attrs['lineBreaks'];
if (String(lineBreaks).toLowerCase() == 'true') {
lineBreaks = true;
} else {
lineBreaks = 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;
if (!allowHtml)
{
val = String(val).replace(/<[^>]+>/gm, '');
}
} // end if
}
else
{
// Export using text() by default...
var exportFn = 'text';
// But with html() if allowHtml is "true"
if (allowHtml)
{
exportFn = 'html';
} // end if
val = element[exportFn]();
} // end if
if(stripWS)
{
val = val.replace(/^[ /t]+/g, '').replace(/\n[ /t]+/g, '\n');
} // end stripWS
if (lineBreaks) {
val = val.replace(/ /g, '\n');
} // end lineBreaks
// 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
//----------------------------------------------------------------------------------------------------------------------