aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-03-07 11:48:53 +0100
committerChocobozzz <me@florianbigard.com>2022-03-07 11:48:53 +0100
commit2f19481147b12e2ae503ed3d1f28621c94447ac3 (patch)
tree6a5d1ab08432d0653b2391f67726e5d0c71ace65 /server
parent907ba7e270b78507a8d9b6aa20da0f6231c37d25 (diff)
downloadPeerTube-2f19481147b12e2ae503ed3d1f28621c94447ac3.tar.gz
PeerTube-2f19481147b12e2ae503ed3d1f28621c94447ac3.tar.zst
PeerTube-2f19481147b12e2ae503ed3d1f28621c94447ac3.zip
Optimize markdown renderer
Diffstat (limited to 'server')
-rw-r--r--server/helpers/markdown.ts54
-rw-r--r--server/tests/helpers/markdown.ts6
2 files changed, 38 insertions, 22 deletions
diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts
index 25685ec6d..f9291bdad 100644
--- a/server/helpers/markdown.ts
+++ b/server/helpers/markdown.ts
@@ -7,8 +7,13 @@ const sanitizeHtml = require('sanitize-html')
7const markdownItEmoji = require('markdown-it-emoji/light') 7const markdownItEmoji = require('markdown-it-emoji/light')
8const MarkdownItClass = require('markdown-it') 8const MarkdownItClass = require('markdown-it')
9 9
10const markdownItWithHTML = new MarkdownItClass('default', { linkify: true, breaks: true, html: true }) 10const markdownItForSafeHtml = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
11const markdownItWithoutHTML = new MarkdownItClass('default', { linkify: true, breaks: true, html: false }) 11 .enable(TEXT_WITH_HTML_RULES)
12 .use(markdownItEmoji)
13
14const markdownItForPlainText = new MarkdownItClass('default', { linkify: false, breaks: true, html: false })
15 .use(markdownItEmoji)
16 .use(plainTextPlugin)
12 17
13const toSafeHtml = (text: string) => { 18const toSafeHtml = (text: string) => {
14 if (!text) return '' 19 if (!text) return ''
@@ -17,9 +22,7 @@ const toSafeHtml = (text: string) => {
17 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n') 22 const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
18 23
19 // Convert possible markdown (emojis, emphasis and lists) to html 24 // Convert possible markdown (emojis, emphasis and lists) to html
20 const html = markdownItWithHTML.enable(TEXT_WITH_HTML_RULES) 25 const html = markdownItForSafeHtml.render(textWithLineFeed)
21 .use(markdownItEmoji)
22 .render(textWithLineFeed)
23 26
24 // Convert to safe Html 27 // Convert to safe Html
25 return sanitizeHtml(html, defaultSanitizeOptions) 28 return sanitizeHtml(html, defaultSanitizeOptions)
@@ -28,12 +31,10 @@ const toSafeHtml = (text: string) => {
28const mdToOneLinePlainText = (text: string) => { 31const mdToOneLinePlainText = (text: string) => {
29 if (!text) return '' 32 if (!text) return ''
30 33
31 markdownItWithoutHTML.use(markdownItEmoji) 34 markdownItForPlainText.render(text)
32 .use(plainTextPlugin)
33 .render(text)
34 35
35 // Convert to safe Html 36 // Convert to safe Html
36 return sanitizeHtml(markdownItWithoutHTML.plainText, textOnlySanitizeOptions) 37 return sanitizeHtml(markdownItForPlainText.plainText, textOnlySanitizeOptions)
37} 38}
38 39
39// --------------------------------------------------------------------------- 40// ---------------------------------------------------------------------------
@@ -47,30 +48,39 @@ export {
47 48
48// Thanks: https://github.com/wavesheep/markdown-it-plain-text 49// Thanks: https://github.com/wavesheep/markdown-it-plain-text
49function plainTextPlugin (markdownIt: any) { 50function plainTextPlugin (markdownIt: any) {
50 let lastSeparator = ''
51
52 function plainTextRule (state: any) { 51 function plainTextRule (state: any) {
53 const text = scan(state.tokens) 52 const text = scan(state.tokens)
54 53
55 markdownIt.plainText = text.replace(/\s+/g, ' ') 54 // markdownIt.plainText = text.replace(/\s+/g, ' ')
55 markdownIt.plainText = text
56 } 56 }
57 57
58 function scan (tokens: any[]) { 58 function scan (tokens: any[]) {
59 let lastSeparator = ''
59 let text = '' 60 let text = ''
60 61
61 for (const token of tokens) { 62 function buildSeparator (token: any) {
62 if (token.children !== null) {
63 text += scan(token.children)
64 continue
65 }
66
67 if (token.type === 'list_item_close') { 63 if (token.type === 'list_item_close') {
68 lastSeparator = ', ' 64 lastSeparator = ', '
69 } else if (/[a-zA-Z]+_close/.test(token.type)) { 65 }
66
67 if (token.tag === 'br' || token.type === 'paragraph_close') {
70 lastSeparator = ' ' 68 lastSeparator = ' '
71 } else if (token.content) { 69 }
72 text += lastSeparator 70 }
73 text += token.content 71
72 for (const token of tokens) {
73 buildSeparator(token)
74
75 if (token.type !== 'inline') continue
76
77 for (const child of token.children) {
78 buildSeparator(child)
79
80 if (!child.content) continue
81
82 text += lastSeparator + child.content
83 lastSeparator = ''
74 } 84 }
75 } 85 }
76 86
diff --git a/server/tests/helpers/markdown.ts b/server/tests/helpers/markdown.ts
index 0488a1a05..8177477f6 100644
--- a/server/tests/helpers/markdown.ts
+++ b/server/tests/helpers/markdown.ts
@@ -30,5 +30,11 @@ describe('Markdown helpers', function () {
30 30
31 expect(result).to.equal('Hello coucou') 31 expect(result).to.equal('Hello coucou')
32 }) 32 })
33
34 it('Should convert tags to plain text', function () {
35 const result = mdToOneLinePlainText(`#déconversion\n#newage\n#histoire`)
36
37 expect(result).to.equal('#déconversion #newage #histoire')
38 })
33 }) 39 })
34}) 40})