]>
Commit | Line | Data |
---|---|---|
1 | import { Injectable } from '@angular/core' | |
2 | import { LinkifierService } from './linkifier.service' | |
3 | import { SANITIZE_OPTIONS } from '@shared/core-utils/renderer/html' | |
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) { | |
24 | await this.loadSanitizeHtml() | |
25 | ||
26 | // Convert possible markdown to html | |
27 | const html = this.linkifier.linkify(text) | |
28 | ||
29 | return this.sanitizeHtml(html, SANITIZE_OPTIONS) | |
30 | } | |
31 | ||
32 | private async loadSanitizeHtml () { | |
33 | // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function | |
34 | this.sanitizeHtml = (await import('sanitize-html') as any).default | |
35 | } | |
36 | } |