Coder Social home page Coder Social logo

Comments (5)

DmitryEfimenko avatar DmitryEfimenko commented on September 21, 2024 1

I built a directive for this. Hopefully, it helps people. Usage:

<pre ngsHighlightContent>
const myVar = 1;
console.log(myVar);
</pre>

from ngx-highlightjs.

MurhafSousli avatar MurhafSousli commented on September 21, 2024

You can already do that, however, you need to use the highlight service and manually highlight using highlightAll or highlightElement functions

from ngx-highlightjs.

peripurnama avatar peripurnama commented on September 21, 2024

i tried the manual way with this code it works, but in the browser there is a warning

var blocks = this.elementRef.nativeElement.querySelectorAll('pre code');
    blocks.forEach((block:any) => {
      this.highlightJS.hljs?.highlightElement(block);
});

One of your code blocks includes unescaped HTML. This is a potentially serious security risk.

from ngx-highlightjs.

peripurnama avatar peripurnama commented on September 21, 2024

I found a solution, you can use this method,
maybe in the library you can add this directive

@Pipe({ name: "safeHtml" })
export class SafeHtmlPipe implements PipeTransform {
  constructor(private sanitizer: DomSanitizer) {}

  transform(value:any) {
    return this.sanitizer.bypassSecurityTrustHtml(value);
  }
}

@Pipe({
  name: 'highlightCode',
})
export class HighlightCodePipe implements PipeTransform {

  constructor(private highlightJS: HighlightJS,
    private sanitizer: DomSanitizer){
  }

  transform(htmlMarkup: string): string {
    const preCodeRegex = /<pre><code(?: class="language-(.*)")?>([\s\S]*?)<\/code><\/pre>/g;

    let result = htmlMarkup.replace(preCodeRegex,  (_match, languageName, codeBlock) => {
      let codeBlockHighlighted: string = '';
      if (!languageName) {
        codeBlockHighlighted = hljs.highlightAuto(codeBlock, ['java']).value || ''
      } else {

        codeBlockHighlighted = hljs.highlight(codeBlock, {
          language: languageName,
          ignoreIllegals: true
        }).value || '';
        
      }

      const code = '<pre><code class="hljs">' + codeBlockHighlighted + '</pre></code>';
      
      return code;
    });
    return result;
  }
}

the html component will look like this

<div [innerHTML]="content | highlightCode | safeHtml"></div>

from ngx-highlightjs.

MurhafSousli avatar MurhafSousli commented on September 21, 2024

Be caution when using a pipe, it isn't ideal for heavy JS, because it will be executed with every change detection.

from ngx-highlightjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.