Skip to content

Commit 95260e8

Browse files
committed
Add support for web fallback URLs for Instagram/Twitter links
1 parent e5ee9c3 commit 95260e8

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class MyComponent extends Component {
2020
render() {
2121
return (
2222
<AutoLink
23-
text="This is the string to parse for urls (https://github.com/joshswan/react-native-autolink), phone numbers (415-555-5555), emails ([email protected]), twitter handles (@twitter), and hashtags (#exciting)"
23+
text="This is the string to parse for urls (https://github.com/joshswan/react-native-autolink), phone numbers (415-555-5555), emails ([email protected]), mentions/handles (@twitter), and hashtags (#exciting)"
2424
hashtag="instagram"
25-
twitter />
25+
mention="twitter" />
2626
);
2727
}
2828
}
@@ -45,6 +45,7 @@ class MyComponent extends Component {
4545
| `renderLink` | `function` | | Custom render function for rendering link nodes. Arguments: `text:String`, `link:String`, `match:Object`. *See [Autolinker.js match object](http://gregjacobs.github.io/Autolinker.js/docs/#!/api/Autolinker.match.Match) for more information about the match object.* |
4646
| `truncate` | `Number` | `32` | Truncate long link text for display (e.g. `https://www.google.com/../something.html`). Possible values: `0` to disable, `1+` to truncate to that maximum length. |
4747
| `truncateChars` | `String` | `..` | Characters to replace truncated url segments with, if enabled. |
48+
| `webFallback` | `Boolean` | Android: `true` iOS: `false` | Link to web versions of Instagram/Twitter for hashtag and mention links when users don't have the respective app installed. **Requires `LSApplicationQueriesSchemes` on iOS. See: https://facebook.github.io/react-native/docs/linking.html |
4849

4950
**Any other props will be passed through to the main Text node (e.g. style, numberOfLines).**
5051

src/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import React, {Component, PropTypes, createElement} from 'react';
1111
import Autolinker from 'autolinker';
12-
import {Linking, StyleSheet, Text} from 'react-native';
12+
import {Linking, Platform, StyleSheet, Text} from 'react-native';
1313

1414
export default class Autolink extends Component {
1515
getURL(match) {
@@ -23,9 +23,9 @@ export default class Autolink extends Component {
2323

2424
switch (this.props.hashtag) {
2525
case 'instagram':
26-
return `instagram://tag?name=${tag}`;
26+
return this.selectURL(`instagram://tag?name=${tag}`, `https://www.instagram.com/explore/tags/${tag}/`);
2727
case 'twitter':
28-
return `twitter://search?query=%23${tag}`;
28+
return this.selectURL(`twitter://search?query=%23${tag}`, `https://twitter.com/hashtag/${tag}`);
2929
default:
3030
return match.getMatchedText();
3131
}
@@ -34,9 +34,9 @@ export default class Autolink extends Component {
3434

3535
switch (this.props.mention) {
3636
case 'instagram':
37-
return `instagram://user?username=${mention}`;
37+
return this.selectURL(`instagram://user?username=${mention}`, `https://www.instagram.com/${mention}/`);
3838
case 'twitter':
39-
return `twitter://user?screen_name=${mention}`;
39+
return this.selectURL(`twitter://user?screen_name=${mention}`, `https://twitter.com/${mention}`);
4040
default:
4141
return match.getMatchedText();
4242
}
@@ -49,6 +49,14 @@ export default class Autolink extends Component {
4949
}
5050
}
5151

52+
selectURL(url, fallback) {
53+
if (this.props.webFallback) {
54+
return Linking.canOpenURL(url) ? url : fallback;
55+
}
56+
57+
return url;
58+
}
59+
5260
_onPress(url, match) {
5361
if (this.props.onPress) {
5462
this.props.onPress(url, match);
@@ -88,6 +96,7 @@ export default class Autolink extends Component {
8896
truncateChars,
8997
twitter,
9098
url,
99+
webFallback,
91100
...other,
92101
} = this.props;
93102

@@ -170,6 +179,7 @@ Autolink.defaultProps = {
170179
truncateChars: '..',
171180
twitter: false,
172181
url: true,
182+
webFallback: Platform.OS !== 'ios', // iOS requires LSApplicationQueriesSchemes for Linking.canOpenURL
173183
};
174184

175185
Autolink.propTypes = {
@@ -187,4 +197,5 @@ Autolink.propTypes = {
187197
truncateChars: PropTypes.string,
188198
twitter: PropTypes.bool,
189199
url: PropTypes.bool,
200+
webFallback: PropTypes.bool,
190201
};

0 commit comments

Comments
 (0)