]>
Commit | Line | Data |
---|---|---|
1 | import { Injectable } from '@angular/core' | |
2 | ||
3 | import { MarkdownIt } from 'markdown-it' | |
4 | ||
5 | @Injectable() | |
6 | export class MarkdownService { | |
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' ]) | |
16 | static COMPLETE_RULES = MarkdownService.ENHANCED_RULES.concat([ 'block', 'inline', 'heading', 'html_inline', 'html_block', 'paragraph' ]) | |
17 | ||
18 | private textMarkdownIt: MarkdownIt | |
19 | private enhancedMarkdownIt: MarkdownIt | |
20 | private completeMarkdownIt: MarkdownIt | |
21 | ||
22 | async textMarkdownToHTML (markdown: string) { | |
23 | if (!markdown) return '' | |
24 | ||
25 | if (!this.textMarkdownIt) { | |
26 | this.textMarkdownIt = await this.createMarkdownIt(MarkdownService.TEXT_RULES) | |
27 | } | |
28 | ||
29 | const html = this.textMarkdownIt.render(markdown) | |
30 | return this.avoidTruncatedTags(html) | |
31 | } | |
32 | ||
33 | async enhancedMarkdownToHTML (markdown: string) { | |
34 | if (!markdown) return '' | |
35 | ||
36 | if (!this.enhancedMarkdownIt) { | |
37 | this.enhancedMarkdownIt = await this.createMarkdownIt(MarkdownService.ENHANCED_RULES) | |
38 | } | |
39 | ||
40 | const html = this.enhancedMarkdownIt.render(markdown) | |
41 | return this.avoidTruncatedTags(html) | |
42 | } | |
43 | ||
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 | |
57 | const MarkdownItClass: typeof import ('markdown-it') = (await import('markdown-it') as any).default | |
58 | ||
59 | const markdownIt = new MarkdownItClass('zero', { linkify: true, breaks: true, html }) | |
60 | ||
61 | for (const rule of rules) { | |
62 | markdownIt.enable(rule) | |
63 | } | |
64 | ||
65 | this.setTargetToLinks(markdownIt) | |
66 | ||
67 | return markdownIt | |
68 | } | |
69 | ||
70 | private setTargetToLinks (markdownIt: MarkdownIt) { | |
71 | // Snippet from markdown-it documentation: https://github.com/markdown-it/markdown-it/blob/master/docs/architecture.md#renderer | |
72 | const defaultRender = markdownIt.renderer.rules.link_open || function (tokens, idx, options, env, self) { | |
73 | return self.renderToken(tokens, idx, options) | |
74 | } | |
75 | ||
76 | markdownIt.renderer.rules.link_open = function (tokens, index, options, env, self) { | |
77 | const token = tokens[index] | |
78 | ||
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' | |
86 | ||
87 | // pass token to default renderer. | |
88 | return defaultRender(tokens, index, options, env, self) | |
89 | } | |
90 | } | |
91 | ||
92 | private avoidTruncatedTags (html: string) { | |
93 | return html.replace(/\*\*?([^*]+)$/, '$1') | |
94 | .replace(/<a[^>]+>([^<]+)<\/a>\s*...((<\/p>)|(<\/li>)|(<\/strong>))?$/mi, '$1...') | |
95 | .replace(/\[[^\]]+\]?\(?([^\)]+)$/, '$1') | |
96 | ||
97 | } | |
98 | } |