import { Pipe , Inject , PipeTransform } from '@angular/core' ; import { FeaturesService } from '../../services/features.service' ; import { SiteService } from '../services/site.service' ; import { RegexService } from '../services/regex.service' ; @ Pipe ( { name : 'tags' , } ) export class TagsPipe implements PipeTransform { results = [ ] ; /**
     * Tags
     */ tags = { url : { rule : this . regexService . getRegex ( 'url' ) , replace : m => { const url = m . match [ 1 ] ; return `<a href="${ url }" target="_blank" rel="${ this . siteService . getLinkRel ( url ) }">${ m . match [ 1 ] }</a>` ; } , } , mail : { rule : this . regexService . getRegex ( 'mail' ) , replace : m => { const url = m . match [ 0 ] ; return `<a href="mailto:${ url }" target="_blank">${ m . match [ 0 ] }</a>` ; } , } , hash : { rule : this . regexService . getRegex ( 'hash' ) , replace : m => { if ( this . siteService . isProDomain ) { return `${ m . match [ 1 ] }<a href="/all;query=${ m . match [ 2 ] . toLowerCase ( ) }">#${ m . match [ 2 ] }</a>` ; } return `${ m . match [ 1 ] }<a href="/discovery/search?f=top&t=all&q=%23${ m . match [ 2 ] . toLowerCase ( ) }" class="m-legible">#${ m . match [ 2 ] }</a>` ; } , } , crypto : { rule : this . regexService . getRegex ( 'crypto' ) , replace : m => { if ( this . siteService . isProDomain ) { return `${ m . match [ 1 ] }<a href="/all;query=${ m . match [ 2 ] . toUpperCase ( ) }">#${ m . match [ 2 ] }</a>` ; } return `${ m . match [ 1 ] }<a href="/discovery/search?f=top&t=all&q=%24${ m . match [ 2 ] . toUpperCase ( ) }" class="m-legible">$${ m . match [ 2 ] }</a>` ; } , } , at : { rule : this . regexService . getRegex ( 'at' ) , replace : m => { return `${ m . match [ 1 ] }<a class="tag" href="/${ m . match [ 2 ] }" target="_blank">@${ m . match [ 2 ] }</a>` ; } , } , } ; constructor ( private featureService , private siteService , public regexService ) { } param match push ( match ) { if ( this . results . findIndex ( m => match . start >= m . start && match . end <= m . end ) !== - 1 ) { return ; } this . results . push ( match ) ; } param tag param value parse ( tag , value ) { let match ; while ( ( match = this . tags [ tag ] . rule . exec ( value ) ) !== null ) { this . push ( { type : tag , start : match . index , end : match . index + match [ 0 ] . length , match : match , } ) ; } } param str replace ( str ) { this . results . forEach ( m => { str = str . replace ( m . match [ 0 ] , this . tags [ m . type ] . replace ( m , str ) ) ; } ) ; return str ; } transform ( value ) { this . results = [ ] ; this . parse ( 'url' , value ) ; this . parse ( 'mail' , value ) ; this . parse ( 'hash' , value ) ; this . parse ( 'crypto' , value ) ; this . parse ( 'at' , value ) ; if ( this . results . length === 0 ) { return value ; } this . results . sort ( ( a , b ) => a . start - b . start ) ; let html = [ ] ; let copyStartIndex = 0 ; for ( let i = 0 ; i < this . results . length ; i ++ ) { let tag = this . results [ i ] ; html . push ( value . substring ( copyStartIndex , tag . start ) ) ; html . push ( this . tags [ tag . type ] . replace ( tag ) ) ; copyStartIndex = tag . end ; if ( i == this . results . length - 1 ) { html . push ( value . substring ( copyStartIndex ) ) ; } } return html . join ( '' ) ; } }