]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/shared/renderer/markdown.service.ts
provide specific engine boundaries for nodejs and yarn
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / renderer / markdown.service.ts
index 9a90663511c850767e91f2e5755b3f292ae6656a..f0c87326f4defc13f7e2987a1db3f188839e6e6f 100644 (file)
@@ -1,6 +1,27 @@
 import { Injectable } from '@angular/core'
+import { buildVideoLink } from '../../../assets/player/utils'
+import { HtmlRendererService } from '@app/shared/renderer/html-renderer.service'
+import * as MarkdownIt from 'markdown-it'
 
-import { MarkdownIt } from 'markdown-it'
+type MarkdownParsers = {
+  textMarkdownIt: MarkdownIt
+  textWithHTMLMarkdownIt: MarkdownIt
+
+  enhancedMarkdownIt: MarkdownIt
+  enhancedWithHTMLMarkdownIt: MarkdownIt
+
+  completeMarkdownIt: MarkdownIt
+}
+
+type MarkdownConfig = {
+  rules: string[]
+  html: boolean
+  escape?: boolean
+}
+
+type MarkdownParserConfigs = {
+  [id in keyof MarkdownParsers]: MarkdownConfig
+}
 
 @Injectable()
 export class MarkdownService {
@@ -12,40 +33,79 @@ export class MarkdownService {
     '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' ])
 
-  private textMarkdownIt: MarkdownIt
-  private enhancedMarkdownIt: MarkdownIt
+  static COMPLETE_RULES = MarkdownService.ENHANCED_WITH_HTML_RULES.concat([ 'block', 'inline', 'heading', 'paragraph' ])
 
-  async textMarkdownToHTML (markdown: string) {
-    if (!markdown) return ''
+  private markdownParsers: MarkdownParsers = {
+    textMarkdownIt: null,
+    textWithHTMLMarkdownIt: null,
+    enhancedMarkdownIt: null,
+    enhancedWithHTMLMarkdownIt: null,
+    completeMarkdownIt: null
+  }
+  private parsersConfig: MarkdownParserConfigs = {
+    textMarkdownIt: { rules: MarkdownService.TEXT_RULES, html: false },
+    textWithHTMLMarkdownIt: { rules: MarkdownService.TEXT_WITH_HTML_RULES, html: true, escape: true },
 
-    if (!this.textMarkdownIt) {
-      this.textMarkdownIt = await this.createMarkdownIt(MarkdownService.TEXT_RULES)
-    }
+    enhancedMarkdownIt: { rules: MarkdownService.ENHANCED_RULES, html: false },
+    enhancedWithHTMLMarkdownIt: { rules: MarkdownService.ENHANCED_WITH_HTML_RULES, html: true, escape: true },
 
-    const html = this.textMarkdownIt.render(markdown)
-    return this.avoidTruncatedTags(html)
+    completeMarkdownIt: { rules: MarkdownService.COMPLETE_RULES, html: true }
   }
 
-  async enhancedMarkdownToHTML (markdown: string) {
+  constructor (private htmlRenderer: HtmlRendererService) {}
+
+  textMarkdownToHTML (markdown: string, withHtml = false) {
+    if (withHtml) return this.render('textWithHTMLMarkdownIt', markdown)
+
+    return this.render('textMarkdownIt', markdown)
+  }
+
+  enhancedMarkdownToHTML (markdown: string, withHtml = false) {
+    if (withHtml) return this.render('enhancedWithHTMLMarkdownIt', markdown)
+
+    return this.render('enhancedMarkdownIt', markdown)
+  }
+
+  completeMarkdownToHTML (markdown: string) {
+    return this.render('completeMarkdownIt', markdown)
+  }
+
+  async processVideoTimestamps (html: string) {
+    return html.replace(/((\d{1,2}):)?(\d{1,2}):(\d{1,2})/g, function (str, _, h, m, s) {
+      const t = (3600 * +(h || 0)) + (60 * +(m || 0)) + (+(s || 0))
+      const url = buildVideoLink({ startTime: t })
+      return `<a class="video-timestamp" href="${url}">${str}</a>`
+    })
+  }
+
+  private async render (name: keyof MarkdownParsers, markdown: string) {
     if (!markdown) return ''
 
-    if (!this.enhancedMarkdownIt) {
-      this.enhancedMarkdownIt = await this.createMarkdownIt(MarkdownService.ENHANCED_RULES)
+    const config = this.parsersConfig[ name ]
+    if (!this.markdownParsers[ name ]) {
+      this.markdownParsers[ name ] = await this.createMarkdownIt(config)
     }
 
-    const html = this.enhancedMarkdownIt.render(markdown)
-    return this.avoidTruncatedTags(html)
+    let html = this.markdownParsers[ name ].render(markdown)
+    html = this.avoidTruncatedTags(html)
+
+    if (config.escape) return this.htmlRenderer.toSafeHtml(html)
+
+    return html
   }
 
-  private async createMarkdownIt (rules: string[]) {
-    // FIXME: import('..') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
+  private async createMarkdownIt (config: MarkdownConfig) {
+    // FIXME: import('...') returns a struct module, containing a "default" field
     const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default
 
-    const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true })
+    const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true, html: config.html })
 
-    for (const rule of rules) {
+    for (const rule of config.rules) {
       markdownIt.enable(rule)
     }
 
@@ -79,7 +139,7 @@ export class MarkdownService {
   private avoidTruncatedTags (html: string) {
     return html.replace(/\*\*?([^*]+)$/, '$1')
       .replace(/<a[^>]+>([^<]+)<\/a>\s*...((<\/p>)|(<\/li>)|(<\/strong>))?$/mi, '$1...')
-      .replace(/\[[^\]]+\]?\(?([^\)]+)$/, '$1')
-
+      .replace(/\[[^\]]+\]\(([^\)]+)$/m, '$1')
+      .replace(/\s?\[[^\]]+\]?[.]{3}<\/p>$/m, '...</p>')
   }
 }