Dynamic Syntax Highlighting with AngularJS and Highlight.js

后端 未结 2 2078
小鲜肉
小鲜肉 2021-02-08 17:43

I am building a site that illustrates common application vulnerabilities such as SQL Injection. I am using AngularJS and highlight.js to creat

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-08 18:14

    A simpler way I just found is to use a filter:

    app.filter('highlight', function($sce) {
      return function(input, lang) {
        if (lang && input) return hljs.highlight(lang, input).value;
        return input;
      }
    }).filter('unsafe', function($sce) { return $sce.trustAsHtml; })
    

    Then you can say:

    The $sce needs to be injected into your app, and tells Angular to display the HTML raw - that you trust it.

提交回复
热议问题