2022-02-04 10:31:54 +01:00
|
|
|
import { getDefaultSanitizeOptions, getTextOnlySanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
|
2021-05-27 15:59:55 +02:00
|
|
|
|
2022-02-04 10:31:54 +01:00
|
|
|
const defaultSanitizeOptions = getDefaultSanitizeOptions()
|
|
|
|
const textOnlySanitizeOptions = getTextOnlySanitizeOptions()
|
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')
|
|
|
|
|
2022-02-04 10:31:54 +01:00
|
|
|
const markdownItWithHTML = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
|
2022-02-28 15:13:56 +01:00
|
|
|
const markdownItWithoutHTML = new MarkdownItClass('default', { linkify: false, breaks: true, html: false })
|
2021-04-11 15:06:36 +02:00
|
|
|
|
2022-01-31 10:07:38 +01:00
|
|
|
const toSafeHtml = (text: string) => {
|
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
|
2022-02-04 10:31:54 +01:00
|
|
|
const html = markdownItWithHTML.enable(TEXT_WITH_HTML_RULES)
|
|
|
|
.use(markdownItEmoji)
|
|
|
|
.render(textWithLineFeed)
|
2021-04-11 15:06:36 +02:00
|
|
|
|
|
|
|
// Convert to safe Html
|
2022-02-04 10:31:54 +01:00
|
|
|
return sanitizeHtml(html, defaultSanitizeOptions)
|
2021-04-11 15:06:36 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 10:31:54 +01:00
|
|
|
const mdToOneLinePlainText = (text: string) => {
|
2021-04-16 07:41:35 +02:00
|
|
|
if (!text) return ''
|
|
|
|
|
2022-02-04 10:31:54 +01:00
|
|
|
markdownItWithoutHTML.use(markdownItEmoji)
|
|
|
|
.use(plainTextPlugin)
|
|
|
|
.render(text)
|
2021-04-12 11:43:29 +02:00
|
|
|
|
|
|
|
// Convert to safe Html
|
2022-02-04 10:31:54 +01:00
|
|
|
return sanitizeHtml(markdownItWithoutHTML.plainText, textOnlySanitizeOptions)
|
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,
|
2022-02-04 10:31:54 +01:00
|
|
|
mdToOneLinePlainText
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Thanks: https://github.com/wavesheep/markdown-it-plain-text
|
|
|
|
function plainTextPlugin (markdownIt: any) {
|
|
|
|
let lastSeparator = ''
|
|
|
|
|
|
|
|
function plainTextRule (state: any) {
|
|
|
|
const text = scan(state.tokens)
|
|
|
|
|
|
|
|
markdownIt.plainText = text.replace(/\s+/g, ' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
function scan (tokens: any[]) {
|
|
|
|
let text = ''
|
|
|
|
|
|
|
|
for (const token of tokens) {
|
|
|
|
if (token.children !== null) {
|
|
|
|
text += scan(token.children)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.type === 'list_item_close') {
|
|
|
|
lastSeparator = ', '
|
2022-02-28 15:13:56 +01:00
|
|
|
} else if (token.type.endsWith('_close')) {
|
2022-02-04 10:31:54 +01:00
|
|
|
lastSeparator = ' '
|
|
|
|
} else if (token.content) {
|
|
|
|
text += lastSeparator
|
|
|
|
text += token.content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
|
|
|
markdownIt.core.ruler.push('plainText', plainTextRule)
|
2021-04-11 15:06:36 +02:00
|
|
|
}
|