]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/renderer/markdown.service.ts
Lazy load emoji
[github/Chocobozzz/PeerTube.git] / client / src / app / core / renderer / markdown.service.ts
index 2ff3de34ed5d0d7cd394af6b85f3461bd0791bd1..edddb0a6621056e00bd88b70f04298d04344ee5a 100644 (file)
@@ -1,7 +1,13 @@
 import * as MarkdownIt from 'markdown-it'
-import MarkdownItEmoji from 'markdown-it-emoji'
 import { buildVideoLink } from 'src/assets/player/utils'
 import { Injectable } from '@angular/core'
+import {
+  COMPLETE_RULES,
+  ENHANCED_RULES,
+  ENHANCED_WITH_HTML_RULES,
+  TEXT_RULES,
+  TEXT_WITH_HTML_RULES
+} from '@shared/core-utils/renderer/markdown'
 import { HtmlRendererService } from './html-renderer.service'
 
 type MarkdownParsers = {
@@ -26,21 +32,6 @@ type MarkdownParserConfigs = {
 
 @Injectable()
 export class MarkdownService {
-  static TEXT_RULES = [
-    'linkify',
-    'autolink',
-    'emphasis',
-    'link',
-    'newline',
-    'list'
-  ]
-  static TEXT_WITH_HTML_RULES = MarkdownService.TEXT_RULES.concat([ 'html_inline', 'html_block' ])
-
-  static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ])
-  static ENHANCED_WITH_HTML_RULES = MarkdownService.TEXT_WITH_HTML_RULES.concat([ 'image' ])
-
-  static COMPLETE_RULES = MarkdownService.ENHANCED_WITH_HTML_RULES.concat([ 'block', 'inline', 'heading', 'paragraph' ])
-
   private markdownParsers: MarkdownParsers = {
     textMarkdownIt: null,
     textWithHTMLMarkdownIt: null,
@@ -49,15 +40,17 @@ export class MarkdownService {
     completeMarkdownIt: null
   }
   private parsersConfig: MarkdownParserConfigs = {
-    textMarkdownIt: { rules: MarkdownService.TEXT_RULES, html: false },
-    textWithHTMLMarkdownIt: { rules: MarkdownService.TEXT_WITH_HTML_RULES, html: true, escape: true },
+    textMarkdownIt: { rules: TEXT_RULES, html: false },
+    textWithHTMLMarkdownIt: { rules: TEXT_WITH_HTML_RULES, html: true, escape: true },
 
-    enhancedMarkdownIt: { rules: MarkdownService.ENHANCED_RULES, html: false },
-    enhancedWithHTMLMarkdownIt: { rules: MarkdownService.ENHANCED_WITH_HTML_RULES, html: true, escape: true },
+    enhancedMarkdownIt: { rules: ENHANCED_RULES, html: false },
+    enhancedWithHTMLMarkdownIt: { rules: ENHANCED_WITH_HTML_RULES, html: true, escape: true },
 
-    completeMarkdownIt: { rules: MarkdownService.COMPLETE_RULES, html: true }
+    completeMarkdownIt: { rules: COMPLETE_RULES, html: true }
   }
 
+  private emojiModule: any
+
   constructor (private htmlRenderer: HtmlRendererService) {}
 
   textMarkdownToHTML (markdown: string, withHtml = false, withEmoji = false) {
@@ -92,7 +85,11 @@ export class MarkdownService {
       this.markdownParsers[ name ] = await this.createMarkdownIt(config)
 
       if (withEmoji) {
-        this.markdownParsers[ name ].use(MarkdownItEmoji)
+        if (!this.emojiModule) {
+          this.emojiModule = (await import('markdown-it-emoji/light')).default
+        }
+
+        this.markdownParsers[ name ].use(this.emojiModule)
       }
     }