1 import { Injectable } from '@angular/core'
2 import { LinkifierService } from './linkifier.service'
3 import { SANITIZE_OPTIONS } from '@shared/core-utils/renderer/html'
6 export class HtmlRendererService {
7 private sanitizeHtml: typeof import ('sanitize-html')
9 constructor (private linkifier: LinkifierService) {
13 async convertToBr (text: string) {
14 await this.loadSanitizeHtml()
16 const html = text.replace(/\r?\n/g, '<br />')
18 return this.sanitizeHtml(html, {
23 async toSafeHtml (text: string) {
24 const [ html ] = await Promise.all([
25 // Convert possible markdown to html
26 this.linkifier.linkify(text),
28 this.loadSanitizeHtml()
31 return this.sanitizeHtml(html, SANITIZE_OPTIONS)
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