modify tests to support current behaviour regarding plaintext description

pull/3963/head
Rigel Kent 2021-04-12 11:43:29 +02:00 committed by Chocobozzz
parent 84bced652c
commit a073c91270
3 changed files with 28 additions and 18 deletions

View File

@ -19,8 +19,21 @@ const toSafeHtml = text => {
return sanitizeHtml(html, SANITIZE_OPTIONS) return sanitizeHtml(html, SANITIZE_OPTIONS)
} }
const mdToPlainText = text => {
// Convert possible markdown (emojis, emphasis and lists) to html
const html = markdownIt.render(text)
// Convert to safe Html
const safeHtml = sanitizeHtml(html, SANITIZE_OPTIONS)
return safeHtml.replace(/<[^>]+>/g, '')
.replace(/\n$/, '')
.replace('\n', ', ')
}
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
export { export {
toSafeHtml toSafeHtml,
mdToPlainText
} }

View File

@ -24,7 +24,7 @@ import { VideoChannelModel } from '../models/video/video-channel'
import { getActivityStreamDuration } from '../models/video/video-format-utils' import { getActivityStreamDuration } from '../models/video/video-format-utils'
import { VideoPlaylistModel } from '../models/video/video-playlist' import { VideoPlaylistModel } from '../models/video/video-playlist'
import { MAccountActor, MChannelActor } from '../types/models' import { MAccountActor, MChannelActor } from '../types/models'
import { toSafeHtml } from '../helpers/markdown' import { mdToPlainText } from '../helpers/markdown'
type Tags = { type Tags = {
ogType: string ogType: string
@ -55,10 +55,6 @@ type Tags = {
} }
} }
const toPlainText = (content: string) => {
return toSafeHtml(content).replace(/<[^>]+>/g, '')
}
class ClientHtml { class ClientHtml {
private static htmlCache: { [path: string]: string } = {} private static htmlCache: { [path: string]: string } = {}
@ -99,13 +95,13 @@ class ClientHtml {
} }
let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name)) let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name))
customHtml = ClientHtml.addDescriptionTag(customHtml, toPlainText(video.description)) customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(video.description))
const url = WEBSERVER.URL + video.getWatchStaticPath() const url = WEBSERVER.URL + video.getWatchStaticPath()
const originUrl = video.url const originUrl = video.url
const title = escapeHTML(video.name) const title = escapeHTML(video.name)
const siteName = escapeHTML(CONFIG.INSTANCE.NAME) const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
const description = toPlainText(video.description) const description = mdToPlainText(video.description)
const image = { const image = {
url: WEBSERVER.URL + video.getPreviewStaticPath() url: WEBSERVER.URL + video.getPreviewStaticPath()
@ -157,13 +153,13 @@ class ClientHtml {
} }
let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name)) let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name))
customHtml = ClientHtml.addDescriptionTag(customHtml, toPlainText(videoPlaylist.description)) customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(videoPlaylist.description))
const url = videoPlaylist.getWatchUrl() const url = videoPlaylist.getWatchUrl()
const originUrl = videoPlaylist.url const originUrl = videoPlaylist.url
const title = escapeHTML(videoPlaylist.name) const title = escapeHTML(videoPlaylist.name)
const siteName = escapeHTML(CONFIG.INSTANCE.NAME) const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
const description = toPlainText(videoPlaylist.description) const description = mdToPlainText(videoPlaylist.description)
const image = { const image = {
url: videoPlaylist.getThumbnailUrl() url: videoPlaylist.getThumbnailUrl()
@ -241,13 +237,13 @@ class ClientHtml {
} }
let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName())) let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName()))
customHtml = ClientHtml.addDescriptionTag(customHtml, toPlainText(entity.description)) customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(entity.description))
const url = entity.getLocalUrl() const url = entity.getLocalUrl()
const originUrl = entity.Actor.url const originUrl = entity.Actor.url
const siteName = escapeHTML(CONFIG.INSTANCE.NAME) const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
const title = escapeHTML(entity.getDisplayName()) const title = escapeHTML(entity.getDisplayName())
const description = toPlainText(entity.description) const description = mdToPlainText(entity.description)
const image = { const image = {
url: entity.Actor.getAvatarUrl(), url: entity.Actor.getAvatarUrl(),
@ -383,7 +379,7 @@ class ClientHtml {
} }
metaTags['og:url'] = tags.url metaTags['og:url'] = tags.url
metaTags['og:description'] = tags.description metaTags['og:description'] = mdToPlainText(tags.description)
if (tags.embed) { if (tags.embed) {
metaTags['og:video:url'] = tags.embed.url metaTags['og:video:url'] = tags.embed.url
@ -399,7 +395,7 @@ class ClientHtml {
private static generateStandardMetaTags (tags: Tags) { private static generateStandardMetaTags (tags: Tags) {
return { return {
name: tags.title, name: tags.title,
description: tags.description, description: mdToPlainText(tags.description),
image: tags.image.url image: tags.image.url
} }
} }

View File

@ -39,7 +39,8 @@ describe('Test a client controllers', function () {
let account: Account let account: Account
const videoName = 'my super name for server 1' const videoName = 'my super name for server 1'
const videoDescription = 'my super description for server 1' const videoDescription = 'my<br> super __description__ for *server* 1<p></p>'
const videoDescriptionPlainText = 'my super description for server 1'
const playlistName = 'super playlist name' const playlistName = 'super playlist name'
const playlistDescription = 'super playlist description' const playlistDescription = 'super playlist description'
@ -169,7 +170,7 @@ describe('Test a client controllers', function () {
.expect(HttpStatusCode.OK_200) .expect(HttpStatusCode.OK_200)
expect(res.text).to.contain(`<meta property="og:title" content="${videoName}" />`) expect(res.text).to.contain(`<meta property="og:title" content="${videoName}" />`)
expect(res.text).to.contain(`<meta property="og:description" content="${videoDescription}" />`) expect(res.text).to.contain(`<meta property="og:description" content="${videoDescriptionPlainText}" />`)
expect(res.text).to.contain('<meta property="og:type" content="video" />') expect(res.text).to.contain('<meta property="og:type" content="video" />')
expect(res.text).to.contain(`<meta property="og:url" content="${servers[0].url}/videos/watch/${servers[0].video.uuid}" />`) expect(res.text).to.contain(`<meta property="og:url" content="${servers[0].url}/videos/watch/${servers[0].video.uuid}" />`)
}) })
@ -181,7 +182,7 @@ describe('Test a client controllers', function () {
.expect(HttpStatusCode.OK_200) .expect(HttpStatusCode.OK_200)
expect(res.text).to.contain(`<meta property="og:title" content="${videoName}" />`) expect(res.text).to.contain(`<meta property="og:title" content="${videoName}" />`)
expect(res.text).to.contain(`<meta property="og:description" content="${videoDescription}" />`) expect(res.text).to.contain(`<meta property="og:description" content="${videoDescriptionPlainText}" />`)
expect(res.text).to.contain('<meta property="og:type" content="video" />') expect(res.text).to.contain('<meta property="og:type" content="video" />')
expect(res.text).to.contain(`<meta property="og:url" content="${servers[0].url}/videos/watch/${servers[0].video.uuid}" />`) expect(res.text).to.contain(`<meta property="og:url" content="${servers[0].url}/videos/watch/${servers[0].video.uuid}" />`)
}) })
@ -210,7 +211,7 @@ describe('Test a client controllers', function () {
expect(res.text).to.contain('<meta property="twitter:card" content="summary_large_image" />') expect(res.text).to.contain('<meta property="twitter:card" content="summary_large_image" />')
expect(res.text).to.contain('<meta property="twitter:site" content="@Chocobozzz" />') expect(res.text).to.contain('<meta property="twitter:site" content="@Chocobozzz" />')
expect(res.text).to.contain(`<meta property="twitter:title" content="${videoName}" />`) expect(res.text).to.contain(`<meta property="twitter:title" content="${videoName}" />`)
expect(res.text).to.contain(`<meta property="twitter:description" content="${videoDescription}" />`) expect(res.text).to.contain(`<meta property="twitter:description" content="${videoDescriptionPlainText}" />`)
}) })
it('Should have valid twitter card on the watch playlist page', async function () { it('Should have valid twitter card on the watch playlist page', async function () {