Reorganize client shared modules
[oweals/peertube.git] / client / src / app / core / renderer / html-renderer.service.ts
1 import { Injectable } from '@angular/core'
2 import { LinkifierService } from './linkifier.service'
3
4 @Injectable()
5 export class HtmlRendererService {
6
7   constructor (private linkifier: LinkifierService) {
8
9   }
10
11   async toSafeHtml (text: string) {
12     // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
13     const sanitizeHtml: typeof import ('sanitize-html') = (await import('sanitize-html') as any).default
14
15     // Convert possible markdown to html
16     const html = this.linkifier.linkify(text)
17
18     return sanitizeHtml(html, {
19       allowedTags: [ 'a', 'p', 'span', 'br', 'strong', 'em', 'ul', 'ol', 'li' ],
20       allowedSchemes: [ 'http', 'https' ],
21       allowedAttributes: {
22         'a': [ 'href', 'class', 'target', 'rel' ]
23       },
24       transformTags: {
25         a: (tagName, attribs) => {
26           let rel = 'noopener noreferrer'
27           if (attribs.rel === 'me') rel += ' me'
28
29           return {
30             tagName,
31             attribs: Object.assign(attribs, {
32               target: '_blank',
33               rel
34             })
35           }
36         }
37       }
38     })
39   }
40 }