aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/shared/markdown.service.ts
blob: 3ef16fdb980a2824a092a354ee5812eb2de4fb68 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Injectable } from '@angular/core'

import * as MarkdownIt from 'markdown-it'

@Injectable()
export class MarkdownService {
  static TEXT_RULES = [
    'linkify',
    'autolink',
    'emphasis',
    'link',
    'newline',
    'list'
  ]
  static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ])

  private textMarkdownIt: MarkdownIt.MarkdownIt
  private enhancedMarkdownIt: MarkdownIt.MarkdownIt

  constructor () {
    this.textMarkdownIt = this.createMarkdownIt(MarkdownService.TEXT_RULES)
    this.enhancedMarkdownIt = this.createMarkdownIt(MarkdownService.ENHANCED_RULES)
  }

  textMarkdownToHTML (markdown: string) {
    if (!markdown) return ''

    const html = this.textMarkdownIt.render(markdown)
    return this.avoidTruncatedLinks(html)
  }

  enhancedMarkdownToHTML (markdown: string) {
    if (!markdown) return ''

    const html = this.enhancedMarkdownIt.render(markdown)
    return this.avoidTruncatedLinks(html)
  }

  private createMarkdownIt (rules: string[]) {
    const markdownIt = new MarkdownIt('zero', { linkify: true, breaks: true })

    for (let rule of rules) {
      markdownIt.enable(rule)
    }

    this.setTargetToLinks(markdownIt)

    return markdownIt
  }

  private setTargetToLinks (markdownIt: MarkdownIt.MarkdownIt) {
    // Snippet from markdown-it documentation: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
    const defaultRender = markdownIt.renderer.rules.link_open || function (tokens, idx, options, env, self) {
      return self.renderToken(tokens, idx, options)
    }

    markdownIt.renderer.rules.link_open = function (tokens, index, options, env, self) {
      const token = tokens[index]

      const targetIndex = token.attrIndex('target')
      if (targetIndex < 0) token.attrPush([ 'target', '_blank' ])
      else token.attrs[targetIndex][1] = '_blank'

      const relIndex = token.attrIndex('rel')
      if (relIndex < 0) token.attrPush([ 'rel', 'noopener noreferrer' ])
      else token.attrs[relIndex][1] = 'noopener noreferrer'

      // pass token to default renderer.
      return defaultRender(tokens, index, options, env, self)
    }
  }

  private avoidTruncatedLinks (html: string) {
    return html.replace(/<a[^>]+>([^<]+)<\/a>\s*...((<\/p>)|(<\/li>)|(<\/strong>))?$/mi, '$1...')
      .replace(/\[[^\]]+\]?\(?([^\)]+)$/, '$1')
  }
}