1 import { getSanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
3 const sanitizeOptions = getSanitizeOptions()
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 })
10 markdownIt.enable(TEXT_WITH_HTML_RULES)
11 markdownIt.use(markdownItEmoji)
13 const toSafeHtml = text => {
17 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
19 // Convert possible markdown (emojis, emphasis and lists) to html
20 const html = markdownIt.render(textWithLineFeed)
22 // Convert to safe Html
23 return sanitizeHtml(html, sanitizeOptions)
26 const mdToPlainText = text => {
29 // Convert possible markdown (emojis, emphasis and lists) to html
30 const html = markdownIt.render(text)
32 // Convert to safe Html
33 const safeHtml = sanitizeHtml(html, sanitizeOptions)
35 return safeHtml.replace(/<[^>]+>/g, '')
40 // ---------------------------------------------------------------------------