]>
Commit | Line | Data |
---|---|---|
1506307f | 1 | import { Injectable } from '@angular/core' |
67ed6552 | 2 | import { LinkifierService } from './linkifier.service' |
9ff36c2d | 3 | import { SANITIZE_OPTIONS } 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 | ||
41d71344 | 23 | async toSafeHtml (text: 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 | |
9ff36c2d | 31 | return this.sanitizeHtml(html, SANITIZE_OPTIONS) |
1506307f | 32 | } |
d573926e C |
33 | |
34 | private async loadSanitizeHtml () { | |
35 | // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function | |
36 | this.sanitizeHtml = (await import('sanitize-html') as any).default | |
37 | } | |
1506307f | 38 | } |