import { Pipe , PipeTransform } from '@angular/core' ; import marked from 'marked' ; import * as highlight from 'highlightjs' ; @ Pipe ( { name : 'markdownToHtml' } ) export class MarkdownToHtmlPipe implements PipeTransform { transform ( text , baseUrl ) { let m = marked . setOptions ( { highlight : function ( code ) { return highlight . highlightAuto ( code ) . value ; } } ) ; let html = m . parse ( text ) ; let dom = ( new DOMParser ( ) ) . parseFromString ( html , 'text/html' ) ; let imgs = dom . getElementsByTagName ( 'img' ) ; for ( let i = 0 ; i < imgs . length ; i ++ ) { let img = imgs [ i ] ; let imgSrc = img . getAttribute ( 'src' ) ; if ( ! ( imgSrc . match ( /^http:\/\// ) || imgSrc . match ( /^https:\/\// ) || imgSrc . match ( /^\/\// ) ) ) { imgSrc = baseUrl + '/' + imgSrc ; } img . setAttribute ( 'src' , imgSrc ) ; } let aTags = dom . getElementsByTagName ( 'a' ) ; for ( let i = 0 ; i < aTags . length ; i ++ ) { aTags [ i ] . removeAttribute ( 'href' ) ; } return dom . getElementsByTagName ( 'body' ) [ 0 ] . innerHTML ; } }