]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/markdown.ts
Refactor feed component
[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 => {
12 // Restore line feed
13 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
14
15 // Convert possible markdown (emojis, emphasis and lists) to html
16 const html = markdownIt.render(textWithLineFeed)
17
18 // Convert to safe Html
19 return sanitizeHtml(html, SANITIZE_OPTIONS)
20}
21
a073c912
RK
22const mdToPlainText = text => {
23 // Convert possible markdown (emojis, emphasis and lists) to html
24 const html = markdownIt.render(text)
25
26 // Convert to safe Html
27 const safeHtml = sanitizeHtml(html, SANITIZE_OPTIONS)
28
29 return safeHtml.replace(/<[^>]+>/g, '')
30 .replace(/\n$/, '')
31 .replace('\n', ', ')
32}
33
84bced65
RK
34// ---------------------------------------------------------------------------
35
36export {
a073c912
RK
37 toSafeHtml,
38 mdToPlainText
84bced65 39}