2021-05-27 15:59:55 +02:00
|
|
|
import { getSanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
|
|
|
|
|
|
|
|
const sanitizeOptions = getSanitizeOptions()
|
2021-04-11 15:06:36 +02:00
|
|
|
|
|
|
|
const sanitizeHtml = require('sanitize-html')
|
|
|
|
const markdownItEmoji = require('markdown-it-emoji/light')
|
|
|
|
const MarkdownItClass = require('markdown-it')
|
|
|
|
const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
|
|
|
|
|
|
|
|
markdownIt.enable(TEXT_WITH_HTML_RULES)
|
|
|
|
markdownIt.use(markdownItEmoji)
|
|
|
|
|
|
|
|
const toSafeHtml = text => {
|
2021-04-16 07:41:35 +02:00
|
|
|
if (!text) return ''
|
|
|
|
|
2021-04-11 15:06:36 +02:00
|
|
|
// Restore line feed
|
|
|
|
const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
|
|
|
|
|
|
|
|
// Convert possible markdown (emojis, emphasis and lists) to html
|
|
|
|
const html = markdownIt.render(textWithLineFeed)
|
|
|
|
|
|
|
|
// Convert to safe Html
|
2021-05-27 15:59:55 +02:00
|
|
|
return sanitizeHtml(html, sanitizeOptions)
|
2021-04-11 15:06:36 +02:00
|
|
|
}
|
|
|
|
|
2021-04-12 11:43:29 +02:00
|
|
|
const mdToPlainText = text => {
|
2021-04-16 07:41:35 +02:00
|
|
|
if (!text) return ''
|
|
|
|
|
2021-04-12 11:43:29 +02:00
|
|
|
// Convert possible markdown (emojis, emphasis and lists) to html
|
|
|
|
const html = markdownIt.render(text)
|
|
|
|
|
|
|
|
// Convert to safe Html
|
2021-05-27 15:59:55 +02:00
|
|
|
const safeHtml = sanitizeHtml(html, sanitizeOptions)
|
2021-04-12 11:43:29 +02:00
|
|
|
|
|
|
|
return safeHtml.replace(/<[^>]+>/g, '')
|
|
|
|
.replace(/\n$/, '')
|
2021-10-11 14:15:42 +02:00
|
|
|
.replace(/\n/g, ', ')
|
2021-04-12 11:43:29 +02:00
|
|
|
}
|
|
|
|
|
2021-04-11 15:06:36 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2021-04-12 11:43:29 +02:00
|
|
|
toSafeHtml,
|
|
|
|
mdToPlainText
|
2021-04-11 15:06:36 +02:00
|
|
|
}
|