Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/components/Activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
<Text key={i} onPress={() => Linking.openURL(url)} style={styles.url}>
{tokens[i].slice(0, 20)}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import PropTypes from 'prop-types';
import { buildStylesheet } from '../styles';

import _ from 'lodash';
import { sanitizeUrlForLinking } from '../utils';

/**
* Card element
Expand All @@ -18,7 +19,7 @@ const Card = (props) => {
return (
<TouchableOpacity
onPress={() => {
Linking.openURL(url);
Linking.openURL(sanitizeUrlForLinking(url));
}}
style={styles.container}
>
Expand Down
3 changes: 1 addition & 2 deletions src/components/StatusUpdateForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks to @AnatolyRugalev !! Works pretty well - https://regex101.com/r/3ms9WF/1/

class StatusUpdateForm extends React.Component {
static defaultProps = {
feedGroup: 'user',
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down