]>
Commit | Line | Data |
---|---|---|
2539932e C |
1 | import { getSanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils' |
2 | ||
3 | const sanitizeOptions = getSanitizeOptions() | |
84bced65 RK |
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 => { | |
46460547 C |
14 | if (!text) return '' |
15 | ||
84bced65 RK |
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 | |
2539932e | 23 | return sanitizeHtml(html, sanitizeOptions) |
84bced65 RK |
24 | } |
25 | ||
a073c912 | 26 | const mdToPlainText = text => { |
46460547 C |
27 | if (!text) return '' |
28 | ||
a073c912 RK |
29 | // Convert possible markdown (emojis, emphasis and lists) to html |
30 | const html = markdownIt.render(text) | |
31 | ||
32 | // Convert to safe Html | |
2539932e | 33 | const safeHtml = sanitizeHtml(html, sanitizeOptions) |
a073c912 RK |
34 | |
35 | return safeHtml.replace(/<[^>]+>/g, '') | |
36 | .replace(/\n$/, '') | |
4d695236 | 37 | .replace(/\n/g, ', ') |
a073c912 RK |
38 | } |
39 | ||
84bced65 RK |
40 | // --------------------------------------------------------------------------- |
41 | ||
42 | export { | |
a073c912 RK |
43 | toSafeHtml, |
44 | mdToPlainText | |
84bced65 | 45 | } |