aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/markdown.ts
blob: a20ac22d4406b1e04be4c7774a26a8c1acd03310 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { getDefaultSanitizeOptions, getTextOnlySanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils'

const defaultSanitizeOptions = getDefaultSanitizeOptions()
const textOnlySanitizeOptions = getTextOnlySanitizeOptions()

const sanitizeHtml = require('sanitize-html')
const markdownItEmoji = require('markdown-it-emoji/light')
const MarkdownItClass = require('markdown-it')

const markdownItForSafeHtml = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
  .enable(TEXT_WITH_HTML_RULES)
  .use(markdownItEmoji)

const markdownItForPlainText = new MarkdownItClass('default', { linkify: false, breaks: true, html: false })
  .use(markdownItEmoji)
  .use(plainTextPlugin)

const toSafeHtml = (text: string) => {
  if (!text) return ''

  // Restore line feed
  const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')

  // Convert possible markdown (emojis, emphasis and lists) to html
  const html = markdownItForSafeHtml.render(textWithLineFeed)

  // Convert to safe Html
  return sanitizeHtml(html, defaultSanitizeOptions)
}

const mdToOneLinePlainText = (text: string) => {
  if (!text) return ''

  markdownItForPlainText.render(text)

  // Convert to safe Html
  return sanitizeHtml(markdownItForPlainText.plainText, textOnlySanitizeOptions)
}

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

export {
  toSafeHtml,
  mdToOneLinePlainText
}

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

// Thanks: https://github.com/wavesheep/markdown-it-plain-text
function plainTextPlugin (markdownIt: any) {
  function plainTextRule (state: any) {
    const text = scan(state.tokens)

    markdownIt.plainText = text
  }

  function scan (tokens: any[]) {
    let lastSeparator = ''
    let text = ''

    function buildSeparator (token: any) {
      if (token.type === 'list_item_close') {
        lastSeparator = ', '
      }

      if (token.tag === 'br' || token.type === 'paragraph_close') {
        lastSeparator = ' '
      }
    }

    for (const token of tokens) {
      buildSeparator(token)

      if (token.type !== 'inline') continue

      for (const child of token.children) {
        buildSeparator(child)

        if (!child.content) continue

        text += lastSeparator + child.content
        lastSeparator = ''
      }
    }

    return text
  }

  markdownIt.core.ruler.push('plainText', plainTextRule)
}