Skip to content

Commit

Permalink
Prompt before opening link
Browse files Browse the repository at this point in the history
Since `[link](http://google.com)` can hide the link destination, I think we should prompt the user before navigating
  • Loading branch information
Ashoat committed Jul 19, 2020
1 parent 3bf844b commit afcb33e
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions native/markdown/rules.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import type { StyleSheetOf } from '../themes/colors';
import type { MarkdownStyles } from './styles';

import * as React from 'react';
import { Text, Linking } from 'react-native';
import { Text, Linking, Alert } from 'react-native';
import * as SimpleMarkdown from 'simple-markdown';

import { urlRegex } from 'lib/shared/markdown';
import { normalizeURL } from 'lib/utils/url-utils';

type MarkdownRuleSpec = {|
+simpleMarkdownRules: SimpleMarkdown.ParserRules,
Expand All @@ -17,6 +18,28 @@ export type MarkdownRules = (
styles: StyleSheetOf<MarkdownStyles>,
) => MarkdownRuleSpec;

function displayLinkPrompt(inputURL: string) {
const url = normalizeURL(inputURL);
const onConfirm = () => {
Linking.openURL(url);
};

let displayURL = url.substring(0, 64);
if (url.length > displayURL.length) {
displayURL += '…';
}

Alert.alert(
'External link',
`You sure you want to open this link?\n\n${displayURL}`,
[
{ text: 'Cancel', style: 'cancel' },
{ text: 'Open', onPress: onConfirm },
],
{ cancelable: true },
);
}

// Entry requires a seamless transition between Markdown and TextInput
// components, so we can't do anything that would change the position of text
function inlineMarkdownRules(
Expand All @@ -39,9 +62,7 @@ function inlineMarkdownRules(
output: SimpleMarkdown.Output<SimpleMarkdown.ReactElement>,
state: SimpleMarkdown.State,
) {
const onPressLink = () => {
Linking.openURL(node.target);
};
const onPressLink = () => displayLinkPrompt(node.target);
return (
<Text key={state.key} style={styles.link} onPress={onPressLink}>
{output(node.content, state)}
Expand Down

0 comments on commit afcb33e

Please sign in to comment.