1 import { Injectable } from '@angular/core'
2 import { LinkifierService } from './linkifier.service'
5 export class HtmlRendererService {
6 private sanitizeHtml: typeof import ('sanitize-html')
8 constructor (private linkifier: LinkifierService) {
12 async convertToBr (text: string) {
13 await this.loadSanitizeHtml()
15 const html = text.replace(/\r?\n/g, '<br />')
17 return this.sanitizeHtml(html, {
22 async toSafeHtml (text: string) {
23 await this.loadSanitizeHtml()
25 // Convert possible markdown to html
26 const html = this.linkifier.linkify(text)
28 return this.sanitizeHtml(html, {
29 allowedTags: [ 'a', 'p', 'span', 'br', 'strong', 'em', 'ul', 'ol', 'li' ],
30 allowedSchemes: [ 'http', 'https' ],
32 'a': [ 'href', 'class', 'target', 'rel' ]
35 a: (tagName, attribs) => {
36 let rel = 'noopener noreferrer'
37 if (attribs.rel === 'me') rel += ' me'
41 attribs: Object.assign(attribs, {
51 private async loadSanitizeHtml () {
52 // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
53 this.sanitizeHtml = (await import('sanitize-html') as any).default