Skip to content

Commit

Permalink
Merge pull request #315 from webkom/better-links
Browse files Browse the repository at this point in the history
Allow mailto links & improve linkinput protocol prefixing
  • Loading branch information
LudvigHz authored Mar 16, 2021
2 parents cc16287 + 44b180c commit b2aa96b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"dependencies": {
"classnames": "^2.2.6",
"escape-html": "^1.0.3",
"is-url": "^1.2.4",
"lodash": "^4.17.15",
"react-dropzone": "^11.0.1",
"react-image-crop": "^8.4.2",
Expand Down
14 changes: 4 additions & 10 deletions src/components/LinkInput.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useRef, useState, useEffect } from 'react';
import { Node, NodeEntry } from 'slate';
import isUrl from 'is-url';
import isUrl, { prependHttps } from '../utils/isUrl';
import Modal from './Modal';

interface LinkInputProps {
Expand Down Expand Up @@ -44,16 +44,10 @@ const LinkInput = (props: LinkInputProps): JSX.Element => {
};

const onChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
const prependProtocol = (url: string): string => {
if (!/^(?:f|ht)tps?:\/\//.test(url)) {
url = 'https://' + url;
}
return url;
};
if (e.target.value.length <= url.length) {
setUrl(e.target.value);
if (e.target.value.length >= url.length) {
setUrl(prependHttps(e.target.value));
} else {
setUrl(prependProtocol(e.currentTarget.value));
setUrl(e.target.value);
}
};

Expand Down
17 changes: 17 additions & 0 deletions src/utils/isUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const protocolRe = /^(?:f|ht)tps?:\/\//;
const hostnameRe = /([a-zA-Z]\.?)+\.([a-zA-Z]){2,}/;

const urlRe = new RegExp(protocolRe.source + hostnameRe.source);
const mailRe = new RegExp('^mailto:([a-zA-Z](\\.|\\+)?)+@' + hostnameRe.source);
const onlyHostnameRe = new RegExp('^' + hostnameRe.source);

const isUrl = (url: string): boolean => urlRe.test(url) || mailRe.test(url);

export const prependHttps = (hostname: string): string => {
if (onlyHostnameRe.test(hostname)) {
return 'https://' + hostname;
}
return hostname;
};

export default isUrl;
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3896,11 +3896,6 @@ is-symbol@^1.0.2:
dependencies:
has-symbols "^1.0.1"

is-url@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52"
integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==

is-windows@^1.0.1, is-windows@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/is-windows/-/is-windows-1.0.2.tgz#d1850eb9791ecd18e6182ce12a30f396634bb19d"
Expand Down

0 comments on commit b2aa96b

Please sign in to comment.