Skip to content

Commit d1bb868

Browse files
committed
feat(sanitize): add feature to force showdown's html output to be aggressively sanitized
1 parent 08f85b7 commit d1bb868

File tree

5 files changed

+68
-30
lines changed

5 files changed

+68
-30
lines changed

dist/ng-showdown.js

Lines changed: 32 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ng-showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ng-showdown.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ng-showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ng-showdown.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
module
1414
.provider('$showdown', ngShowdown)
15-
.directive('sdModelToHtml', ['$showdown', '$sce', sdModelToHtmlDirective]) //<-- DEPRECATED: will be removed in the next major version release
16-
.directive('markdownToHtml', ['$showdown', '$sce', markdownToHtmlDirective])
15+
.directive('sdModelToHtml', ['$showdown', '$sanitize', sdModelToHtmlDirective]) //<-- DEPRECATED: will be removed in the next major version release
16+
.directive('markdownToHtml', ['$showdown', '$sanitize', markdownToHtmlDirective])
1717
.filter('sdStripHtml', ['$showdown', stripHtmlFilter]) //<-- DEPRECATED: will be removed in the next major version release
1818
.filter('stripHtml', ['$showdown', stripHtmlFilter]);
1919

@@ -29,7 +29,8 @@
2929

3030
// Configuration parameters for Showdown
3131
var config = {
32-
extensions: []
32+
extensions: [],
33+
sanitize: false
3334
};
3435

3536
/**
@@ -91,6 +92,23 @@
9192
this.stripHtml = function (text) {
9293
return String(text).replace(/<[^>]+>/gm, '');
9394
};
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+
};
94112
}
95113

96114
// The object returned by service provider
@@ -107,13 +125,13 @@
107125
* <div sd-model-to-html="markdownText" ></div>
108126
*
109127
* @param {showdown.Converter} $showdown
110-
* @param {$sce} $sce
128+
* @param {$sanitize} $sanitize
111129
* @returns {*}
112130
*/
113-
function sdModelToHtmlDirective($showdown, $sce) {
131+
function sdModelToHtmlDirective($showdown, $sanitize) {
114132
return {
115133
restrict: 'A',
116-
link: getLinkFn($showdown, $sce),
134+
link: getLinkFn($showdown, $sanitize),
117135
scope: {
118136
model: '=sdModelToHtml'
119137
}
@@ -127,26 +145,27 @@
127145
* <div markdown-to-html="markdownText" ></div>
128146
*
129147
* @param {showdown.Converter} $showdown
130-
* @param {$sce} $sce
148+
* @param {$sanitize} $sanitize
131149
* @returns {*}
132150
*/
133-
function markdownToHtmlDirective($showdown, $sce) {
151+
function markdownToHtmlDirective($showdown, $sanitize) {
134152
return {
135153
restrict: 'A',
136-
link: getLinkFn($showdown, $sce),
154+
link: getLinkFn($showdown, $sanitize),
137155
scope: {
138156
model: '=markdownToHtml'
139157
}
140158
};
141159
}
142160

143-
function getLinkFn($showdown, $sce) {
161+
function getLinkFn($showdown, $sanitize) {
144162
return function (scope, element) {
145163
scope.$watch('model', function (newValue) {
146-
var val;
164+
var val,
165+
showdownHTML;
147166
if (typeof newValue === 'string') {
148-
var showdownHTML = $showdown.makeHtml(newValue);
149-
val = $sce.trustAsHtml(showdownHTML);
167+
showdownHTML = $showdown.makeHtml(newValue);
168+
val = ($showdown.getOption('sanitize')) ? $sanitize(showdownHTML) : showdownHTML;
150169
} else {
151170
val = typeof newValue;
152171
}

0 commit comments

Comments
 (0)