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