aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/core/renderer/linkifier.service.ts
blob: d99591d6c630497cae8bf01d0979a6bfbfa36b1d (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                                          
                                                      


                               

                                


                                



                                                       


                                  
                                                                                       
       


     


                                        


                                                            


                                    
                                        
     
 
                                                            
   
 
import { Injectable } from '@angular/core'
import { getAbsoluteAPIUrl } from '@app/helpers/utils'

@Injectable()
export class LinkifierService {
  static CLASSNAME = 'linkified'

  private linkifyModule: any
  private linkifyHtmlModule: any

  private linkifyOptions = {
    className: {
      mention: LinkifierService.CLASSNAME + '-mention',
      url: LinkifierService.CLASSNAME + '-url'
    },
    formatHref: {
      mention: (href: string) => {
        return getAbsoluteAPIUrl() + '/services/redirect/accounts/' + href.substring(1)
      }
    }
  }

  async linkify (text: string) {
    if (!this.linkifyModule) {
      const result = await Promise.all([
        import('linkifyjs'),
        import('linkify-plugin-mention'),
        import('linkify-html').then(m => (m as any).default)
      ])

      this.linkifyModule = result[0]
      this.linkifyHtmlModule = result[2]
    }

    return this.linkifyHtmlModule(text, this.linkifyOptions)
  }
}