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; }