]>
Commit | Line | Data |
---|---|---|
1 | import { Injectable } from '@angular/core' | |
2 | import { getCustomMarkupSanitizeOptions, getDefaultSanitizeOptions } from '@shared/core-utils/renderer/html' | |
3 | import { LinkifierService } from './linkifier.service' | |
4 | ||
5 | @Injectable() | |
6 | export class HtmlRendererService { | |
7 | private sanitizeHtml: typeof import ('sanitize-html') | |
8 | ||
9 | constructor (private linkifier: LinkifierService) { | |
10 | ||
11 | } | |
12 | ||
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 | ||
23 | async toSafeHtml (text: string, additionalAllowedTags: string[] = []) { | |
24 | const [ html ] = await Promise.all([ | |
25 | // Convert possible markdown to html | |
26 | this.linkifier.linkify(text), | |
27 | ||
28 | this.loadSanitizeHtml() | |
29 | ]) | |
30 | ||
31 | const options = additionalAllowedTags.length !== 0 | |
32 | ? getCustomMarkupSanitizeOptions(additionalAllowedTags) | |
33 | : getDefaultSanitizeOptions() | |
34 | ||
35 | return this.sanitizeHtml(html, options) | |
36 | } | |
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 | } | |
42 | } |