aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/renderer/html-renderer.service.ts
blob: d49df9b6d9a7c6e9ca2262982dbb1683fbabdd2c (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
import { Injectable } from '@angular/core'
import { LinkifierService } from '@app/shared/renderer/linkifier.service'
import * as sanitizeHtml from 'sanitize-html'

@Injectable()
export class HtmlRendererService {

  constructor (private linkifier: LinkifierService) {

  }

  toSafeHtml (text: string) {
    // Convert possible markdown to html
    const html = this.linkifier.linkify(text)

    return sanitizeHtml(html, {
      allowedTags: [ 'a', 'p', 'span', 'br' ],
      allowedSchemes: [ 'http', 'https' ],
      allowedAttributes: {
        'a': [ 'href', 'class', 'target' ]
      },
      transformTags: {
        a: (tagName, attribs) => {
          return {
            tagName,
            attribs: Object.assign(attribs, {
              target: '_blank',
              rel: 'noopener noreferrer'
            })
          }
        }
      }
    })
  }
}