Merge pull request #436 from sachaaaaa/add_to_link_preview_whitelist

Enable link preview for i.imgur.com, tenor and giphy
pull/465/head
sachaaaaa 6 years ago committed by GitHub
commit 94f1d8e1bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -31,9 +31,14 @@ const SUPPORTED_DOMAINS = [
'imgur.com',
'www.imgur.com',
'm.imgur.com',
'i.imgur.com',
'instagram.com',
'www.instagram.com',
'm.instagram.com',
'tenor.com',
'gph.is',
'giphy.com',
'media.giphy.com',
];
function isLinkInWhitelist(link) {
try {
@ -58,7 +63,7 @@ function isLinkInWhitelist(link) {
}
}
const SUPPORTED_MEDIA_DOMAINS = /^([^.]+\.)*(ytimg.com|cdninstagram.com|redd.it|imgur.com|fbcdn.net)$/i;
const SUPPORTED_MEDIA_DOMAINS = /^([^.]+\.)*(ytimg.com|cdninstagram.com|redd.it|imgur.com|fbcdn.net|giphy.com|tenor.com)$/i;
function isMediaLinkInWhitelist(link) {
try {
const url = new URL(link);
@ -81,8 +86,8 @@ function isMediaLinkInWhitelist(link) {
}
}
const META_TITLE = /<meta\s+property="og:title"\s+content="([\s\S]+?)"\s*\/?\s*>/im;
const META_IMAGE = /<meta\s+property="og:image"\s+content="([\s\S]+?)"\s*\/?\s*>/im;
const META_TITLE = /<meta\s+(?:class="dynamic"\s+)?property="og:title"\s+content="([\s\S]+?)"\s*\/?\s*>/im;
const META_IMAGE = /<meta\s+(?:class="dynamic"\s+)?property="og:image"\s+content="([\s\S]+?)"\s*\/?\s*>/im;
function _getMetaTag(html, regularExpression) {
const match = regularExpression.exec(html);
if (match && match[1]) {
@ -96,7 +101,8 @@ function getTitleMetaTag(html) {
return _getMetaTag(html, META_TITLE);
}
function getImageMetaTag(html) {
return _getMetaTag(html, META_IMAGE);
const tag = _getMetaTag(html, META_IMAGE);
return typeof tag === 'string' ? tag.replace('http://', 'https://') : tag;
}
function findLinks(text, caretLocation) {

@ -192,7 +192,7 @@ describe('Link previews', () => {
);
});
it('returns html-decoded tag contents from Instagram', () => {
it('returns html-decoded tag contents from Imgur', () => {
const imgur = `
<meta property="og:site_name" content="Imgur">
<meta property="og:url" content="https://imgur.com/gallery/KFCL8fm">
@ -211,6 +211,50 @@ describe('Link previews', () => {
);
});
it('returns html-decoded tag contents from Giphy', () => {
const giphy = `
<meta property="og:site_name" content="GIPHY">
<meta property="og:url" content="https://media.giphy.com/media/3o7qE8mq5bT9FQj7j2/giphy.gif">
<meta property="og:type" content="video.other">
<meta property="og:title" content="I Cant Hear You Kobe Bryant GIF - Find & Share on GIPHY">
<meta property="og:description" content="Discover &amp; share this Kobe GIF with everyone you know. GIPHY is how you search, share, discover, and create GIFs.">
<meta property="og:image" content="https://media.giphy.com/media/3o7qE8mq5bT9FQj7j2/giphy.gif">
<meta property="og:image:width" content="480">
<meta property="og:image:height" content="262">
`;
assert.strictEqual(
'I Cant Hear You Kobe Bryant GIF - Find & Share on GIPHY',
getTitleMetaTag(giphy)
);
assert.strictEqual(
'https://media.giphy.com/media/3o7qE8mq5bT9FQj7j2/giphy.gif',
getImageMetaTag(giphy)
);
});
it('returns html-decoded tag contents from Tenor', () => {
const tenor = `
<meta class="dynamic" property="og:site_name" content="Tenor" >
<meta class="dynamic" property="og:url" content="https://media1.tenor.com/images/3772949a5b042e626d259f313fd1e9b8/tenor.gif?itemid=14834517">
<meta class="dynamic" property="og:type" content="video.other">
<meta class="dynamic" property="og:title" content="Hopping Jumping GIF - Hopping Jumping Bird - Discover & Share GIFs">
<meta class="dynamic" property="og:description" content="Click to view the GIF">
<meta class="dynamic" property="og:image" content="https://media1.tenor.com/images/3772949a5b042e626d259f313fd1e9b8/tenor.gif?itemid=14834517">
<meta class="dynamic" property="og:image:width" content="498">
<meta class="dynamic" property="og:image:height" content="435">
`;
assert.strictEqual(
'Hopping Jumping GIF - Hopping Jumping Bird - Discover & Share GIFs',
getTitleMetaTag(tenor)
);
assert.strictEqual(
'https://media1.tenor.com/images/3772949a5b042e626d259f313fd1e9b8/tenor.gif?itemid=14834517',
getImageMetaTag(tenor)
);
});
it('returns only the first tag', () => {
const html = `
<meta property="og:title" content="First&nbsp;Second&nbsp;Third"><meta property="og:title" content="Fourth&nbsp;Fifth&nbsp;Sixth">
@ -229,6 +273,17 @@ describe('Link previews', () => {
getTitleMetaTag(html)
);
});
it('converts image url protocol http to https', () => {
const html = `
<meta property="og:image" content="http://giphygifs.s3.amazonaws.com/media/APcFiiTrG0x2/200.gif">
`;
assert.strictEqual(
'https://giphygifs.s3.amazonaws.com/media/APcFiiTrG0x2/200.gif',
getImageMetaTag(html)
);
});
});
describe('#findLinks', () => {

Loading…
Cancel
Save