Search code examples
angularjsmarkdownangular-filters

Angular filter for highlighting word(s) inside of asterik to with bold


I want to input * this is a sentence * and get < strong >this is a sentence< /strong > as an output.

I have looked at https://github.com/Hypercubed/angular-marked but this gives too many options that I don't want (like headings).

Any help?


Solution

  • I've created a demo filter to convert text to <strong>text</strong> here: http://plnkr.co/edit/cQjytfvsT1Qu9ygjfEHz?p=preview

    app.filter('asteriskToBoldFilter', function($sce){
      return function(val) {
        var matches = val.match( /\*(.*?)\*/g );
        if(matches){
          matches.forEach(function(line){
            var newline = line.replace('*', '<strong>').replace('*', '</strong>');
            val = val.replace(line, newline);
          })
        }
        return $sce.trustAsHtml(val);
      };
    })