From 9d7363ea8dd70151fbdd5badc18acfab770c8941 Mon Sep 17 00:00:00 2001 From: Leo Botinelly Date: Wed, 2 May 2018 13:49:24 -0400 Subject: [PATCH 1/3] $compilation of render results This allows for inserted view variables to be properly compiled into the resulting HTML content. --- markdown.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/markdown.js b/markdown.js index 989a6be..40deb6d 100644 --- a/markdown.js +++ b/markdown.js @@ -18,18 +18,18 @@ angular.module('btford.markdown', ['ngSanitize']). } }; }). - directive('btfMarkdown', ['$sanitize', 'markdownConverter', function ($sanitize, markdownConverter) { + directive('btfMarkdown', ["$sanitize", "$compile", "markdownConverter", function ($sanitize, $compile, markdownConverter) { return { restrict: 'AE', link: function (scope, element, attrs) { if (attrs.btfMarkdown) { scope.$watch(attrs.btfMarkdown, function (newVal) { var html = newVal ? $sanitize(markdownConverter.makeHtml(newVal)) : ''; - element.html(html); + element.html($compile(html)(scope)); }); } else { var html = $sanitize(markdownConverter.makeHtml(element.text())); - element.html(html); + element.html($compile(html)(scope)); } } }; From 430a506a8969b172e533dbb4ebbc94396eae1986 Mon Sep 17 00:00:00 2001 From: Leo Botinelly Date: Fri, 4 May 2018 15:03:58 -0400 Subject: [PATCH 2/3] Parametrized compilation Use can add `compile-html='true'` to request html output to be $compiled. --- markdown.js | 61 +++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/markdown.js b/markdown.js index 40deb6d..858098f 100644 --- a/markdown.js +++ b/markdown.js @@ -4,33 +4,38 @@ * License: MIT */ -'use strict'; +"use strict"; -angular.module('btford.markdown', ['ngSanitize']). - provider('markdownConverter', function () { - var opts = {}; - return { - config: function (newOpts) { - opts = newOpts; - }, - $get: function () { - return new Showdown.converter(opts); - } - }; - }). - directive('btfMarkdown', ["$sanitize", "$compile", "markdownConverter", function ($sanitize, $compile, markdownConverter) { - return { - restrict: 'AE', - link: function (scope, element, attrs) { - if (attrs.btfMarkdown) { - scope.$watch(attrs.btfMarkdown, function (newVal) { - var html = newVal ? $sanitize(markdownConverter.makeHtml(newVal)) : ''; - element.html($compile(html)(scope)); - }); - } else { - var html = $sanitize(markdownConverter.makeHtml(element.text())); - element.html($compile(html)(scope)); +angular.module("btford.markdown", ["ngSanitize"]) + .provider("markdownConverter", + function () { + var opts = {}; + return { + config: function (newOpts) { opts = newOpts; }, + $get: function () { return new showdown.Converter(opts); } + }; + }) + .directive("btfMarkdown", + [ + "$sanitize", "$compile", "markdownConverter", function ($sanitize, $compile, markdownConverter) { + return { + restrict: "AE", + link: function (scope, element, attrs) { + + console.log("btfMarkdown", attrs); + + if (attrs.btfMarkdown) { + scope.$watch(attrs.btfMarkdown, function (newVal) { + element.html(newVal ? $sanitize(markdownConverter.makeHtml(newVal)) : ""); + + if (attrs.compileHtml) $compile(element.contents())(scope); + }); + } else { + element.html($sanitize(markdownConverter.makeHtml(element.text()))); + if (attrs.compileHtml) $compile(element.contents())(scope); + + } + } + }; } - } - }; - }]); + ]); From de6461dd94869aa9ca1bab70546ed0f03de6bf81 Mon Sep 17 00:00:00 2001 From: Leo Botinelly Date: Fri, 4 May 2018 15:05:15 -0400 Subject: [PATCH 3/3] Added compile-html attribute Use can add `compile-html='true'` to request html output to be $compiled. --- markdown.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/markdown.js b/markdown.js index 858098f..b296042 100644 --- a/markdown.js +++ b/markdown.js @@ -21,9 +21,6 @@ angular.module("btford.markdown", ["ngSanitize"]) return { restrict: "AE", link: function (scope, element, attrs) { - - console.log("btfMarkdown", attrs); - if (attrs.btfMarkdown) { scope.$watch(attrs.btfMarkdown, function (newVal) { element.html(newVal ? $sanitize(markdownConverter.makeHtml(newVal)) : ""); @@ -32,8 +29,8 @@ angular.module("btford.markdown", ["ngSanitize"]) }); } else { element.html($sanitize(markdownConverter.makeHtml(element.text()))); + if (attrs.compileHtml) $compile(element.contents())(scope); - } } };