diff options
author | Rigel Kent <sendmemail@rigelk.eu> | 2021-04-11 15:06:36 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2021-04-13 15:45:09 +0200 |
commit | 84bced652cd72aad852914a4a734c47dd0002fef (patch) | |
tree | fd1b5fb097c0958792be7e89fe1dec621eb0c841 /server/helpers/markdown.ts | |
parent | 13fec08ba3e6dff8a4a72163d94cbdb32ad57563 (diff) | |
download | PeerTube-84bced652cd72aad852914a4a734c47dd0002fef.tar.gz PeerTube-84bced652cd72aad852914a4a734c47dd0002fef.tar.zst PeerTube-84bced652cd72aad852914a4a734c47dd0002fef.zip |
render markdown and plainify descriptions on previews
Diffstat (limited to 'server/helpers/markdown.ts')
-rw-r--r-- | server/helpers/markdown.ts | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts new file mode 100644 index 000000000..c8fb31c8c --- /dev/null +++ b/server/helpers/markdown.ts | |||
@@ -0,0 +1,26 @@ | |||
1 | import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils' | ||
2 | |||
3 | const sanitizeHtml = require('sanitize-html') | ||
4 | const markdownItEmoji = require('markdown-it-emoji/light') | ||
5 | const MarkdownItClass = require('markdown-it') | ||
6 | const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true }) | ||
7 | |||
8 | markdownIt.enable(TEXT_WITH_HTML_RULES) | ||
9 | markdownIt.use(markdownItEmoji) | ||
10 | |||
11 | const 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 | |||
22 | // --------------------------------------------------------------------------- | ||
23 | |||
24 | export { | ||
25 | toSafeHtml | ||
26 | } | ||