Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 4602023

Browse files
authored
Merge pull request #2565 from withspectrum/linkification-god-dammit
Fix linkification
2 parents 5225f48 + a220c0a commit 4602023

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

shared/clients/draft-js/links-decorator/core.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow
22
import React from 'react';
3-
import findWithRegex from 'find-with-regex';
4-
import { URL } from 'shared/regexps';
3+
import linkStrategy from 'draft-js-linkify-plugin/lib/linkStrategy';
54
import normalizeUrl from '../../../normalize-url';
65
import type { ContentBlock } from 'draft-js/lib/ContentBlock';
76
import type { ComponentType, Node } from 'react';
@@ -20,10 +19,7 @@ let i = 0;
2019
const createLinksDecorator = (
2120
Component: ComponentType<LinksDecoratorComponentProps>
2221
) => ({
23-
strategy: (
24-
contentBlock: ContentBlock,
25-
callback: (...args?: Array<any>) => any
26-
) => findWithRegex(URL, contentBlock, callback),
22+
strategy: linkStrategy,
2723
component: ({ decoratedText, children }: DecoratorComponentProps) => (
2824
<Component
2925
href={normalizeUrl(decoratedText)}

shared/normalize-url.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// @flow
22

3-
const PROTOCOL = /(http(s?)):\/\//gi;
3+
const STARTS_WITH_PROTOCOL = /^https?:\/\//i;
44

5-
const addProtocolToString = (string: string) => {
6-
// if the string starts with http or https, we are good
7-
if (PROTOCOL.test(string)) {
8-
return string;
5+
// Note(@mxstbr): This method assumes that a string passed into it is already verified to be an URL
6+
// it'll just append https:// to anything that doesn't look like an URL
7+
const addProtocolToString = (url: string) => {
8+
if (STARTS_WITH_PROTOCOL.test(url)) {
9+
return url;
910
} else {
10-
// otherwise it doesn't start with a protocol, prepend http
11-
return `http://${string}`;
11+
return `https://${url}`;
1212
}
1313
};
1414

shared/test/normalize-url.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// @flow
2+
import normalizeUrl from '../normalize-url';
3+
4+
type Input = string;
5+
type Output = string;
6+
7+
// If only Input is provided it tests for input === output
8+
type TestCase = [Input, Output];
9+
10+
type TestCases = {
11+
[testName: string]: TestCase,
12+
};
13+
14+
const testCases: TestCases = {
15+
'should do nothing to https URLs': [
16+
'https://github.com',
17+
'https://github.com',
18+
],
19+
'should do nothing to http URLs': ['http://github.com', 'http://github.com'],
20+
'should add https:// in front of URLs without protocol': [
21+
'github.com',
22+
'https://github.com',
23+
],
24+
};
25+
26+
Object.keys(testCases).forEach(name => {
27+
it(name, () => {
28+
expect(normalizeUrl(testCases[name][0])).toEqual(testCases[name][1]);
29+
});
30+
});

0 commit comments

Comments
 (0)