]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/helpers/markdown.ts
Convert markdown to html/plain text for feeds
[github/Chocobozzz/PeerTube.git] / server / helpers / markdown.ts
index 2126bb7526d00054cc5131e5aa56e4f069f2f87b..0b8c2fabce82e09689042d9e517cb6e751353826 100644 (file)
@@ -1,4 +1,6 @@
-import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
+import { getSanitizeOptions, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
+
+const sanitizeOptions = getSanitizeOptions()
 
 const sanitizeHtml = require('sanitize-html')
 const markdownItEmoji = require('markdown-it-emoji/light')
@@ -8,7 +10,7 @@ const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true,
 markdownIt.enable(TEXT_WITH_HTML_RULES)
 markdownIt.use(markdownItEmoji)
 
-const toSafeHtml = text => {
+const toSafeHtml = (text: string) => {
   if (!text) return ''
 
   // Restore line feed
@@ -18,21 +20,21 @@ const toSafeHtml = text => {
   const html = markdownIt.render(textWithLineFeed)
 
   // Convert to safe Html
-  return sanitizeHtml(html, SANITIZE_OPTIONS)
+  return sanitizeHtml(html, sanitizeOptions)
 }
 
-const mdToPlainText = text => {
+const mdToPlainText = (text: string) => {
   if (!text) return ''
 
   // Convert possible markdown (emojis, emphasis and lists) to html
   const html = markdownIt.render(text)
 
   // Convert to safe Html
-  const safeHtml = sanitizeHtml(html, SANITIZE_OPTIONS)
+  const safeHtml = sanitizeHtml(html, sanitizeOptions)
 
   return safeHtml.replace(/<[^>]+>/g, '')
                  .replace(/\n$/, '')
-                 .replace('\n', ', ')
+                 .replace(/\n/g, ', ')
 }
 
 // ---------------------------------------------------------------------------