Skip to content

Commit

Permalink
Use markdown-it and sanitize-html for comment mail notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
kimsible committed Nov 6, 2020
1 parent f5c9c8c commit 5203c30
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
53 changes: 48 additions & 5 deletions server/lib/emailer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,50 @@ import { MAbuseFull, MAbuseMessage, MAccountDefault, MActorFollowActors, MActorF
import { MCommentOwnerVideo, MVideo, MVideoAccountLight } from '../types/models/video'
import { JobQueue } from './job-queue'

const marked = require('marked')
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([
'emphasis',
'link',
'list'
])

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, {
allowedTags: [ 'a', 'p', 'span', 'br', 'strong', 'em', 'ul', 'ol', 'li' ],
allowedSchemes: [ 'http', 'https' ],
allowedAttributes: {
a: [ 'href', 'class', 'target', 'rel' ]
},
transformTags: {
a: (tagName, attribs) => {
let rel = 'noopener noreferrer'
if (attribs.rel === 'me') rel += ' me'

return {
tagName,
attribs: Object.assign(attribs, {
target: '_blank',
rel
})
}
}
}
})
}

const Email = require('email-templates')

class Emailer {
Expand Down Expand Up @@ -237,7 +280,7 @@ class Emailer {
const video = comment.Video
const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath()
const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
const commentText = marked(comment.text)
const commentHtml = toSafeHtml(comment.text)

const emailPayload: EmailPayload = {
template: 'video-comment-new',
Expand All @@ -247,7 +290,7 @@ class Emailer {
accountName: comment.Account.getDisplayName(),
accountUrl: comment.Account.Actor.url,
comment,
commentText,
commentHtml,
video,
videoUrl,
action: {
Expand All @@ -265,15 +308,15 @@ class Emailer {
const video = comment.Video
const videoUrl = WEBSERVER.URL + comment.Video.getWatchStaticPath()
const commentUrl = WEBSERVER.URL + comment.getCommentStaticPath()
const commentText = marked(comment.text)
const commentHtml = toSafeHtml(comment.text)

const emailPayload: EmailPayload = {
template: 'video-comment-mention',
to,
subject: 'Mention on video ' + video.name,
locals: {
comment,
commentText,
commentHtml,
video,
videoUrl,
accountName,
Expand Down
2 changes: 1 addition & 1 deletion server/lib/emails/video-comment-mention/html.pug
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ block content
p.
#[a(href=accountUrl title=handle) #{accountName}] mentioned you in a comment on video
"#[a(href=videoUrl) #{video.name}]":
blockquote !{commentText}
blockquote !{commentHtml}
br(style="display: none;")
2 changes: 1 addition & 1 deletion server/lib/emails/video-comment-new/html.pug
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ block content
p.
#[a(href=accountUrl title=handle) #{accountName}] added a comment on your video
"#[a(href=videoUrl) #{video.name}]":
blockquote !{commentText}
blockquote !{commentHtml}
br(style="display: none;")

0 comments on commit 5203c30

Please sign in to comment.