|
10 | 10 | (function (module, showdown) { |
11 | 11 | 'use strict'; |
12 | 12 |
|
13 | | - module.provider('$showdown', ngShowdown) |
14 | | - .directive('sdModelToHtml', ['$showdown', '$sce', markdownToHtmlDirective]) //<-- DEPRECATED: will be removed in the next major version release |
15 | | - .directive('markdownToHtml', ['$showdown', '$sce', markdownToHtmlDirective]) |
| 13 | + module |
| 14 | + .provider('$showdown', ngShowdown) |
| 15 | + .directive('sdModelToHtml', ['$showdown', '$sanitize', '$sce', sdModelToHtmlDirective]) //<-- DEPRECATED: will be removed in the next major version release |
| 16 | + .directive('markdownToHtml', ['$showdown', '$sanitize', '$sce', markdownToHtmlDirective]) |
16 | 17 | .filter('sdStripHtml', ['$showdown', stripHtmlFilter]) //<-- DEPRECATED: will be removed in the next major version release |
17 | 18 | .filter('stripHtml', ['$showdown', stripHtmlFilter]); |
18 | 19 |
|
|
28 | 29 |
|
29 | 30 | // Configuration parameters for Showdown |
30 | 31 | var config = { |
31 | | - extensions: [] |
| 32 | + extensions: [], |
| 33 | + sanitize: false |
32 | 34 | }; |
33 | 35 |
|
34 | 36 | /** |
|
90 | 92 | this.stripHtml = function (text) { |
91 | 93 | return String(text).replace(/<[^>]+>/gm, ''); |
92 | 94 | }; |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets the value of the configuration parameter of CONVERTER specified by key |
| 98 | + * @param {string} key The config parameter key |
| 99 | + * @returns {*} |
| 100 | + */ |
| 101 | + this.getOption = function (key) { |
| 102 | + return converter.getOption(key); |
| 103 | + }; |
| 104 | + |
| 105 | + /** |
| 106 | + * Gets the converter configuration params |
| 107 | + * @returns {*} |
| 108 | + */ |
| 109 | + this.getOptions = function () { |
| 110 | + return converter.getOptions(); |
| 111 | + }; |
93 | 112 | } |
94 | 113 |
|
95 | 114 | // The object returned by service provider |
|
99 | 118 | } |
100 | 119 |
|
101 | 120 | /** |
102 | | - * AngularJS Directive to Md to HTML transformation |
| 121 | + * @deprecated |
| 122 | + * Legacy AngularJS Directive to Md to HTML transformation |
103 | 123 | * |
104 | 124 | * Usage example: |
105 | 125 | * <div sd-model-to-html="markdownText" ></div> |
106 | 126 | * |
107 | 127 | * @param {showdown.Converter} $showdown |
| 128 | + * @param {$sanitize} $sanitize |
108 | 129 | * @param {$sce} $sce |
109 | 130 | * @returns {*} |
110 | 131 | */ |
111 | | - function markdownToHtmlDirective($showdown, $sce) { |
| 132 | + function sdModelToHtmlDirective($showdown, $sanitize, $sce) { |
112 | 133 | return { |
113 | 134 | restrict: 'A', |
114 | | - link: link, |
| 135 | + link: getLinkFn($showdown, $sanitize, $sce), |
115 | 136 | scope: { |
116 | 137 | model: '=sdModelToHtml' |
117 | 138 | } |
118 | 139 | }; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * AngularJS Directive to Md to HTML transformation |
| 144 | + * |
| 145 | + * Usage example: |
| 146 | + * <div markdown-to-html="markdownText" ></div> |
| 147 | + * |
| 148 | + * @param {showdown.Converter} $showdown |
| 149 | + * @param {$sanitize} $sanitize |
| 150 | + * @param {$sce} $sce |
| 151 | + * @returns {*} |
| 152 | + */ |
| 153 | + function markdownToHtmlDirective($showdown, $sanitize, $sce) { |
| 154 | + return { |
| 155 | + restrict: 'A', |
| 156 | + link: getLinkFn($showdown, $sanitize, $sce), |
| 157 | + scope: { |
| 158 | + model: '=markdownToHtml' |
| 159 | + } |
| 160 | + }; |
| 161 | + } |
119 | 162 |
|
120 | | - function link(scope, element) { |
| 163 | + function getLinkFn($showdown, $sanitize, $sce) { |
| 164 | + return function (scope, element) { |
121 | 165 | scope.$watch('model', function (newValue) { |
122 | | - var val; |
| 166 | + var val, |
| 167 | + showdownHTML; |
123 | 168 | if (typeof newValue === 'string') { |
124 | | - var showdownHTML = $showdown.makeHtml(newValue); |
125 | | - val = $sce.trustAsHtml(showdownHTML); |
| 169 | + showdownHTML = $showdown.makeHtml(newValue); |
| 170 | + val = ($showdown.getOption('sanitize')) ? $sanitize(showdownHTML) : $sce.trustAsHtml(showdownHTML); |
126 | 171 | } else { |
127 | 172 | val = typeof newValue; |
128 | 173 | } |
129 | 174 | element.html(val); |
130 | 175 | }); |
131 | | - } |
| 176 | + }; |
132 | 177 | } |
133 | 178 |
|
134 | 179 | /** |
|
0 commit comments