]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
render markdown and plainify descriptions on previews
authorRigel Kent <sendmemail@rigelk.eu>
Sun, 11 Apr 2021 13:06:36 +0000 (15:06 +0200)
committerChocobozzz <chocobozzz@cpy.re>
Tue, 13 Apr 2021 13:45:09 +0000 (15:45 +0200)
server/helpers/markdown.ts [new file with mode: 0644]
server/lib/client-html.ts
server/lib/emailer.ts

diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts
new file mode 100644 (file)
index 0000000..c8fb31c
--- /dev/null
@@ -0,0 +1,26 @@
+import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
+
+const sanitizeHtml = require('sanitize-html')
+const markdownItEmoji = require('markdown-it-emoji/light')
+const MarkdownItClass = require('markdown-it')
+const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
+
+markdownIt.enable(TEXT_WITH_HTML_RULES)
+markdownIt.use(markdownItEmoji)
+
+const toSafeHtml = text => {
+  // Restore line feed
+  const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
+
+  // Convert possible markdown (emojis, emphasis and lists) to html
+  const html = markdownIt.render(textWithLineFeed)
+
+  // Convert to safe Html
+  return sanitizeHtml(html, SANITIZE_OPTIONS)
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  toSafeHtml
+}
index 6ddaa82c83725486da80f39ca58cb121707ee0de..5485376d3e3e0db22e5b5fa1aa253bacd75f370d 100644 (file)
@@ -24,6 +24,7 @@ import { VideoChannelModel } from '../models/video/video-channel'
 import { getActivityStreamDuration } from '../models/video/video-format-utils'
 import { VideoPlaylistModel } from '../models/video/video-playlist'
 import { MAccountActor, MChannelActor } from '../types/models'
+import { toSafeHtml } from '../helpers/markdown'
 
 type Tags = {
   ogType: string
@@ -54,6 +55,10 @@ type Tags = {
   }
 }
 
+const toPlainText = (content: string) => {
+  return toSafeHtml(content).replace(/<[^>]+>/g, '')
+}
+
 class ClientHtml {
 
   private static htmlCache: { [path: string]: string } = {}
@@ -94,13 +99,13 @@ class ClientHtml {
     }
 
     let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name))
-    customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description))
+    customHtml = ClientHtml.addDescriptionTag(customHtml, toPlainText(video.description))
 
     const url = WEBSERVER.URL + video.getWatchStaticPath()
     const originUrl = video.url
     const title = escapeHTML(video.name)
     const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
-    const description = escapeHTML(video.description)
+    const description = toPlainText(video.description)
 
     const image = {
       url: WEBSERVER.URL + video.getPreviewStaticPath()
@@ -152,13 +157,13 @@ class ClientHtml {
     }
 
     let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name))
-    customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(videoPlaylist.description))
+    customHtml = ClientHtml.addDescriptionTag(customHtml, toPlainText(videoPlaylist.description))
 
     const url = videoPlaylist.getWatchUrl()
     const originUrl = videoPlaylist.url
     const title = escapeHTML(videoPlaylist.name)
     const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
-    const description = escapeHTML(videoPlaylist.description)
+    const description = toPlainText(videoPlaylist.description)
 
     const image = {
       url: videoPlaylist.getThumbnailUrl()
@@ -236,13 +241,13 @@ class ClientHtml {
     }
 
     let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName()))
-    customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(entity.description))
+    customHtml = ClientHtml.addDescriptionTag(customHtml, toPlainText(entity.description))
 
     const url = entity.getLocalUrl()
     const originUrl = entity.Actor.url
     const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
     const title = escapeHTML(entity.getDisplayName())
-    const description = escapeHTML(entity.description)
+    const description = toPlainText(entity.description)
 
     const image = {
       url: entity.Actor.getAvatarUrl(),
index 9ca0d5d5b139a593472e541bd0407812cd2c3d91..2fad82bccbaa151df18c8b001d3fef0a3ec5883f 100644 (file)
@@ -5,7 +5,6 @@ import { join } from 'path'
 import { VideoChannelModel } from '@server/models/video/video-channel'
 import { MVideoBlacklistLightVideo, MVideoBlacklistVideo } from '@server/types/models/video/video-blacklist'
 import { MVideoImport, MVideoImportVideo } from '@server/types/models/video/video-import'
-import { SANITIZE_OPTIONS, TEXT_WITH_HTML_RULES } from '@shared/core-utils'
 import { AbuseState, EmailPayload, UserAbuse } from '@shared/models'
 import { SendEmailDefaultOptions } from '../../shared/models/server/emailer.model'
 import { isTestInstance, root } from '../helpers/core-utils'
@@ -15,26 +14,7 @@ import { WEBSERVER } from '../initializers/constants'
 import { MAbuseFull, MAbuseMessage, MAccountDefault, MActorFollowActors, MActorFollowFull, MPlugin, MUser } from '../types/models'
 import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video'
 import { JobQueue } from './job-queue'
-
-const sanitizeHtml = require('sanitize-html')
-const markdownItEmoji = require('markdown-it-emoji/light')
-const MarkdownItClass = require('markdown-it')
-const markdownIt = new MarkdownItClass('default', { linkify: true, breaks: true, html: true })
-
-markdownIt.enable(TEXT_WITH_HTML_RULES)
-
-markdownIt.use(markdownItEmoji)
-
-const toSafeHtml = text => {
-  // Restore line feed
-  const textWithLineFeed = text.replace(/<br.?\/?>/g, '\r\n')
-
-  // Convert possible markdown (emojis, emphasis and lists) to html
-  const html = markdownIt.render(textWithLineFeed)
-
-  // Convert to safe Html
-  return sanitizeHtml(html, SANITIZE_OPTIONS)
-}
+import { toSafeHtml } from '../helpers/markdown'
 
 const Email = require('email-templates')