aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/core/renderer/linkifier.service.ts
blob: d99591d6c630497cae8bf01d0979a6bfbfa36b1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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)
  }
}