Skip to content

Commit

Permalink
Force url's to be normalized (mattermost#1058)
Browse files Browse the repository at this point in the history
* Force url's to be normalized

* Refactor and move behind config flag
  • Loading branch information
csduarte authored and jarredwitt committed Oct 26, 2017
1 parent 6b132cc commit 582db04
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
50 changes: 41 additions & 9 deletions app/components/markdown/markdown_link.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React, {PureComponent} from 'react';
import React, {Children, PureComponent} from 'react';
import PropTypes from 'prop-types';
import {Linking, Text} from 'react-native';
import urlParse from 'url-parse';

import CustomPropTypes from 'app/constants/custom_prop_types';
import Config from 'assets/config';

export default class MarkdownLink extends PureComponent {
static propTypes = {
Expand All @@ -20,13 +22,7 @@ export default class MarkdownLink extends PureComponent {

handlePress = () => {
// Android doesn't like the protocol being upper case
let url = this.props.href;

const index = url.indexOf(':');
if (index !== -1) {
const protocol = url.substring(0, index);
url = protocol.toLowerCase() + url.substring(index);
}
const url = this.props.href;

Linking.canOpenURL(url).then((supported) => {
if (supported) {
Expand All @@ -35,13 +31,49 @@ export default class MarkdownLink extends PureComponent {
});
};

parseLinkLiteral = (literal) => {
let nextLiteral = literal;

const WWW_REGEX = /\b^(?:www.)/i;
if (nextLiteral.match(WWW_REGEX)) {
nextLiteral = literal.replace(WWW_REGEX, 'www.');
}

const parsed = urlParse(nextLiteral, {});

return parsed.href;
}

parseChildren = () => {
return Children.map(this.props.children, (child) => {
if (!child.props.literal || typeof child.props.literal !== 'string' || (child.props.context && child.props.context.length && !child.props.context.includes('link'))) {
return child;
}

const {props, ...otherChildProps} = child;
const {literal, ...otherProps} = props;

const nextProps = {
literal: this.parseLinkLiteral(literal),
...otherProps
};

return {
props: nextProps,
...otherChildProps
};
});
}

render() {
const children = Config.ExperimentalNormalizeMarkdownLinks ? this.parseChildren() : this.props.children;

return (
<Text
onPress={this.handlePress}
onLongPress={this.props.onLongPress}
>
{this.props.children}
{children}
</Text>
);
}
Expand Down
4 changes: 3 additions & 1 deletion assets/base/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
"console": true
}
},


"ExperimentalNormalizeMarkdownLinks": false,

"AutoSelectServerUrl": false,

"EnableMobileClientUpgrade": false,
Expand Down

0 comments on commit 582db04

Please sign in to comment.