Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Improved angularjs markdown provider/filter/directive
/*
* angular-markdown-directive v0.1.0
* (c) 2013 J. Harshbarger
* License: MIT
*/
/*
Provider usage:
app.config(['markdownProvider', function(markdownProvider) {
markdownProvider.setDefaultOpts({ extensions: ['github'] });
}]);
Factory usage:
function exampleController(markdown) {
var converter = markdown({ extensions: ['twitter'] });
$scope.html = converter.makeHtml('#TEST');
};
Directive Usage:
<markdown>##TEXT 1</markdown>
<div markdown>##TEXT 2</div>
<div markdown ng-include="scope_element"></div>
<div markdown ng-include="'filename'"></div>
<div markdown="scope_element"></div>
<div markdown="'##TEXT 3'"></div>
<div markdown opts=""{ extensions: ['twitter','github']}"></div>
*/
(function () {
var app = angular.module('markdown', []);
app.provider('markdown', function() {
var defaultOpts = {};
this.$get = function() {
return function(opts) {
var opts = opts || defaultOpts;
return new Showdown.converter(opts);
};
};
this.setDefaultOpts = function(opts) {
defaultOpts = opts;
}
});
app.filter('markdown', ['markdown', function (markdown) {
var converter = markdown(null);
return function (value) {
return converter.makeHtml(value || '');
};
}]);
app.directive("markdown", ['$http', 'markdown', function ($http, markdown) {
return {
restrict: 'AE',
replace: true,
scope: {
opts: '=opts'
},
link: function (scope, element, attrs) {
var converter = markdown(scope.opts || null);
if (attrs.markdown) {
scope.$watch(attrs.markdown, function(value) {
element.html(converter.makeHtml(value || ''));
});
} else {
element.html(converter.makeHtml(element.text()));
}
}
};
}]);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.