Merge pull request #1676 from omnivore-app/fix/youtube-video-title

Do not escape the title of youtube video or tweet which is saved in elastic
This commit is contained in:
Hongbo Wu
2023-01-18 19:24:33 +08:00
committed by GitHub
3 changed files with 22 additions and 8 deletions

View File

@ -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 {
<head>
<meta property="og:image" content="${authorImage}" />
<meta property="og:image:secure_url" content="${authorImage}" />
<meta property="og:title" content="${title}" />
<meta property="og:title" content="${escapedTitle}" />
<meta property="og:description" content="${description}" />
</head>
<body>

View File

@ -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 = `
<html>
<head><title>${title}</title>
<head><title>${escapedTitle}</title>
<meta property="og:image" content="${thumbnail}" />
<meta property="og:image:secure_url" content="${thumbnail}" />
<meta property="og:title" content="${title}" />
<meta property="og:title" content="${escapedTitle}" />
<meta property="og:description" content="" />
<meta property="og:article:author" content="${authorName}" />
</head>
<body>
<iframe width="${width}" height="${height}" src="https://www.youtube.com/embed/${videoId}" title="${title}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<p><a href="${url}" target="_blank">${title}</a></p>
<iframe width="${width}" height="${height}" src="https://www.youtube.com/embed/${videoId}" title="${escapedTitle}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
<p><a href="${url}" target="_blank">${escapedTitle}</a></p>
<p itemscope="" itemprop="author" itemtype="http://schema.org/Person">By <a href="${oembed.author_url}" target="_blank">${authorName}</a></p>
</body>
</html>`

View File

@ -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&#x27;s Parable'
)
})
})