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