diff --git a/src/components/Activity.js b/src/components/Activity.js index 80cb9843..e87b4efd 100644 --- a/src/components/Activity.js +++ b/src/components/Activity.js @@ -18,7 +18,7 @@ import _ from 'lodash'; import UserBar from './UserBar'; import Card from './Card'; -import { smartRender } from '../utils'; +import { sanitizeUrlForLinking, smartRender } from '../utils'; /** * Renders feed activities @@ -188,7 +188,8 @@ export default class Activity extends React.Component { Object.keys(activity.attachments.og).length > 0 && tokens[i] === activity.attachments.og.url ) { - const url = activity.attachments.og.url; + const url = sanitizeUrlForLinking(activity.attachments.og.url); + rendered.push( Linking.openURL(url)} style={styles.url}> {tokens[i].slice(0, 20)} diff --git a/src/components/Card.js b/src/components/Card.js index 35a5f194..b8929012 100644 --- a/src/components/Card.js +++ b/src/components/Card.js @@ -6,6 +6,7 @@ import PropTypes from 'prop-types'; import { buildStylesheet } from '../styles'; import _ from 'lodash'; +import { sanitizeUrlForLinking } from '../utils'; /** * Card element @@ -18,7 +19,7 @@ const Card = (props) => { return ( { - Linking.openURL(url); + Linking.openURL(sanitizeUrlForLinking(url)); }} style={styles.container} > diff --git a/src/components/StatusUpdateForm.js b/src/components/StatusUpdateForm.js index 917aae5f..2d6a64a5 100644 --- a/src/components/StatusUpdateForm.js +++ b/src/components/StatusUpdateForm.js @@ -29,8 +29,7 @@ const ImageState = Object.freeze({ UPLOAD_FAILED: Symbol('upload_failed'), }); -const urlRegex = /(https?:\/\/[^\s]+)/gi; - +const urlRegex = /(?:\s|^)((?:https?:\/\/)?(?:[a-z0-9-]+(?:\.[a-z0-9-]+)+)(?::[0-9]+)?(?:\/(?:[^\s]+)?)?)/g; class StatusUpdateForm extends React.Component { static defaultProps = { feedGroup: 'user', diff --git a/src/utils.js b/src/utils.js index 4363cbfc..c5237dfc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -26,6 +26,15 @@ export function humanizeTimestamp(timestamp, tDateTimeParser) { return time.from(now); } +// https://reactnative.dev/docs/linking +export const sanitizeUrlForLinking = (url) => { + if (!/^https?:\/\//.test(url)) { + url = `https://${url}`; + } + + return url.replace(/(www\.)/, ''); +}; + export const smartRender = (ElementOrComponentOrLiteral, props, fallback) => { if (ElementOrComponentOrLiteral === undefined) { ElementOrComponentOrLiteral = fallback;