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