diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts index 2e5e770714..014aa7c10b 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts @@ -8,7 +8,7 @@ import {PluginContext, UserPluginOptions} from '../types'; import collectRedirects from '../collectRedirects'; import normalizePluginOptions from '../normalizePluginOptions'; -import {removeTrailingSlash} from '../utils'; +import {removeTrailingSlash} from '@docusaurus/utils'; function createTestPluginContext( options?: UserPluginOptions, diff --git a/packages/docusaurus-plugin-client-redirects/src/__tests__/utils.test.ts b/packages/docusaurus-plugin-client-redirects/src/__tests__/utils.test.ts deleted file mode 100644 index 8e7cc6a19d..0000000000 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/utils.test.ts +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright (c) 2017-present, Facebook, Inc. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -import { - addTrailingSlash, - removeTrailingSlash, - removeSuffix, - getFilePathForRoutePath, -} from '../utils'; - -describe('addTrailingSlash', () => { - test('should no-op', () => { - expect(addTrailingSlash('/abcd/')).toEqual('/abcd/'); - }); - test('should add /', () => { - expect(addTrailingSlash('/abcd')).toEqual('/abcd/'); - }); -}); - -describe('removeTrailingSlash', () => { - test('should no-op', () => { - expect(removeTrailingSlash('/abcd')).toEqual('/abcd'); - }); - test('should remove /', () => { - expect(removeTrailingSlash('/abcd/')).toEqual('/abcd'); - }); -}); - -describe('removeSuffix', () => { - test('should no-op 1', () => { - expect(removeSuffix('abcdef', 'ijk')).toEqual('abcdef'); - }); - test('should no-op 2', () => { - expect(removeSuffix('abcdef', 'abc')).toEqual('abcdef'); - }); - test('should no-op 3', () => { - expect(removeSuffix('abcdef', '')).toEqual('abcdef'); - }); - test('should remove suffix', () => { - expect(removeSuffix('abcdef', 'ef')).toEqual('abcd'); - }); -}); - -describe('getFilePathForRoutePath', () => { - test('works for /', () => { - expect(getFilePathForRoutePath('/')).toEqual('/index.html'); - }); - test('works for /somePath', () => { - expect(getFilePathForRoutePath('/somePath')).toEqual( - '/somePath/index.html', - ); - }); - test('works for /somePath/', () => { - expect(getFilePathForRoutePath('/somePath/')).toEqual( - '/somePath/index.html', - ); - }); -}); diff --git a/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts b/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts index b82999c07a..df4a7110f2 100644 --- a/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts +++ b/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts @@ -6,7 +6,7 @@ */ import {flatten} from 'lodash'; -import {removeSuffix} from './utils'; +import {removeSuffix} from '@docusaurus/utils'; import {RedirectMetadata} from './types'; const ExtensionAdditionalMessage = diff --git a/packages/docusaurus-plugin-client-redirects/src/utils.ts b/packages/docusaurus-plugin-client-redirects/src/utils.ts deleted file mode 100644 index 8c082c25e3..0000000000 --- a/packages/docusaurus-plugin-client-redirects/src/utils.ts +++ /dev/null @@ -1,30 +0,0 @@ -/** - * 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 path from 'path'; - -export function addTrailingSlash(str: string) { - return str.endsWith('/') ? str : `${str}/`; -} - -export function removeTrailingSlash(str: string) { - return removeSuffix(str, '/'); -} - -export function removeSuffix(str: string, suffix: string) { - if (suffix === '') { - return str; // always returns "" otherwise! - } - return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str; -} - -// TODO does this function already exist? -export function getFilePathForRoutePath(routePath: string) { - const fileName = path.basename(routePath); - const filePath = path.dirname(routePath); - return path.join(filePath, `${fileName}/index.html`); -} diff --git a/packages/docusaurus-plugin-client-redirects/src/writeRedirectFiles.ts b/packages/docusaurus-plugin-client-redirects/src/writeRedirectFiles.ts index 46e0f37947..d8b41a3de9 100644 --- a/packages/docusaurus-plugin-client-redirects/src/writeRedirectFiles.ts +++ b/packages/docusaurus-plugin-client-redirects/src/writeRedirectFiles.ts @@ -15,7 +15,7 @@ import { addTrailingSlash, getFilePathForRoutePath, removeTrailingSlash, -} from './utils'; +} from '@docusaurus/utils'; export type WriteFilesPluginContext = Pick; diff --git a/packages/docusaurus-utils/src/__tests__/index.test.ts b/packages/docusaurus-utils/src/__tests__/index.test.ts index f6a0543207..29cde6684a 100644 --- a/packages/docusaurus-utils/src/__tests__/index.test.ts +++ b/packages/docusaurus-utils/src/__tests__/index.test.ts @@ -19,6 +19,10 @@ import { aliasedSitePath, createExcerpt, isValidPathname, + addTrailingSlash, + removeTrailingSlash, + removeSuffix, + getFilePathForRoutePath, } from '../index'; describe('load utils', () => { @@ -381,3 +385,52 @@ describe('load utils', () => { expect(isValidPathname('//hey')).toBe(false); }); }); + +describe('addTrailingSlash', () => { + test('should no-op', () => { + expect(addTrailingSlash('/abcd/')).toEqual('/abcd/'); + }); + test('should add /', () => { + expect(addTrailingSlash('/abcd')).toEqual('/abcd/'); + }); +}); + +describe('removeTrailingSlash', () => { + test('should no-op', () => { + expect(removeTrailingSlash('/abcd')).toEqual('/abcd'); + }); + test('should remove /', () => { + expect(removeTrailingSlash('/abcd/')).toEqual('/abcd'); + }); +}); + +describe('removeSuffix', () => { + test('should no-op 1', () => { + expect(removeSuffix('abcdef', 'ijk')).toEqual('abcdef'); + }); + test('should no-op 2', () => { + expect(removeSuffix('abcdef', 'abc')).toEqual('abcdef'); + }); + test('should no-op 3', () => { + expect(removeSuffix('abcdef', '')).toEqual('abcdef'); + }); + test('should remove suffix', () => { + expect(removeSuffix('abcdef', 'ef')).toEqual('abcd'); + }); +}); + +describe('getFilePathForRoutePath', () => { + test('works for /', () => { + expect(getFilePathForRoutePath('/')).toEqual('/index.html'); + }); + test('works for /somePath', () => { + expect(getFilePathForRoutePath('/somePath')).toEqual( + '/somePath/index.html', + ); + }); + test('works for /somePath/', () => { + expect(getFilePathForRoutePath('/somePath/')).toEqual( + '/somePath/index.html', + ); + }); +}); diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 8ecc4f9f9f..773fe6849b 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -339,3 +339,24 @@ export function isValidPathname(str: string): boolean { return false; } } + +export function addTrailingSlash(str: string) { + return str.endsWith('/') ? str : `${str}/`; +} + +export function removeTrailingSlash(str: string) { + return removeSuffix(str, '/'); +} + +export function removeSuffix(str: string, suffix: string) { + if (suffix === '') { + return str; // always returns "" otherwise! + } + return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str; +} + +export function getFilePathForRoutePath(routePath: string) { + const fileName = path.basename(routePath); + const filePath = path.dirname(routePath); + return path.join(filePath, `${fileName}/index.html`); +}