[FIX] Encode Image URI (#1909)

* [FIX] Encode Image URI

* [FIX] Check if Image is Valid

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Djorkaeff Alexandre 2020-03-20 13:26:50 -03:00 committed by GitHub
parent 87724ae6d4
commit ce1a2d5374
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -22,6 +22,7 @@ import MarkdownTableCell from './TableCell';
import mergeTextNodes from './mergeTextNodes';
import styles from './styles';
import { isValidURL } from '../../utils/url';
// Support <http://link|Text>
const formatText = text => text.replace(
@ -278,7 +279,18 @@ class Markdown extends PureComponent {
);
}
renderImage = ({ src }) => <Image style={styles.inlineImage} source={{ uri: src }} />;
renderImage = ({ src }) => {
if (!isValidURL(src)) {
return null;
}
return (
<Image
style={styles.inlineImage}
source={{ uri: encodeURI(src) }}
/>
);
}
renderEditedIndicator = () => {
const { theme } = this.props;

9
app/utils/url.js Normal file
View File

@ -0,0 +1,9 @@
export const isValidURL = (url) => {
const pattern = new RegExp('^(https?:\\/\\/)?' // protocol
+ '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' // domain name
+ '((\\d{1,3}\\.){3}\\d{1,3}))' // OR ip (v4) address
+ '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' // port and path
+ '(\\?[;&a-z\\d%_.~+=-]*)?' // query string
+ '(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator
return !!pattern.test(url);
};