]>
Commit | Line | Data |
---|---|---|
1506307f | 1 | import { Injectable } from '@angular/core' |
67ed6552 | 2 | import { LinkifierService } from './linkifier.service' |
2539932e | 3 | import { getCustomMarkupSanitizeOptions, getSanitizeOptions } from '@shared/core-utils/renderer/html' |
1506307f C |
4 | |
5 | @Injectable() | |
6 | export class HtmlRendererService { | |
d573926e | 7 | private sanitizeHtml: typeof import ('sanitize-html') |
1506307f C |
8 | |
9 | constructor (private linkifier: LinkifierService) { | |
10 | ||
11 | } | |
12 | ||
d573926e C |
13 | async convertToBr (text: string) { |
14 | await this.loadSanitizeHtml() | |
15 | ||
16 | const html = text.replace(/\r?\n/g, '<br />') | |
17 | ||
18 | return this.sanitizeHtml(html, { | |
19 | allowedTags: [ 'br' ] | |
20 | }) | |
21 | } | |
22 | ||
2539932e | 23 | async toSafeHtml (text: string, additionalAllowedTags: string[] = []) { |
b355b394 C |
24 | const [ html ] = await Promise.all([ |
25 | // Convert possible markdown to html | |
26 | this.linkifier.linkify(text), | |
41d71344 | 27 | |
b355b394 C |
28 | this.loadSanitizeHtml() |
29 | ]) | |
1506307f | 30 | |
2539932e C |
31 | const options = additionalAllowedTags.length !== 0 |
32 | ? getCustomMarkupSanitizeOptions(additionalAllowedTags) | |
33 | : getSanitizeOptions() | |
34 | ||
35 | return this.sanitizeHtml(html, options) | |
1506307f | 36 | } |
d573926e C |
37 | |
38 | private async loadSanitizeHtml () { | |
39 | // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function | |
40 | this.sanitizeHtml = (await import('sanitize-html') as any).default | |
41 | } | |
1506307f | 42 | } |