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 markdownItWithHTML = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
11 const markdownItWithoutHTML = new MarkdownItClass('default', { linkify: true, breaks: true, html: false })
13 const toSafeHtml = (text: string) => {
17 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
19 // Convert possible markdown (emojis, emphasis and lists) to html
20 const html = markdownItWithHTML.enable(TEXT_WITH_HTML_RULES)
22 .render(textWithLineFeed)
24 // Convert to safe Html
25 return sanitizeHtml(html, defaultSanitizeOptions)
28 const mdToOneLinePlainText = (text: string) => {
31 markdownItWithoutHTML.use(markdownItEmoji)
35 // Convert to safe Html
36 return sanitizeHtml(markdownItWithoutHTML.plainText, textOnlySanitizeOptions)
39 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 // Thanks: https://github.com/wavesheep/markdown-it-plain-text
49 function plainTextPlugin (markdownIt: any) {
50 let lastSeparator = ''
52 function plainTextRule (state: any) {
53 const text = scan(state.tokens)
55 markdownIt.plainText = text.replace(/\s+/g, ' ')
58 function scan (tokens: any[]) {
61 for (const token of tokens) {
62 if (token.children !== null) {
63 text += scan(token.children)
67 if (token.type === 'list_item_close') {
69 } else if (/[a-zA-Z]+_close/.test(token.type)) {
71 } else if (token.content) {
80 markdownIt.core.ruler.push('plainText', plainTextRule)