Skip to content

Instantly share code, notes, and snippets.

@dberesford
Created November 15, 2012 20:30
Show Gist options
  • Save dberesford/4081066 to your computer and use it in GitHub Desktop.
Save dberesford/4081066 to your computer and use it in GitHub Desktop.
How to add a new markdown HTML element to your AngularJS App

First install [https://github.com/coreyti/showdown](showdown js) component:

yeoman install showdown

This adds the showdown component to your App.

Create a new 'markdown' AngularJS Directive:

yeoman init angular:directive markdown

This creates two new files:

app/scripts/directives/markdown.js
test/spec/directives/markdown.js

Edit app/scripts/directives/markdown.js and change as follows:

yourApp.directive('markdown', function() {
  var converter = new Showdown.converter();
  return {
    restrict: 'E',
    link: function(scope, element, attrs) {
      var tmp = $interpolate(element.text())(scope);
      $timeout(function() {
        var htmlText = converter.makeHtml(tmp);
        element.html(htmlText);
      }, 0);
    }
  };
});

Finally, add a reference to showdown.js to your index.html:

<script src="components/showdown/compressed/showdown.js"></script> 

You should now be able to use the new markdown element in any of your views, e.g <markdown> **hello world** </markdown>, or <markdown>{{foo}}</markdown>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment