import { Component , Input } from '@angular/core' ; @ Component ( { selector : 'tweetParser' , template : `<div [innerHTML]="myHtml"></div>` } ) export class TweetParser { @ Input ( 'urlClass' ) urlClass : string = 'react-tweet-parser__url' ; @ Input ( 'userClass' ) userClass : string = 'react-tweet-parser__user' ; @ Input ( 'hashtagClass' ) hashtagClass : string = 'react-tweet-parser__hashTag' ; @ Input ( 'target' ) target : string = '_blank' ; @ Input ( 'searchWithHashtags' ) searchWithHashtags : boolean = true ; @ Input ( 'parseUsers' ) parseUsers : boolean = true ; @ Input ( 'parseUrls' ) parseUrls : boolean = true ; @ Input ( 'parseHashtags' ) parseHashtags : boolean = true ; @ Input ( 'tweet' ) tweet : string = '' ; myHtml : string = '' ; constructor ( ) { } generateLink ( url , urlClass , target , text ) { return `<a href="${ url }" className="${ urlClass }" target="${ target }">${ text }</a>` ; } ngOnChanges ( ) { this . parseTweet ( ) ; } ngOnInit ( ) { this . parseTweet ( ) ; } parseTweet ( ) { const { urlClass , userClass , hashtagClass , target , searchWithHashtags , parseUsers , parseUrls , parseHashtags } = this ; const REGEX_URL = /(?:\s)(f|ht)tps?:\/\/([^\s\t\r\n<]*[^\s\t\r\n<)*_,\.])/g , REGEX_USER = /\B@([a-zA-Z0-9_]+)/g , REGEX_HASHTAG = /\B(#[Ã¡-ÃºÃ-ÃÃ¤-Ã¼Ã-Ãa-zA-Z0-9_]+)/g ; let searchlink , myTweet = this . tweet ; if ( searchWithHashtags ) { searchlink = "https://twitter.com/hashtag/" ; } else { searchlink = "https://twitter.com/search?q=" ; } if ( parseUrls ) { myTweet = myTweet . replace ( REGEX_URL , ( url ) => { let link = this . generateLink ( url , urlClass , target , url ) ; return url . replace ( url , link ) ; } ) ; } if ( parseUsers ) { myTweet = myTweet . replace ( REGEX_USER , ( user ) => { let userOnly = user . slice ( 1 ) , url = `http://twitter.com/${ userOnly }` , link = this . generateLink ( url , userClass , target , user ) ; return user . replace ( user , link ) ; } ) ; } if ( parseHashtags ) { myTweet = myTweet . replace ( REGEX_HASHTAG , ( hashtag ) => { let hashtagOnly = hashtag . slice ( 1 ) , url = searchlink + hashtagOnly , link = this . generateLink ( url , hashtagClass , target , hashtag ) ; return hashtag . replace ( hashtag , link ) ; } ) ; } this . myHtml = myTweet ; } }