import { Pipe , PipeTransform } from '@angular/core' ; import { CodeHighlightService } from './code-highlight.service' ; import { FeaturesService } from '../../services/features.service' ; example example author Jim Toth <jim@meme.coach>
  @ Pipe ( { name : 'codeHighlight' , } ) export class CodeHighlightPipe implements PipeTransform { constructor ( private codeHighlightService , private featuresService ) { } param { string } text returns { string } transform ( text ) { if ( ! text || ! this . featuresService . has ( 'code-highlight' ) ) { return text ; } const codeFenceRegex = /```(\w+)?[\s\n]((?:(?!```).|\n)*)```/gm ; let language ; const transformed = text . replace ( codeFenceRegex , ( match , lang , code ) => { language = lang ; let highlighted ; if ( lang && lang !== 'auto' ) { highlighted = this . codeHighlightService . highlight ( lang , code ) . value ; } else { const highlightResult = this . codeHighlightService . highlightAuto ( code ) ; language = highlightResult . language ; highlighted = highlightResult . value ; } return ( `<div class="${ CodeHighlightService . moduleWrapperClass }">` + `<pre><code class="language-${ language }">${ highlighted }</code></pre>` + `</div>` ) ; } ) ; if ( language ) { return transformed ; } return text ; } }