react-native-link-preview
Advanced tools
Comparing version 1.0.5 to 1.0.7
168
index.js
@@ -5,39 +5,37 @@ /** | ||
import { NativeModules } from 'react-native'; | ||
const cheerio = require('cheerio-without-node-native'); | ||
const Autolinker = require('autolinker'); | ||
const cheerio = require('cheerio-without-node-native') | ||
const Autolinker = require('autolinker') | ||
export default class LinkPreview { | ||
static getPreview(text) { | ||
return new Promise((resolve, reject) => { | ||
if(!text) { | ||
reject({error: 'LinkPreview did not receive either a url or text'}) | ||
if (!text) { | ||
reject({ error: 'React-Native-Link-Preview did not receive either a url or text' }); | ||
} | ||
let detectedUrl = null | ||
const linkedText = Autolinker.link(text, { | ||
let detectedUrl = null; | ||
Autolinker.link(text, { | ||
replaceFn: match => { | ||
switch( match.getType() ) { | ||
switch (match.getType()) { | ||
case 'url' : | ||
if(!detectedUrl) | ||
detectedUrl = match.getUrl() | ||
return true | ||
if (!detectedUrl) { detectedUrl = match.getUrl(); } | ||
return true; | ||
default: | ||
break | ||
return false; | ||
} | ||
}, | ||
}) | ||
} | ||
}); | ||
if(detectedUrl) { | ||
if (detectedUrl) { | ||
fetch(detectedUrl) | ||
.then(response => response.text()) | ||
.then(text => { | ||
resolve(this._parseResponse(text, detectedUrl)) | ||
resolve(this._parseResponse(text, detectedUrl)); | ||
}) | ||
.catch(err => reject(err)) | ||
.catch(error => reject({ error })); | ||
} else { | ||
reject({error: 'Preview link did not find a link in the text'}) | ||
reject({ error: 'Preview link did not find a link in the text' }); | ||
} | ||
}) | ||
}); | ||
} | ||
@@ -47,3 +45,3 @@ | ||
static _parseResponse(body, url) { | ||
const doc = cheerio.load(body) | ||
const doc = cheerio.load(body); | ||
@@ -56,39 +54,38 @@ return { | ||
images: this._getImages(doc, url), | ||
videos: this._getVideos(doc), | ||
} | ||
videos: this._getVideos(doc) | ||
}; | ||
} | ||
static _getTitle(doc){ | ||
let title = doc('title').text() | ||
static _getTitle(doc) { | ||
let title = doc('title').text(); | ||
if(title === undefined || !title){ | ||
title = doc('meta[property=\'og:title\']').attr('content') | ||
if (!title) { | ||
title = doc('meta[property=\'og:title\']').attr('content'); | ||
} | ||
return title | ||
return title; | ||
} | ||
static _getDescription(doc){ | ||
let description = doc('meta[name=description]').attr('content') | ||
static _getDescription(doc) { | ||
let description = doc('meta[name=description]').attr('content'); | ||
if(description === undefined) { | ||
description = doc('meta[name=Description]').attr('content') | ||
if (description === undefined) { | ||
description = doc('meta[name=Description]').attr('content'); | ||
} | ||
if(description === undefined) { | ||
description = doc('meta[property=\'og:description\']').attr('content') | ||
} | ||
if (description === undefined) { | ||
description = doc('meta[property=\'og:description\']').attr('content'); | ||
} | ||
return description | ||
return description; | ||
} | ||
static _getMediaType(doc) { | ||
let node = doc('meta[name=medium]'), | ||
content | ||
const node = doc('meta[name=medium]'); | ||
if(node.length) { | ||
content = node.attr('content') | ||
return content == 'image' ? 'photo' : content | ||
if (node.length) { | ||
const content = node.attr('content'); | ||
return content === 'image' ? 'photo' : content; | ||
} else { | ||
return doc('meta[property=\'og:type\']').attr('content') | ||
return doc('meta[property=\'og:type\']').attr('content'); | ||
} | ||
@@ -98,48 +95,49 @@ } | ||
static _getImages(doc, rootUrl) { | ||
let images = [], nodes, src, | ||
width, height, | ||
dic | ||
const images = []; | ||
const nodes = doc('meta[property=\'og:image\']'); | ||
nodes = doc('meta[property=\'og:image\']') | ||
if (nodes.length > 0) { | ||
nodes.each((index, node) => { images.push(node.attribs.content); }); | ||
} | ||
if(nodes.length > 0) { | ||
nodes.each((index, node) => { | ||
src = node.attribs['content'] | ||
images.push(src) | ||
}) | ||
} | ||
return images | ||
return images; | ||
} | ||
static _getVideos(doc) { | ||
let videos, | ||
nodes, nodeTypes, nodeSecureUrls, | ||
nodeType, nodeSecureUrl, | ||
video, videoType, videoSecureUrl, | ||
width, height, | ||
videoObj, index, length | ||
const videos = []; | ||
let nodeTypes; | ||
let nodeSecureUrls; | ||
let nodeType; | ||
let nodeSecureUrl; | ||
let video; | ||
let videoType; | ||
let videoSecureUrl; | ||
let width; | ||
let height; | ||
let videoObj; | ||
let index; | ||
nodes = doc('meta[property=\'og:video\']') | ||
length = nodes.length | ||
if(length) { | ||
videos = [] | ||
nodeTypes = doc('meta[property=\'og:video:type\']') | ||
nodeSecureUrls = doc('meta[property=\'og:video:secure_url\']') | ||
width = doc('meta[property=\'og:video:width\']').attr('content') | ||
height = doc('meta[property=\'og:video:height\']').attr('content') | ||
const nodes = doc('meta[property=\'og:video\']'); | ||
const length = nodes.length; | ||
for(index = 0; index < length; index++) { | ||
video = nodes[index].attribs['content'] | ||
if (length) { | ||
nodeTypes = doc('meta[property=\'og:video:type\']'); | ||
nodeSecureUrls = doc('meta[property=\'og:video:secure_url\']'); | ||
width = doc('meta[property=\'og:video:width\']').attr('content'); | ||
height = doc('meta[property=\'og:video:height\']').attr('content'); | ||
nodeType = nodeTypes[index] | ||
videoType = nodeType ? nodeType.attribs['content'] : null | ||
for (index = 0; index < length; index++) { | ||
video = nodes[index].attribs.content; | ||
nodeSecureUrl = nodeSecureUrls[index] | ||
videoSecureUrl = nodeSecureUrl ? nodeSecureUrl.attribs['content'] : null | ||
nodeType = nodeTypes[index]; | ||
videoType = nodeType ? nodeType.attribs.content : null; | ||
videoObj = { url: video, secureUrl: videoSecureUrl, type: videoType, width, height } | ||
if(videoType.indexOf('video/') === 0) { | ||
videos.splice(0, 0, videoObj) | ||
nodeSecureUrl = nodeSecureUrls[index]; | ||
videoSecureUrl = nodeSecureUrl ? nodeSecureUrl.attribs.content : null; | ||
videoObj = { url: video, secureUrl: videoSecureUrl, type: videoType, width, height }; | ||
if (videoType.indexOf('video/') === 0) { | ||
videos.splice(0, 0, videoObj); | ||
} else { | ||
videos.push(videoObj) | ||
videos.push(videoObj); | ||
} | ||
@@ -149,13 +147,13 @@ } | ||
return videos | ||
return videos; | ||
} | ||
static _parseMediaResponse(res, contentType, url) { | ||
if(contentType.indexOf('image/') === 0) { | ||
return createResponseData(url, false, '', '', contentType, 'photo', [url]) | ||
} else { | ||
return createResponseData(url, false, '', '', contentType) | ||
} | ||
} | ||
// static _parseMediaResponse(res, contentType, url) { | ||
// if (contentType.indexOf('image/') === 0) { | ||
// return createResponseData(url, false, '', '', contentType, 'photo', [url]); | ||
// } else { | ||
// return createResponseData(url, false, '', '', contentType); | ||
// } | ||
// } | ||
} |
@@ -1,9 +0,8 @@ | ||
{ | ||
"name": "react-native-link-preview", | ||
"version": "1.0.5", | ||
"version": "1.0.7", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jest" | ||
}, | ||
@@ -16,7 +15,24 @@ "keywords": [ | ||
"peerDependencies": {}, | ||
"repository" : "https://github.com/ospfranco/react-native-link-preview", | ||
"repository": "https://github.com/ospfranco/react-native-link-preview", | ||
"dependencies": { | ||
"cheerio-without-node-native": "^0.20.1", | ||
"autolinker": "^1.2.0" | ||
"autolinker": "^1.2.0", | ||
"cheerio-without-node-native": "^0.20.1" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.22.1", | ||
"babel-eslint": "^7.1.1", | ||
"babel-jest": "^18.0.0", | ||
"babel-loader": "^6.2.10", | ||
"babel-polyfill": "^6.22.0", | ||
"babel-preset-react-native": "^1.9.1", | ||
"eslint": "^3.12.1", | ||
"eslint-config-airbnb": "^13.0.0", | ||
"eslint-loader": "^1.6.1", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-jsx-a11y": "^2.2.3", | ||
"eslint-plugin-react": "^6.9.0", | ||
"expect.js": "^0.3.1", | ||
"jest": "^16.0.2", | ||
"node-fetch": "^1.6.3" | ||
} | ||
} |
@@ -11,6 +11,8 @@ | ||
## Usage | ||
You have to pass an object argument with either a valid URL (no validation on library side yet) or a text that can be parsed | ||
Library exposes just one method: getPreview, you have to pass a string (doesn't matter if it is just an URL or a piece of text that contains an URL), the library will take care of parsing it and returning the info of first valid URL info it finds. | ||
The parsing is done automatically via [Autolinker](https://github.com/gregjacobs/Autolinker.js/), if you have problems getting your URL recognized you can dig their documentation for a valid regex. | ||
```javascript | ||
import LinkPreview from 'react-native-react-native-link-preview'; | ||
import LinkPreview from 'react-native-link-preview'; | ||
@@ -29,20 +31,13 @@ LinkPreview.getPreview('https://www.youtube.com/watch?v=MejbOFk7H6c') | ||
{ | ||
url: "https://www.youtube.com/watch?v=MejbOFk7H6c", | ||
title: "OK Go - Needing/Getting - Official Video - YouTube", | ||
description: "Buy the video on iTunes: https://itunes.apple.com/us/album/needing-getting-bundle-ep/id508124847 See more about the guitars at: http://www.gretschguitars.com...", | ||
images: ["https://i.ytimg.com/vi/MejbOFk7H6c/maxresdefault.jpg"], | ||
mediaType: "video", | ||
title: "OK Go - Needing/Getting - Official Video - YouTube", | ||
url: "https://www.youtube.com/watch?v=MejbOFk7H6c", | ||
videos: undefined | ||
videos: [] | ||
} | ||
``` | ||
## Dependencies | ||
This library depends on: | ||
- [Autolinker](https://github.com/gregjacobs/Autolinker.js/) | ||
- [cheerio-without-node-native](https://github.com/oyyd/cheerio-without-node-native) | ||
Check them out, both pretty cool | ||
## License | ||
MIT license |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
139324
15
145
1
15
42
2