]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/markdown.ts
Fix s3 mock cleanup
[github/Chocobozzz/PeerTube.git] / server / helpers / markdown.ts
1 import { getDefaultSanitizeOptions, getTextOnlySanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
2
3 const defaultSanitizeOptions = getDefaultSanitizeOptions()
4 const textOnlySanitizeOptions = getTextOnlySanitizeOptions()
5
6 const sanitizeHtml = require('sanitize-html')
7 const markdownItEmoji = require('markdown-it-emoji/light')
8 const MarkdownItClass = require('markdown-it')
9
10 const markdownItForSafeHtml = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
11 .enable(TEXT_WITH_HTML_RULES)
12 .use(markdownItEmoji)
13
14 const markdownItForPlainText = new MarkdownItClass('default', { linkify: false, breaks: true, html: false })
15 .use(markdownItEmoji)
16 .use(plainTextPlugin)
17
18 const toSafeHtml = (text: string) => {
19 if (!text) return ''
20
21 // Restore line feed
22 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
23
24 // Convert possible markdown (emojis, emphasis and lists) to html
25 const html = markdownItForSafeHtml.render(textWithLineFeed)
26
27 // Convert to safe Html
28 return sanitizeHtml(html, defaultSanitizeOptions)
29 }
30
31 const mdToOneLinePlainText = (text: string) => {
32 if (!text) return ''
33
34 markdownItForPlainText.render(text)
35
36 // Convert to safe Html
37 return sanitizeHtml(markdownItForPlainText.plainText, textOnlySanitizeOptions)
38 }
39
40 // ---------------------------------------------------------------------------
41
42 export {
43 toSafeHtml,
44 mdToOneLinePlainText
45 }
46
47 // ---------------------------------------------------------------------------
48
49 // Thanks: https://github.com/wavesheep/markdown-it-plain-text
50 function plainTextPlugin (markdownIt: any) {
51 function plainTextRule (state: any) {
52 const text = scan(state.tokens)
53
54 markdownIt.plainText = text
55 }
56
57 function scan (tokens: any[]) {
58 let lastSeparator = ''
59 let text = ''
60
61 function buildSeparator (token: any) {
62 if (token.type === 'list_item_close') {
63 lastSeparator = ', '
64 }
65
66 if (token.tag === 'br' || token.type === 'paragraph_close') {
67 lastSeparator = ' '
68 }
69 }
70
71 for (const token of tokens) {
72 buildSeparator(token)
73
74 if (token.type !== 'inline') continue
75
76 for (const child of token.children) {
77 buildSeparator(child)
78
79 if (!child.content) continue
80
81 text += lastSeparator + child.content
82 lastSeparator = ''
83 }
84 }
85
86 return text
87 }
88
89 markdownIt.core.ruler.push('plainText', plainTextRule)
90 }