aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos/shared/markdown.service.ts
blob: a275446eba89aa0c2ee95b625f87f6c526d1b575 (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
import { Injectable } from '@angular/core'

import * as MarkdownIt from 'markdown-it'

@Injectable()
export class MarkdownService {
  private markdownIt: MarkdownIt.MarkdownIt
  private linkifier: MarkdownIt.MarkdownIt

  constructor () {
    this.markdownIt = new MarkdownIt('zero', { linkify: true, breaks: true })
      .enable('linkify')
      .enable('autolink')
      .enable('emphasis')
      .enable('link')
      .enable('newline')
      .enable('list')
    this.setTargetToLinks(this.markdownIt)

    this.linkifier = new MarkdownIt('zero', { linkify: true })
      .enable('linkify')
    this.setTargetToLinks(this.linkifier)
  }

  markdownToHTML (markdown: string) {
    const html = this.markdownIt.render(markdown)

    // Avoid linkify truncated links
    return html.replace(/<a[^>]+>([^<]+)<\/a>\s*...(<\/p>)?$/mi, '$1...')
  }

  linkify (text: string) {
    return this.linkifier.render(text)
  }

  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, idx, options, env, self) {
      // If you are sure other plugins can't add `target` - drop check below
      const aIndex = tokens[idx].attrIndex('target')

      if (aIndex < 0) {
        tokens[idx].attrPush(['target', '_blank']) // add new attribute
      } else {
        tokens[idx].attrs[aIndex][1] = '_blank'    // replace value of existing attr
      }

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