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