]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/markdown.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / markdown.ts
CommitLineData
84bced65
RK
1import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
2
3const sanitizeHtml = require('sanitize-html')
4const markdownItEmoji = require('markdown-it-emoji/light')
5const MarkdownItClass = require('markdown-it')
6const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
7
8markdownIt.enable(TEXT_WITH_HTML_RULES)
9markdownIt.use(markdownItEmoji)
10
11const toSafeHtml = text => {
46460547
C
12 if (!text) return ''
13
84bced65
RK
14 // Restore line feed
15 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
16
17 // Convert possible markdown (emojis, emphasis and lists) to html
18 const html = markdownIt.render(textWithLineFeed)
19
20 // Convert to safe Html
21 return sanitizeHtml(html, SANITIZE_OPTIONS)
22}
23
a073c912 24const mdToPlainText = text => {
46460547
C
25 if (!text) return ''
26
a073c912
RK
27 // Convert possible markdown (emojis, emphasis and lists) to html
28 const html = markdownIt.render(text)
29
30 // Convert to safe Html
31 const safeHtml = sanitizeHtml(html, SANITIZE_OPTIONS)
32
33 return safeHtml.replace(/<[^>]+>/g, '')
34 .replace(/\n$/, '')
35 .replace('\n', ', ')
36}
37
84bced65
RK
38// ---------------------------------------------------------------------------
39
40export {
a073c912
RK
41 toSafeHtml,
42 mdToPlainText
84bced65 43}