1 import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
3 const sanitizeHtml = require('sanitize-html')
4 const markdownItEmoji = require('markdown-it-emoji/light')
5 const MarkdownItClass = require('markdown-it')
6 const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
8 markdownIt.enable(TEXT_WITH_HTML_RULES)
9 markdownIt.use(markdownItEmoji)
11 const toSafeHtml = text => {
15 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
17 // Convert possible markdown (emojis, emphasis and lists) to html
18 const html = markdownIt.render(textWithLineFeed)
20 // Convert to safe Html
21 return sanitizeHtml(html, SANITIZE_OPTIONS)
24 const mdToPlainText = text => {
27 // Convert possible markdown (emojis, emphasis and lists) to html
28 const html = markdownIt.render(text)
30 // Convert to safe Html
31 const safeHtml = sanitizeHtml(html, SANITIZE_OPTIONS)
33 return safeHtml.replace(/<[^>]+>/g, '')
38 // ---------------------------------------------------------------------------