question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to compile an extension's result?

See original GitHub issue

I was able to make a custom extension, but I couldn’t find how to compile the contained directives.
The given tag is replaced, but the resulting HTML is not processed by Angular.

How to compile the result with Angular? I don’t know if it is even possible to define a compilation from .config(). Should the extension be included somewhere else?

Here is a test extension replacing &test by '<simple-element></simple-element>:

angular.module('myApp')
.config(['$showdownProvider', function($showdownProvider) {
    myExt =  function () {
        return [{
            type: 'output',
            filter: function (text) {
                var regex = /(<p>&amp;test<\/p>)+/g;
                text = text.replace(
                    regex,
                    '<simple-element>extension: ok, directive: NOT</simple-element>'
                );
                return text;
            }
        }];
    };

    $showdownProvider.loadExtension(myExt);   
}]);

The simpleElement directive:

angular.module('myApp')
.directive('simpleElement', [function() {
    return {
        restrict: 'E',
        replace: true,
        template: '<div>Everything works!</div>'
    }
}]);

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jspizziricommented, Jun 14, 2016

Here’s what I ended up with @ntopulos, hopefully this helps.

I created my own markdown directive as an alternative to the mardown-to-html directive:

// directive/markdown/markdown.js
'use strict';

angular.module('myApp')
  .directive('markdown', function($showdown, $sanitize, $sce) {
    return {
      restrict: 'A',
      link: function (scope, element, attrs) {
        scope.$watch('model', function (newValue) {
          var showdownHTML;
          if (typeof newValue === 'string') {
            showdownHTML = $showdown.makeHtml(newValue);
            scope.trustedHtml = ($showdown.getOption('sanitize')) ? $sanitize(showdownHTML) : $sce.trustAsHtml(showdownHTML);
          } else {
            scope.trustedHtml = typeof newValue;
          }
        });
      },
      scope: {
        model: '=markdown'
      },
      template: '<div bind-html-compile="trustedHtml"></div>'
    };
  });

Please note that this is an exact copy of the markdown-to-html directive with the exception of this: template: '<div bind-html-compile="trustedHtml"></div>'.

With that being done, I created another directive bind-html-compile which compiled the html given to it which looks like so:

// directive/bindHtmlCompile/bindHtmlCompile.js
'use strict';

angular.module('myApp')
  .directive('bindHtmlCompile', function($compile) {
    return {
      restrict: 'A',
      link: function(scope, element, attrs) {
        scope.$watch(function() {
          return scope.$eval(attrs.bindHtmlCompile);
        }, function(value) {
          element.html(value);
          $compile(element.contents())(scope);
        });
      }
    };
  });

Usage:

<!-- instead of this: -->
<div markdown-to-html="myMarkdown"></div>

<!-- use this: -->
<div markdown="myMarkdown"></div>

Voilà!

Additionally, to keep my organization a little cleaner, I created the showdown extensions as angular constants like so:

// constants/myExt.js
(function(){

  'use strict';

  angular.module('myApp')
    .constant('myExt',
      function myExt() {
        var myext1 = {
           type: 'lang',
           regex: /markdown/g,
           replace: 'showdown'
         };
         var myext2 = {
           /* extension code */
         };
         return [myext1, myext2];
      });

})();

And then injected and registered them in the config:

// app.js

  .config(function($showdownProvider, myExt) {
    $showdownProvider.setOption('tables', true);
    //...

    $showdownProvider.loadExtension(myExt);
    //...
  })

Hope this helps!

1reaction
ntopuloscommented, Jun 17, 2016

Thank you both for your replies! Unfortunately I wasn’t able to make it work, what do I miss?

I’ve created these jsfiddles, and as you can see the directive is never executed when generated by a ShowDown extension:

In each example I use the extension to replace &test inside the markdown string by <simple-element>extension: executed, directive: NOT executed</simple-element>. Then the SimpleElement directive should replace this by <div>directive executed</div>, but it doesn’t.

Edit (works):

Actually @jspizziri’s method worked! https://jsfiddle.net/ov0q23Le/1/

I thought it didn’t because of a local issue with this line that I forgot to uncomment when creating the jsfiddle: scope.trustedHtml = ($showdown.getOption('sanitize')) ? $sanitize(showdownHTML) : $sce.trustAsHtml(showdownHTML);

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to compile a Google Chrome extension out of code I ...
1 Answer 1 · First organize your script, source file(s) and explicitly create the manifest. · You do not need to alter that...
Read more >
Chrome Extensions getting started guides
These tutorials not only teach you how to build real-world extensions but also strive to share development tips and best practices.
Read more >
How to Build a Chrome Extension - Neil Patel
Build a Chrome Extension Step 4: Load Your Extension into Chrome and Check for Errors · Go to chrome://extensions in your Google Chrome...
Read more >
Google Chrome Extensions: How to build an ... - YouTube
Brian Kennish, a developer advocate for Google Chrome presents how to build an extension for Google Chrome.
Read more >
Your First Extension - Visual Studio Code
Then, inside the editor, press F5. This will compile and run the extension in a new Extension Development Host window. Run the Hello...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found