1 import { getDefaultSanitizeOptions, getTextOnlySanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
3 const defaultSanitizeOptions = getDefaultSanitizeOptions()
4 const textOnlySanitizeOptions = getTextOnlySanitizeOptions()
6 const sanitizeHtml = require('sanitize-html')
7 const markdownItEmoji = require('markdown-it-emoji/light')
8 const MarkdownItClass = require('markdown-it')
10 const markdownItForSafeHtml = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
11 .enable(TEXT_WITH_HTML_RULES)
14 const markdownItForPlainText = new MarkdownItClass('default', { linkify: false, breaks: true, html: false })
18 const toSafeHtml = (text: string) => {
22 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
24 // Convert possible markdown (emojis, emphasis and lists) to html
25 const html = markdownItForSafeHtml.render(textWithLineFeed)
27 // Convert to safe Html
28 return sanitizeHtml(html, defaultSanitizeOptions)
31 const mdToOneLinePlainText = (text: string) => {
34 markdownItForPlainText.render(text)
36 // Convert to safe Html
37 return sanitizeHtml(markdownItForPlainText.plainText, textOnlySanitizeOptions)
40 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 // Thanks: https://github.com/wavesheep/markdown-it-plain-text
50 function plainTextPlugin (markdownIt: any) {
51 function plainTextRule (state: any) {
52 const text = scan(state.tokens)
54 markdownIt.plainText = text
57 function scan (tokens: any[]) {
58 let lastSeparator = ''
61 function buildSeparator (token: any) {
62 if (token.type === 'list_item_close') {
66 if (token.tag === 'br' || token.type === 'paragraph_close') {
71 for (const token of tokens) {
74 if (token.type !== 'inline') continue
76 for (const child of token.children) {
79 if (!child.content) continue
81 text += lastSeparator + child.content
89 markdownIt.core.ruler.push('plainText', plainTextRule)