]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/renderer/html-renderer.service.ts
Lazy load emoji
[github/Chocobozzz/PeerTube.git] / client / src / app / core / renderer / html-renderer.service.ts
index f0527c759355312477af13bcae6e4e3ad77771f8..3176cf6a4c21795179a9234161712a168cb8a43a 100644 (file)
@@ -1,40 +1,38 @@
 import { Injectable } from '@angular/core'
 import { LinkifierService } from './linkifier.service'
+import { SANITIZE_OPTIONS } from '@shared/core-utils/renderer/html'
 
 @Injectable()
 export class HtmlRendererService {
+  private sanitizeHtml: typeof import ('sanitize-html')
 
   constructor (private linkifier: LinkifierService) {
 
   }
 
+  async convertToBr (text: string) {
+    await this.loadSanitizeHtml()
+
+    const html = text.replace(/\r?\n/g, '<br />')
+
+    return this.sanitizeHtml(html, {
+      allowedTags: [ 'br' ]
+    })
+  }
+
   async toSafeHtml (text: string) {
+    const [ html ] = await Promise.all([
+      // Convert possible markdown to html
+      this.linkifier.linkify(text),
+
+      this.loadSanitizeHtml()
+    ])
+
+    return this.sanitizeHtml(html, SANITIZE_OPTIONS)
+  }
+
+  private async loadSanitizeHtml () {
     // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
-    const sanitizeHtml: typeof import ('sanitize-html') = (await import('sanitize-html') as any).default
-
-    // Convert possible markdown to html
-    const html = this.linkifier.linkify(text)
-
-    return sanitizeHtml(html, {
-      allowedTags: [ 'a', 'p', 'span', 'br', 'strong', 'em', 'ul', 'ol', 'li' ],
-      allowedSchemes: [ 'http', 'https' ],
-      allowedAttributes: {
-        'a': [ 'href', 'class', 'target', 'rel' ]
-      },
-      transformTags: {
-        a: (tagName, attribs) => {
-          let rel = 'noopener noreferrer'
-          if (attribs.rel === 'me') rel += ' me'
-
-          return {
-            tagName,
-            attribs: Object.assign(attribs, {
-              target: '_blank',
-              rel
-            })
-          }
-        }
-      }
-    })
+    this.sanitizeHtml = (await import('sanitize-html') as any).default
   }
 }