]> 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 09bbbfb7ef863f1752a954f0c0482cbe36abc32b..f0c87326f4defc13f7e2987a1db3f188839e6e6f 100644 (file)
@@ -1,12 +1,15 @@
 import { Injectable } from '@angular/core'
-import { MarkdownIt } from 'markdown-it'
+import { buildVideoLink } from '../../../assets/player/utils'
 import { HtmlRendererService } from '@app/shared/renderer/html-renderer.service'
-import { mark } from '@angular/compiler-cli/src/ngtsc/perf/src/clock'
+import * as MarkdownIt from 'markdown-it'
 
 type MarkdownParsers = {
   textMarkdownIt: MarkdownIt
+  textWithHTMLMarkdownIt: MarkdownIt
+
   enhancedMarkdownIt: MarkdownIt
-  enhancedMarkdownWithHTMLIt: MarkdownIt
+  enhancedWithHTMLMarkdownIt: MarkdownIt
+
   completeMarkdownIt: MarkdownIt
 }
 
@@ -30,39 +33,56 @@ 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.ENHANCED_RULES.concat([ 'html_inline', 'html_block' ])
+  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,
     enhancedMarkdownIt: null,
-    enhancedMarkdownWithHTMLIt: 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 },
+
     enhancedMarkdownIt: { rules: MarkdownService.ENHANCED_RULES, html: false },
-    enhancedMarkdownWithHTMLIt: { rules: MarkdownService.ENHANCED_WITH_HTML_RULES, html: true, escape: true },
+    enhancedWithHTMLMarkdownIt: { rules: MarkdownService.ENHANCED_WITH_HTML_RULES, html: true, escape: true },
+
     completeMarkdownIt: { rules: MarkdownService.COMPLETE_RULES, html: true }
   }
 
   constructor (private htmlRenderer: HtmlRendererService) {}
 
-  textMarkdownToHTML (markdown: string) {
+  textMarkdownToHTML (markdown: string, withHtml = false) {
+    if (withHtml) return this.render('textWithHTMLMarkdownIt', markdown)
+
     return this.render('textMarkdownIt', markdown)
   }
 
-  async enhancedMarkdownToHTML (markdown: string, withHtml = false) {
-    if (withHtml) return this.render('enhancedMarkdownWithHTMLIt', markdown)
+  enhancedMarkdownToHTML (markdown: string, withHtml = false) {
+    if (withHtml) return this.render('enhancedWithHTMLMarkdownIt', markdown)
 
     return this.render('enhancedMarkdownIt', markdown)
   }
 
-  async completeMarkdownToHTML (markdown: string) {
+  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 ''
 
@@ -74,15 +94,13 @@ export class MarkdownService {
     let html = this.markdownParsers[ name ].render(markdown)
     html = this.avoidTruncatedTags(html)
 
-    console.log(html)
-
     if (config.escape) return this.htmlRenderer.toSafeHtml(html)
 
     return html
   }
 
   private async createMarkdownIt (config: MarkdownConfig) {
-    // FIXME: import('...') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
+    // 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, html: config.html })