]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/renderer/markdown.service.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / renderer / markdown.service.ts
CommitLineData
9d9597df
C
1import { Injectable } from '@angular/core'
2
41d71344 3import { MarkdownIt } from 'markdown-it'
9d9597df
C
4
5@Injectable()
6export class MarkdownService {
621d99f5
C
7 static TEXT_RULES = [
8 'linkify',
9 'autolink',
10 'emphasis',
11 'link',
12 'newline',
13 'list'
14 ]
15 static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ])
1eb23e12 16 static COMPLETE_RULES = MarkdownService.ENHANCED_RULES.concat([ 'block', 'inline', 'heading', 'html_inline', 'html_block', 'paragraph' ])
621d99f5 17
41d71344
C
18 private textMarkdownIt: MarkdownIt
19 private enhancedMarkdownIt: MarkdownIt
1eb23e12 20 private completeMarkdownIt: MarkdownIt
9d9597df 21
41d71344 22 async textMarkdownToHTML (markdown: string) {
53055a11 23 if (!markdown) return ''
66b16caf 24
41d71344
C
25 if (!this.textMarkdownIt) {
26 this.textMarkdownIt = await this.createMarkdownIt(MarkdownService.TEXT_RULES)
27 }
28
53055a11 29 const html = this.textMarkdownIt.render(markdown)
8eaa6d62 30 return this.avoidTruncatedTags(html)
07fa4c97
C
31 }
32
41d71344 33 async enhancedMarkdownToHTML (markdown: string) {
53055a11 34 if (!markdown) return ''
07fa4c97 35
41d71344
C
36 if (!this.enhancedMarkdownIt) {
37 this.enhancedMarkdownIt = await this.createMarkdownIt(MarkdownService.ENHANCED_RULES)
38 }
39
53055a11 40 const html = this.enhancedMarkdownIt.render(markdown)
8eaa6d62 41 return this.avoidTruncatedTags(html)
66b16caf 42 }
621d99f5 43
1eb23e12
C
44 async completeMarkdownToHTML (markdown: string) {
45 if (!markdown) return ''
46
47 if (!this.completeMarkdownIt) {
48 this.completeMarkdownIt = await this.createMarkdownIt(MarkdownService.COMPLETE_RULES, true)
49 }
50
51 const html = this.completeMarkdownIt.render(markdown)
52 return this.avoidTruncatedTags(html)
53 }
54
55 private async createMarkdownIt (rules: string[], html = false) {
56 // FIXME: import('...') returns a struct module, containing a "default" field corresponding to our sanitizeHtml function
41d71344
C
57 const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default
58
1eb23e12 59 const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true, html })
621d99f5 60
c4710631 61 for (const rule of rules) {
621d99f5
C
62 markdownIt.enable(rule)
63 }
64
65 this.setTargetToLinks(markdownIt)
66
67 return markdownIt
68 }
3d9eaae3 69
41d71344 70 private setTargetToLinks (markdownIt: MarkdownIt) {
9d9597df 71 // Snippet from markdown-it documentation: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer
3d9eaae3 72 const defaultRender = markdownIt.renderer.rules.link_open || function (tokens, idx, options, env, self) {
9d9597df
C
73 return self.renderToken(tokens, idx, options)
74 }
75
632c5e36
C
76 markdownIt.renderer.rules.link_open = function (tokens, index, options, env, self) {
77 const token = tokens[index]
9d9597df 78
632c5e36
C
79 const targetIndex = token.attrIndex('target')
80 if (targetIndex < 0) token.attrPush([ 'target', '_blank' ])
81 else token.attrs[targetIndex][1] = '_blank'
82
83 const relIndex = token.attrIndex('rel')
84 if (relIndex < 0) token.attrPush([ 'rel', 'noopener noreferrer' ])
85 else token.attrs[relIndex][1] = 'noopener noreferrer'
9d9597df
C
86
87 // pass token to default renderer.
632c5e36 88 return defaultRender(tokens, index, options, env, self)
9d9597df
C
89 }
90 }
07fa4c97 91
8eaa6d62 92 private avoidTruncatedTags (html: string) {
e182430a
C
93 return html.replace(/\*\*?([^*]+)$/, '$1')
94 .replace(/<a[^>]+>([^<]+)<\/a>\s*...((<\/p>)|(<\/li>)|(<\/strong>))?$/mi, '$1...')
040467f5 95 .replace(/\[[^\]]+\]?\(?([^\)]+)$/, '$1')
e182430a 96
07fa4c97 97 }
9d9597df 98}