]>
Commit | Line | Data |
---|---|---|
14aa8556 | 1 | import * as MarkdownIt from 'markdown-it' |
67ed6552 C |
2 | import { buildVideoLink } from 'src/assets/player/utils' |
3 | import { Injectable } from '@angular/core' | |
4 | import { HtmlRendererService } from './html-renderer.service' | |
7e049ea4 C |
5 | |
6 | type MarkdownParsers = { | |
7 | textMarkdownIt: MarkdownIt | |
3131c13e C |
8 | textWithHTMLMarkdownIt: MarkdownIt |
9 | ||
7e049ea4 | 10 | enhancedMarkdownIt: MarkdownIt |
3131c13e C |
11 | enhancedWithHTMLMarkdownIt: MarkdownIt |
12 | ||
7e049ea4 C |
13 | completeMarkdownIt: MarkdownIt |
14 | } | |
15 | ||
16 | type MarkdownConfig = { | |
17 | rules: string[] | |
18 | html: boolean | |
19 | escape?: boolean | |
20 | } | |
21 | ||
22 | type MarkdownParserConfigs = { | |
23 | [id in keyof MarkdownParsers]: MarkdownConfig | |
24 | } | |
9d9597df C |
25 | |
26 | @Injectable() | |
27 | export class MarkdownService { | |
621d99f5 C |
28 | static TEXT_RULES = [ |
29 | 'linkify', | |
30 | 'autolink', | |
31 | 'emphasis', | |
32 | 'link', | |
33 | 'newline', | |
34 | 'list' | |
35 | ] | |
3131c13e C |
36 | static TEXT_WITH_HTML_RULES = MarkdownService.TEXT_RULES.concat([ 'html_inline', 'html_block' ]) |
37 | ||
621d99f5 | 38 | static ENHANCED_RULES = MarkdownService.TEXT_RULES.concat([ 'image' ]) |
3131c13e C |
39 | static ENHANCED_WITH_HTML_RULES = MarkdownService.TEXT_WITH_HTML_RULES.concat([ 'image' ]) |
40 | ||
7e049ea4 C |
41 | static COMPLETE_RULES = MarkdownService.ENHANCED_WITH_HTML_RULES.concat([ 'block', 'inline', 'heading', 'paragraph' ]) |
42 | ||
43 | private markdownParsers: MarkdownParsers = { | |
44 | textMarkdownIt: null, | |
3131c13e | 45 | textWithHTMLMarkdownIt: null, |
7e049ea4 | 46 | enhancedMarkdownIt: null, |
3131c13e | 47 | enhancedWithHTMLMarkdownIt: null, |
7e049ea4 C |
48 | completeMarkdownIt: null |
49 | } | |
50 | private parsersConfig: MarkdownParserConfigs = { | |
51 | textMarkdownIt: { rules: MarkdownService.TEXT_RULES, html: false }, | |
3131c13e C |
52 | textWithHTMLMarkdownIt: { rules: MarkdownService.TEXT_WITH_HTML_RULES, html: true, escape: true }, |
53 | ||
7e049ea4 | 54 | enhancedMarkdownIt: { rules: MarkdownService.ENHANCED_RULES, html: false }, |
3131c13e C |
55 | enhancedWithHTMLMarkdownIt: { rules: MarkdownService.ENHANCED_WITH_HTML_RULES, html: true, escape: true }, |
56 | ||
7e049ea4 C |
57 | completeMarkdownIt: { rules: MarkdownService.COMPLETE_RULES, html: true } |
58 | } | |
621d99f5 | 59 | |
7e049ea4 | 60 | constructor (private htmlRenderer: HtmlRendererService) {} |
9d9597df | 61 | |
0672dc76 | 62 | textMarkdownToHTML (markdown: string, withHtml = false, withEmoji = false) { |
63 | if (withHtml) return this.render('textWithHTMLMarkdownIt', markdown, withEmoji) | |
3131c13e | 64 | |
0672dc76 | 65 | return this.render('textMarkdownIt', markdown, withEmoji) |
7e049ea4 | 66 | } |
66b16caf | 67 | |
0672dc76 | 68 | enhancedMarkdownToHTML (markdown: string, withHtml = false, withEmoji = false) { |
69 | if (withHtml) return this.render('enhancedWithHTMLMarkdownIt', markdown, withEmoji) | |
41d71344 | 70 | |
0672dc76 | 71 | return this.render('enhancedMarkdownIt', markdown, withEmoji) |
07fa4c97 C |
72 | } |
73 | ||
3131c13e | 74 | completeMarkdownToHTML (markdown: string) { |
0672dc76 | 75 | return this.render('completeMarkdownIt', markdown, true) |
7e049ea4 C |
76 | } |
77 | ||
b29bf61d RK |
78 | async processVideoTimestamps (html: string) { |
79 | return html.replace(/((\d{1,2}):)?(\d{1,2}):(\d{1,2})/g, function (str, _, h, m, s) { | |
80 | const t = (3600 * +(h || 0)) + (60 * +(m || 0)) + (+(s || 0)) | |
81 | const url = buildVideoLink({ startTime: t }) | |
82 | return `<a class="video-timestamp" href="${url}">${str}</a>` | |
83 | }) | |
84 | } | |
85 | ||
0672dc76 | 86 | private async render (name: keyof MarkdownParsers, markdown: string, withEmoji = false) { |
53055a11 | 87 | if (!markdown) return '' |
07fa4c97 | 88 | |
7e049ea4 C |
89 | const config = this.parsersConfig[ name ] |
90 | if (!this.markdownParsers[ name ]) { | |
91 | this.markdownParsers[ name ] = await this.createMarkdownIt(config) | |
0672dc76 | 92 | |
93 | if (withEmoji) { | |
04c7f701 | 94 | // TODO: write types |
95 | const emoji = require('markdown-it-emoji/light') | |
96 | this.markdownParsers[ name ].use(emoji) | |
0672dc76 | 97 | } |
41d71344 C |
98 | } |
99 | ||
7e049ea4 C |
100 | let html = this.markdownParsers[ name ].render(markdown) |
101 | html = this.avoidTruncatedTags(html) | |
621d99f5 | 102 | |
7e049ea4 | 103 | if (config.escape) return this.htmlRenderer.toSafeHtml(html) |
1eb23e12 | 104 | |
7e049ea4 | 105 | return html |
1eb23e12 C |
106 | } |
107 | ||
7e049ea4 | 108 | private async createMarkdownIt (config: MarkdownConfig) { |
14aa8556 | 109 | // FIXME: import('...') returns a struct module, containing a "default" field |
41d71344 C |
110 | const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default |
111 | ||
7e049ea4 | 112 | const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true, html: config.html }) |
621d99f5 | 113 | |
7e049ea4 | 114 | for (const rule of config.rules) { |
621d99f5 C |
115 | markdownIt.enable(rule) |
116 | } | |
117 | ||
118 | this.setTargetToLinks(markdownIt) | |
119 | ||
120 | return markdownIt | |
121 | } | |
3d9eaae3 | 122 | |
41d71344 | 123 | private setTargetToLinks (markdownIt: MarkdownIt) { |
9d9597df | 124 | // Snippet from markdown-it documentation: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer |
3d9eaae3 | 125 | const defaultRender = markdownIt.renderer.rules.link_open || function (tokens, idx, options, env, self) { |
9d9597df C |
126 | return self.renderToken(tokens, idx, options) |
127 | } | |
128 | ||
632c5e36 C |
129 | markdownIt.renderer.rules.link_open = function (tokens, index, options, env, self) { |
130 | const token = tokens[index] | |
9d9597df | 131 | |
632c5e36 C |
132 | const targetIndex = token.attrIndex('target') |
133 | if (targetIndex < 0) token.attrPush([ 'target', '_blank' ]) | |
134 | else token.attrs[targetIndex][1] = '_blank' | |
135 | ||
136 | const relIndex = token.attrIndex('rel') | |
137 | if (relIndex < 0) token.attrPush([ 'rel', 'noopener noreferrer' ]) | |
138 | else token.attrs[relIndex][1] = 'noopener noreferrer' | |
9d9597df C |
139 | |
140 | // pass token to default renderer. | |
632c5e36 | 141 | return defaultRender(tokens, index, options, env, self) |
9d9597df C |
142 | } |
143 | } | |
07fa4c97 | 144 | |
8eaa6d62 | 145 | private avoidTruncatedTags (html: string) { |
e182430a C |
146 | return html.replace(/\*\*?([^*]+)$/, '$1') |
147 | .replace(/<a[^>]+>([^<]+)<\/a>\s*...((<\/p>)|(<\/li>)|(<\/strong>))?$/mi, '$1...') | |
b29bf61d | 148 | .replace(/\[[^\]]+\]\(([^\)]+)$/m, '$1') |
f5b72c39 | 149 | .replace(/\s?\[[^\]]+\]?[.]{3}<\/p>$/m, '...</p>') |
07fa4c97 | 150 | } |
9d9597df | 151 | } |