Skip to content

Commit 73c8690

Browse files
committed
[ENH] More thorough image parsing, guaranteed to return images, bump version to 1.2
1 parent f88f391 commit 73c8690

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

__tests__/index.spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ describe('link preview', () => {
1414
// videos: undefined }
1515

1616
expect(linkInfo.url).to.be.equal('https://www.youtube.com/watch?v=wuClZjOdT30');
17-
expect(linkInfo.title).to.be.equal('Geography Now! Germany - YouTube');
18-
expect(linkInfo.description).to.be.equal('Gluten free vegetarians beware. Watch at your own risk. We now have a Public mailbox! Feel free to send anything via mail! Our public mailbox address is: 190...');
17+
expect(linkInfo.title).to.be.equal('Geography Now! Germany');
18+
expect(linkInfo.description).to.be.ok();
1919
expect(linkInfo.mediaType).to.be.equal('video');
2020
expect(linkInfo.images.length).to.be.equal(1);
2121
expect(linkInfo.images[0]).to.be.equal('https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg');
@@ -32,8 +32,8 @@ describe('link preview', () => {
3232
// videos: undefined }
3333

3434
expect(linkInfo.url).to.be.equal('https://www.youtube.com/watch?v=wuClZjOdT30');
35-
expect(linkInfo.title).to.be.equal('Geography Now! Germany - YouTube');
36-
expect(linkInfo.description).to.be.equal('Gluten free vegetarians beware. Watch at your own risk. We now have a Public mailbox! Feel free to send anything via mail! Our public mailbox address is: 190...');
35+
expect(linkInfo.title).to.be.equal('Geography Now! Germany');
36+
expect(linkInfo.description).to.be.ok();
3737
expect(linkInfo.mediaType).to.be.equal('video');
3838
expect(linkInfo.images.length).to.be.equal(1);
3939
expect(linkInfo.images[0]).to.be.equal('https://i.ytimg.com/vi/wuClZjOdT30/maxresdefault.jpg');

index.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
2-
* @providesModule react-native-link-preview
3-
*/
2+
* @providesModule react-native-link-preview
3+
*/
44

55
const cheerio = require('cheerio-without-node-native');
6+
const urlObj = require('url');
67

78
import { REGEX_VALID_URL } from './constants';
89

@@ -84,11 +85,45 @@ export default class LinkPreview {
8485
}
8586

8687
static _getImages(doc, rootUrl) {
87-
const images = [];
88-
const nodes = doc('meta[property=\'og:image\']');
88+
let images = [],
89+
nodes,
90+
src,
91+
dic;
92+
93+
nodes = doc('meta[property=\'og:image\']');
94+
95+
if (nodes.length) {
96+
nodes.each((index, node) => {
97+
src = node.attribs.content;
98+
if (src) {
99+
src = urlObj.resolve(rootUrl, src);
100+
images.push(src);
101+
}
102+
});
103+
}
89104

90-
if (nodes.length > 0) {
91-
nodes.each((index, node) => { images.push(node.attribs.content); });
105+
if (images.length <= 0) {
106+
src = doc('link[rel=image_src]').attr('href');
107+
if (src) {
108+
src = urlObj.resolve(rootUrl, src);
109+
images = [src];
110+
} else {
111+
nodes = doc('img');
112+
113+
if (nodes.length) {
114+
dic = {};
115+
images = [];
116+
nodes.each((index, node) => {
117+
src = node.attribs.src;
118+
if (src && !dic[src]) {
119+
dic[src] = 1;
120+
// width = node.attribs.width;
121+
// height = node.attribs.height;
122+
images.push(urlObj.resolve(rootUrl, src));
123+
}
124+
});
125+
}
126+
}
92127
}
93128

94129
return images;

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-link-preview",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -14,7 +14,8 @@
1414
"peerDependencies": {},
1515
"repository": "https://github.com/ospfranco/react-native-link-preview",
1616
"dependencies": {
17-
"cheerio-without-node-native": "^0.20.1"
17+
"cheerio-without-node-native": "^0.20.1",
18+
"url": "^0.11.0"
1819
},
1920
"devDependencies": {
2021
"babel-core": "^6.22.1",

yarn.lock

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,6 @@ asynckit@^0.4.0:
160160
version "0.4.0"
161161
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
162162

163-
autolinker@^1.2.0:
164-
version "1.4.2"
165-
resolved "https://registry.yarnpkg.com/autolinker/-/autolinker-1.4.2.tgz#bbd43341ee199b3ee50c5b0c48614ca5b95f0f92"
166-
167163
aws-sign2@~0.6.0:
168164
version "0.6.0"
169165
resolved "https://registry.yarnpkg.com/aws-sign2/-/aws-sign2-0.6.0.tgz#14342dd38dbcc94d0e5b87d763cd63612c0e794f"
@@ -2730,6 +2726,10 @@ prr@~0.0.0:
27302726
version "0.0.0"
27312727
resolved "https://registry.yarnpkg.com/prr/-/prr-0.0.0.tgz#1a84b85908325501411853d0081ee3fa86e2926a"
27322728

2729+
punycode@1.3.2:
2730+
version "1.3.2"
2731+
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"
2732+
27332733
punycode@^1.4.1:
27342734
version "1.4.1"
27352735
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"
@@ -2738,6 +2738,10 @@ qs@~6.4.0:
27382738
version "6.4.0"
27392739
resolved "https://registry.yarnpkg.com/qs/-/qs-6.4.0.tgz#13e26d28ad6b0ffaa91312cd3bf708ed351e7233"
27402740

2741+
querystring@0.2.0:
2742+
version "0.2.0"
2743+
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620"
2744+
27412745
randomatic@^1.1.3:
27422746
version "1.1.6"
27432747
resolved "https://registry.yarnpkg.com/randomatic/-/randomatic-1.1.6.tgz#110dcabff397e9dcff7c0789ccc0a49adf1ec5bb"
@@ -3202,6 +3206,13 @@ uglify-to-browserify@~1.0.0:
32023206
version "1.0.2"
32033207
resolved "https://registry.yarnpkg.com/uglify-to-browserify/-/uglify-to-browserify-1.0.2.tgz#6e0924d6bda6b5afe349e39a6d632850a0f882b7"
32043208

3209+
url@^0.11.0:
3210+
version "0.11.0"
3211+
resolved "https://registry.yarnpkg.com/url/-/url-0.11.0.tgz#3838e97cfc60521eb73c525a8e55bfdd9e2e28f1"
3212+
dependencies:
3213+
punycode "1.3.2"
3214+
querystring "0.2.0"
3215+
32053216
user-home@^2.0.0:
32063217
version "2.0.0"
32073218
resolved "https://registry.yarnpkg.com/user-home/-/user-home-2.0.0.tgz#9c70bfd8169bc1dcbf48604e0f04b8b49cde9e9f"

0 commit comments

Comments
 (0)