aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/markdown.ts
blob: c8fb31c8c78b30ced2d326520cbe81a0ced20e6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils'

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 => {
  // 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
  return sanitizeHtml(html, SANITIZE_OPTIONS)
}

// ---------------------------------------------------------------------------

export {
  toSafeHtml
}