Created
December 18, 2013 07:59
-
-
Save Hypercubed/8018807 to your computer and use it in GitHub Desktop.
Improved angularjs markdown provider/filter/directive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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