]>
Commit | Line | Data |
---|---|---|
1506307f C |
1 | import { Injectable } from '@angular/core' |
2 | import { LinkifierService } from '@app/shared/renderer/linkifier.service' | |
1506307f C |
3 | |
4 | @Injectable() | |
5 | export class HtmlRendererService { | |
6 | ||
7 | constructor (private linkifier: LinkifierService) { | |
8 | ||
9 | } | |
10 | ||
41d71344 C |
11 | async toSafeHtml (text: string) { |
12 | // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function | |
13 | const sanitizeHtml: typeof import ('sanitize-html') = (await import('sanitize-html') as any).default | |
14 | ||
1506307f C |
15 | // Convert possible markdown to html |
16 | const html = this.linkifier.linkify(text) | |
17 | ||
18 | return sanitizeHtml(html, { | |
19 | allowedTags: [ 'a', 'p', 'span', 'br' ], | |
20 | allowedSchemes: [ 'http', 'https' ], | |
21 | allowedAttributes: { | |
22 | 'a': [ 'href', 'class', 'target' ] | |
23 | }, | |
24 | transformTags: { | |
25 | a: (tagName, attribs) => { | |
26 | return { | |
27 | tagName, | |
28 | attribs: Object.assign(attribs, { | |
29 | target: '_blank', | |
30 | rel: 'noopener noreferrer' | |
31 | }) | |
32 | } | |
33 | } | |
34 | } | |
35 | }) | |
36 | } | |
37 | } |