From 2fd50f9c33ee1742a561e0f6df2e15bd750f1f88 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Wed, 18 Mar 2020 18:14:39 +0300 Subject: [PATCH] fix(v2): make correct internal link check (#2424) --- .../client/exports/__tests__/isInternalUrl.ts | 30 +++++++++++++++++++ .../src/client/exports/isInternalUrl.js | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 packages/docusaurus/src/client/exports/__tests__/isInternalUrl.ts diff --git a/packages/docusaurus/src/client/exports/__tests__/isInternalUrl.ts b/packages/docusaurus/src/client/exports/__tests__/isInternalUrl.ts new file mode 100644 index 0000000000..bc0833dd89 --- /dev/null +++ b/packages/docusaurus/src/client/exports/__tests__/isInternalUrl.ts @@ -0,0 +1,30 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import isInternalUrl from '../isInternalUrl'; + +describe('isInternalUrl', () => { + test('should be true for root relative links', () => { + expect(isInternalUrl('/foo/bar')).toBeTruthy(); + }); + + test('should be true for relative links', () => { + expect(isInternalUrl('foo/bar')).toBeTruthy(); + }); + + test('should be false for HTTP links', () => { + expect(isInternalUrl('http://foo.com')).toBeFalsy(); + }); + + test('should be false for HTTPS links', () => { + expect(isInternalUrl('https://foo.com')).toBeFalsy(); + }); + + test('should be false for whatever protocol links', () => { + expect(isInternalUrl('//foo.com')).toBeFalsy(); + }); +}); diff --git a/packages/docusaurus/src/client/exports/isInternalUrl.js b/packages/docusaurus/src/client/exports/isInternalUrl.js index ee15f12a60..182de277fe 100644 --- a/packages/docusaurus/src/client/exports/isInternalUrl.js +++ b/packages/docusaurus/src/client/exports/isInternalUrl.js @@ -6,5 +6,5 @@ */ export default function isInternalUrl(url) { - return /^\/(?!\/)/.test(url); + return /^(https?:|\/\/)/.test(url) === false; }