]>
Commit | Line | Data |
---|---|---|
1 | import { getSanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils' | |
2 | ||
3 | const sanitizeOptions = getSanitizeOptions() | |
4 | ||
5 | const sanitizeHtml = require('sanitize-html') | |
6 | const markdownItEmoji = require('markdown-it-emoji/light') | |
7 | const MarkdownItClass = require('markdown-it') | |
8 | const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true }) | |
9 | ||
10 | markdownIt.enable(TEXT_WITH_HTML_RULES) | |
11 | markdownIt.use(markdownItEmoji) | |
12 | ||
13 | const toSafeHtml = text => { | |
14 | if (!text) return '' | |
15 | ||
16 | // Restore line feed | |
17 | const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n') | |
18 | ||
19 | // Convert possible markdown (emojis, emphasis and lists) to html | |
20 | const html = markdownIt.render(textWithLineFeed) | |
21 | ||
22 | // Convert to safe Html | |
23 | return sanitizeHtml(html, sanitizeOptions) | |
24 | } | |
25 | ||
26 | const mdToPlainText = text => { | |
27 | if (!text) return '' | |
28 | ||
29 | // Convert possible markdown (emojis, emphasis and lists) to html | |
30 | const html = markdownIt.render(text) | |
31 | ||
32 | // Convert to safe Html | |
33 | const safeHtml = sanitizeHtml(html, sanitizeOptions) | |
34 | ||
35 | return safeHtml.replace(/<[^>]+>/g, '') | |
36 | .replace(/\n$/, '') | |
37 | .replace(/\n/g, ', ') | |
38 | } | |
39 | ||
40 | // --------------------------------------------------------------------------- | |
41 | ||
42 | export { | |
43 | toSafeHtml, | |
44 | mdToPlainText | |
45 | } |