From a073c912700e1ecda70a463f334f316dc62db7a4 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Mon, 12 Apr 2021 11:43:29 +0200 Subject: [PATCH] modify tests to support current behaviour regarding plaintext description --- server/helpers/markdown.ts | 15 ++++++++++++++- server/lib/client-html.ts | 22 +++++++++------------- server/tests/client.ts | 9 +++++---- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/server/helpers/markdown.ts b/server/helpers/markdown.ts index c8fb31c8c..7c4a1a8b1 100644 --- a/server/helpers/markdown.ts +++ b/server/helpers/markdown.ts @@ -19,8 +19,21 @@ const toSafeHtml = text => { 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 { - toSafeHtml + toSafeHtml, + mdToPlainText } diff --git a/server/lib/client-html.ts b/server/lib/client-html.ts index 5485376d3..203bd3893 100644 --- a/server/lib/client-html.ts +++ b/server/lib/client-html.ts @@ -24,7 +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' +import { mdToPlainText } from '../helpers/markdown' type Tags = { ogType: string @@ -55,10 +55,6 @@ type Tags = { } } -const toPlainText = (content: string) => { - return toSafeHtml(content).replace(/<[^>]+>/g, '') -} - class ClientHtml { private static htmlCache: { [path: string]: string } = {} @@ -99,13 +95,13 @@ class ClientHtml { } 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 originUrl = video.url const title = escapeHTML(video.name) const siteName = escapeHTML(CONFIG.INSTANCE.NAME) - const description = toPlainText(video.description) + const description = mdToPlainText(video.description) const image = { url: WEBSERVER.URL + video.getPreviewStaticPath() @@ -157,13 +153,13 @@ class ClientHtml { } 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 originUrl = videoPlaylist.url const title = escapeHTML(videoPlaylist.name) const siteName = escapeHTML(CONFIG.INSTANCE.NAME) - const description = toPlainText(videoPlaylist.description) + const description = mdToPlainText(videoPlaylist.description) const image = { url: videoPlaylist.getThumbnailUrl() @@ -241,13 +237,13 @@ class ClientHtml { } 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 originUrl = entity.Actor.url const siteName = escapeHTML(CONFIG.INSTANCE.NAME) const title = escapeHTML(entity.getDisplayName()) - const description = toPlainText(entity.description) + const description = mdToPlainText(entity.description) const image = { url: entity.Actor.getAvatarUrl(), @@ -383,7 +379,7 @@ class ClientHtml { } metaTags['og:url'] = tags.url - metaTags['og:description'] = tags.description + metaTags['og:description'] = mdToPlainText(tags.description) if (tags.embed) { metaTags['og:video:url'] = tags.embed.url @@ -399,7 +395,7 @@ class ClientHtml { private static generateStandardMetaTags (tags: Tags) { return { name: tags.title, - description: tags.description, + description: mdToPlainText(tags.description), image: tags.image.url } } diff --git a/server/tests/client.ts b/server/tests/client.ts index d608764ee..3c99bcd1f 100644 --- a/server/tests/client.ts +++ b/server/tests/client.ts @@ -39,7 +39,8 @@ describe('Test a client controllers', function () { let account: Account const videoName = 'my super name for server 1' - const videoDescription = 'my super description for server 1' + const videoDescription = 'my
super __description__ for *server* 1

' + const videoDescriptionPlainText = 'my super description for server 1' const playlistName = 'super playlist name' const playlistDescription = 'super playlist description' @@ -169,7 +170,7 @@ describe('Test a client controllers', function () { .expect(HttpStatusCode.OK_200) expect(res.text).to.contain(``) - expect(res.text).to.contain(``) + expect(res.text).to.contain(``) expect(res.text).to.contain('') expect(res.text).to.contain(``) }) @@ -181,7 +182,7 @@ describe('Test a client controllers', function () { .expect(HttpStatusCode.OK_200) expect(res.text).to.contain(``) - expect(res.text).to.contain(``) + expect(res.text).to.contain(``) expect(res.text).to.contain('') expect(res.text).to.contain(``) }) @@ -210,7 +211,7 @@ describe('Test a client controllers', function () { expect(res.text).to.contain('') expect(res.text).to.contain('') expect(res.text).to.contain(``) - expect(res.text).to.contain(``) + expect(res.text).to.contain(``) }) it('Should have valid twitter card on the watch playlist page', async function () {