From 1b3d4bbffa4d7a77abffd1ef3ffa7da4f62eca29 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Wed, 18 Jan 2023 19:08:36 +0800 Subject: [PATCH] Do not escape the title of youtube video or tweet which is saved in elastic --- .../src/websites/twitter-handler.ts | 5 +++-- .../src/websites/youtube-handler.ts | 15 ++++++++++----- .../content-handler/test/youtube-handler.test.ts | 10 +++++++++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/content-handler/src/websites/twitter-handler.ts b/packages/content-handler/src/websites/twitter-handler.ts index c745f6d66..121e380a4 100644 --- a/packages/content-handler/src/websites/twitter-handler.ts +++ b/packages/content-handler/src/websites/twitter-handler.ts @@ -316,7 +316,8 @@ export class TwitterHandler extends ContentHandler { const authorId = tweetData.author_id const author = tweet.includes.users.filter((u) => (u.id = authorId))[0] // escape html entities in title - const title = _.escape(titleForAuthor(author)) + const title = titleForAuthor(author) + const escapedTitle = _.escape(title) const authorImage = author.profile_image_url.replace('_normal', '_400x400') const description = _.escape(tweetData.text) @@ -368,7 +369,7 @@ export class TwitterHandler extends ContentHandler { - + diff --git a/packages/content-handler/src/websites/youtube-handler.ts b/packages/content-handler/src/websites/youtube-handler.ts index 1fdbb8029..a12107f50 100644 --- a/packages/content-handler/src/websites/youtube-handler.ts +++ b/packages/content-handler/src/websites/youtube-handler.ts @@ -18,6 +18,10 @@ export const getYoutubeVideoId = (url: string) => { return videoId } +export const escapeTitle = (title: string) => { + return _.escape(title) +} + export class YoutubeHandler extends ContentHandler { constructor() { super() @@ -46,7 +50,8 @@ export class YoutubeHandler extends ContentHandler { author_url: string } // escape html entities in title - const title = _.escape(oembed.title) + const title = oembed.title + const escapedTitle = escapeTitle(title) const ratio = oembed.width / oembed.height const thumbnail = oembed.thumbnail_url const height = 350 @@ -55,16 +60,16 @@ export class YoutubeHandler extends ContentHandler { const content = ` - ${title} + ${escapedTitle} - + - -

${title}

+ +

${escapedTitle}

` diff --git a/packages/content-handler/test/youtube-handler.test.ts b/packages/content-handler/test/youtube-handler.test.ts index beb4d3a66..410229691 100644 --- a/packages/content-handler/test/youtube-handler.test.ts +++ b/packages/content-handler/test/youtube-handler.test.ts @@ -1,6 +1,6 @@ import { expect } from 'chai' import 'mocha' -import { getYoutubeVideoId } from '../src/websites/youtube-handler' +import { escapeTitle, getYoutubeVideoId } from '../src/websites/youtube-handler' describe('getYoutubeVideoId', () => { it('should parse video id out of a URL', async () => { @@ -23,3 +23,11 @@ describe('getYoutubeVideoId', () => { ) }) }) + +describe('escapeTitle', () => { + it('escapes the special characters in the title', async () => { + expect(escapeTitle("The Stanley's Parable")).to.eq( + 'The Stanley's Parable' + ) + }) +})