From 1cea07c97edd6613a595d1e5bf37d49fcec7eda0 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 21 Mar 2024 14:24:41 +0100 Subject: [PATCH 1/3] refactor(utils): remove duplicated function --- .../src/collectRedirects.ts | 4 ++-- .../src/extensionRedirects.ts | 7 ++----- packages/docusaurus-plugin-content-docs/package.json | 1 + .../src/sidebars/generator.ts | 2 +- packages/docusaurus-plugin-content-docs/src/slug.ts | 2 +- .../docusaurus-utils-common/src/applyTrailingSlash.ts | 8 ++++---- packages/docusaurus-utils-common/src/index.ts | 1 + packages/docusaurus-utils/package.json | 1 + packages/docusaurus-utils/src/__tests__/urlUtils.test.ts | 2 +- packages/docusaurus-utils/src/index.ts | 1 - packages/docusaurus-utils/src/urlUtils.ts | 8 +------- packages/docusaurus/package.json | 2 +- packages/docusaurus/src/server/brokenLinks.ts | 2 +- packages/docusaurus/src/server/configValidation.ts | 2 +- 14 files changed, 18 insertions(+), 25 deletions(-) diff --git a/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts b/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts index 92a29a6ed307..cf31aca0919c 100644 --- a/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts +++ b/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts @@ -7,8 +7,8 @@ import _ from 'lodash'; import logger from '@docusaurus/logger'; -import {addTrailingSlash, removeTrailingSlash} from '@docusaurus/utils'; -import {applyTrailingSlash} from '@docusaurus/utils-common'; +import {removeTrailingSlash} from '@docusaurus/utils'; +import {applyTrailingSlash, addTrailingSlash} from '@docusaurus/utils-common'; import { createFromExtensionsRedirects, createToExtensionsRedirects, diff --git a/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts b/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts index 86fa4aeb48de..178450d6cd9c 100644 --- a/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts +++ b/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts @@ -5,11 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -import { - addTrailingSlash, - removeSuffix, - removeTrailingSlash, -} from '@docusaurus/utils'; +import {removeSuffix, removeTrailingSlash} from '@docusaurus/utils'; +import {addTrailingSlash} from '@docusaurus/utils-common'; import type {RedirectItem} from './types'; const ExtensionAdditionalMessage = diff --git a/packages/docusaurus-plugin-content-docs/package.json b/packages/docusaurus-plugin-content-docs/package.json index d7099a125e60..e8349e4fb455 100644 --- a/packages/docusaurus-plugin-content-docs/package.json +++ b/packages/docusaurus-plugin-content-docs/package.json @@ -41,6 +41,7 @@ "@docusaurus/module-type-aliases": "3.0.0", "@docusaurus/types": "3.0.0", "@docusaurus/utils": "3.0.0", + "@docusaurus/utils-common": "3.0.0", "@docusaurus/utils-validation": "3.0.0", "@types/react-router-config": "^5.0.7", "combine-promises": "^1.1.0", diff --git a/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts b/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts index 5a6b325449b1..0edf909bb9c8 100644 --- a/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts +++ b/packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts @@ -8,7 +8,7 @@ import path from 'path'; import _ from 'lodash'; import logger from '@docusaurus/logger'; -import {addTrailingSlash} from '@docusaurus/utils'; +import {addTrailingSlash} from '@docusaurus/utils-common'; import {createDocsByIdIndex, toCategoryIndexMatcherParam} from '../docs'; import type { SidebarItemDoc, diff --git a/packages/docusaurus-plugin-content-docs/src/slug.ts b/packages/docusaurus-plugin-content-docs/src/slug.ts index 130c7a63b1f5..2183f9186e86 100644 --- a/packages/docusaurus-plugin-content-docs/src/slug.ts +++ b/packages/docusaurus-plugin-content-docs/src/slug.ts @@ -7,10 +7,10 @@ import { addLeadingSlash, - addTrailingSlash, isValidPathname, resolvePathname, } from '@docusaurus/utils'; +import {addTrailingSlash} from '@docusaurus/utils-common'; import { DefaultNumberPrefixParser, stripPathNumberPrefixes, diff --git a/packages/docusaurus-utils-common/src/applyTrailingSlash.ts b/packages/docusaurus-utils-common/src/applyTrailingSlash.ts index d8faa439cc5b..7e80992d203d 100644 --- a/packages/docusaurus-utils-common/src/applyTrailingSlash.ts +++ b/packages/docusaurus-utils-common/src/applyTrailingSlash.ts @@ -12,6 +12,10 @@ export type ApplyTrailingSlashParams = Pick< 'trailingSlash' | 'baseUrl' >; +export function addTrailingSlash(str: string): string { + return str.endsWith('/') ? str : `${str}/`; +} + // Trailing slash handling depends in some site configuration options export default function applyTrailingSlash( path: string, @@ -24,10 +28,6 @@ export default function applyTrailingSlash( return path; } - // TODO deduplicate: also present in @docusaurus/utils - function addTrailingSlash(str: string): string { - return str.endsWith('/') ? str : `${str}/`; - } function removeTrailingSlash(str: string): string { return str.endsWith('/') ? str.slice(0, -1) : str; } diff --git a/packages/docusaurus-utils-common/src/index.ts b/packages/docusaurus-utils-common/src/index.ts index 17b69e0727f0..a69d3eca81e3 100644 --- a/packages/docusaurus-utils-common/src/index.ts +++ b/packages/docusaurus-utils-common/src/index.ts @@ -11,6 +11,7 @@ export const blogPostContainerID = '__blog-post-container'; export { default as applyTrailingSlash, + addTrailingSlash, type ApplyTrailingSlashParams, } from './applyTrailingSlash'; export {getErrorCausalChain} from './errorUtils'; diff --git a/packages/docusaurus-utils/package.json b/packages/docusaurus-utils/package.json index b3fa5dee583d..9f7adc9cbe3b 100644 --- a/packages/docusaurus-utils/package.json +++ b/packages/docusaurus-utils/package.json @@ -19,6 +19,7 @@ "license": "MIT", "dependencies": { "@docusaurus/logger": "3.0.0", + "@docusaurus/utils-common": "3.0.0", "@svgr/webpack": "^6.5.1", "escape-string-regexp": "^4.0.0", "file-loader": "^6.2.0", diff --git a/packages/docusaurus-utils/src/__tests__/urlUtils.test.ts b/packages/docusaurus-utils/src/__tests__/urlUtils.test.ts index 7ed1cbbf9cea..8bca1ee126d1 100644 --- a/packages/docusaurus-utils/src/__tests__/urlUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/urlUtils.test.ts @@ -5,12 +5,12 @@ * LICENSE file in the root directory of this source tree. */ +import {addTrailingSlash} from '@docusaurus/utils-common'; import { normalizeUrl, getEditUrl, fileToPath, isValidPathname, - addTrailingSlash, addLeadingSlash, removeTrailingSlash, resolvePathname, diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 2a47cfb6140a..19129803c217 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -51,7 +51,6 @@ export { parseURLPath, serializeURLPath, addLeadingSlash, - addTrailingSlash, removeTrailingSlash, hasSSHProtocol, buildHttpsUrl, diff --git a/packages/docusaurus-utils/src/urlUtils.ts b/packages/docusaurus-utils/src/urlUtils.ts index 8a7af4aa4b6d..dbad79892bb8 100644 --- a/packages/docusaurus-utils/src/urlUtils.ts +++ b/packages/docusaurus-utils/src/urlUtils.ts @@ -6,7 +6,7 @@ */ import resolvePathnameUnsafe from 'resolve-pathname'; -import {addPrefix, addSuffix, removeSuffix} from './jsUtils'; +import {addPrefix, removeSuffix} from './jsUtils'; /** * Much like `path.join`, but much better. Takes an array of URL segments, and @@ -237,12 +237,6 @@ export function addLeadingSlash(str: string): string { return addPrefix(str, '/'); } -// TODO deduplicate: also present in @docusaurus/utils-common -/** Appends a trailing slash to `str`, if one doesn't exist. */ -export function addTrailingSlash(str: string): string { - return addSuffix(str, '/'); -} - /** Removes the trailing slash from `str`. */ export function removeTrailingSlash(str: string): string { return removeSuffix(str, '/'); diff --git a/packages/docusaurus/package.json b/packages/docusaurus/package.json index 104ebb888fbc..6d47141a33c2 100644 --- a/packages/docusaurus/package.json +++ b/packages/docusaurus/package.json @@ -69,8 +69,8 @@ "del": "^6.1.1", "detect-port": "^1.5.1", "escape-html": "^1.0.3", - "eval": "^0.1.8", "eta": "^2.2.0", + "eval": "^0.1.8", "file-loader": "^6.2.0", "fs-extra": "^11.1.1", "html-minifier-terser": "^7.2.0", diff --git a/packages/docusaurus/src/server/brokenLinks.ts b/packages/docusaurus/src/server/brokenLinks.ts index e3ad16926030..d03a4d771ad7 100644 --- a/packages/docusaurus/src/server/brokenLinks.ts +++ b/packages/docusaurus/src/server/brokenLinks.ts @@ -9,13 +9,13 @@ import _ from 'lodash'; import logger from '@docusaurus/logger'; import {matchRoutes as reactRouterMatchRoutes} from 'react-router-config'; import { - addTrailingSlash, parseURLPath, removeTrailingSlash, serializeURLPath, flattenRoutes, type URLPath, } from '@docusaurus/utils'; +import {addTrailingSlash} from '@docusaurus/utils-common'; import type {RouteConfig, ReportingSeverity} from '@docusaurus/types'; function matchRoutes(routeConfig: RouteConfig[], pathname: string) { diff --git a/packages/docusaurus/src/server/configValidation.ts b/packages/docusaurus/src/server/configValidation.ts index eafabc3cedad..7d3d1ac258dd 100644 --- a/packages/docusaurus/src/server/configValidation.ts +++ b/packages/docusaurus/src/server/configValidation.ts @@ -10,10 +10,10 @@ import { DEFAULT_STATIC_DIR_NAME, DEFAULT_I18N_DIR_NAME, addLeadingSlash, - addTrailingSlash, removeTrailingSlash, } from '@docusaurus/utils'; import {Joi, printWarning} from '@docusaurus/utils-validation'; +import {addTrailingSlash} from '@docusaurus/utils-common'; import type { DocusaurusConfig, I18nConfig, From a35b949f03e471f85dc758e7a2242d8199383346 Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Thu, 21 Mar 2024 20:04:11 +0100 Subject: [PATCH 2/3] refactor: review comments --- .../src/__tests__/collectRedirects.test.ts | 2 +- .../src/collectRedirects.ts | 7 ++- .../src/extensionRedirects.ts | 7 ++- .../src/index.ts | 3 +- .../src/slug.ts | 8 +--- .../src/__tests__/applyTrailingSlash.test.ts | 45 +++++++++++++++++++ .../src/applyTrailingSlash.ts | 27 +++++++++-- packages/docusaurus-utils-common/src/index.ts | 4 ++ .../docusaurus-utils-validation/package.json | 1 + .../src/validationSchemas.ts | 8 +--- packages/docusaurus-utils/package.json | 1 - .../src/__tests__/jsUtils.test.ts | 12 ----- .../src/__tests__/urlUtils.test.ts | 30 ------------- packages/docusaurus-utils/src/index.ts | 9 +--- packages/docusaurus-utils/src/jsUtils.ts | 14 ------ packages/docusaurus-utils/src/urlUtils.ts | 11 ----- packages/docusaurus/src/server/brokenLinks.ts | 3 +- .../docusaurus/src/server/configValidation.ts | 8 ++-- 18 files changed, 98 insertions(+), 102 deletions(-) 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 e303f7289b92..11b0d3455061 100644 --- a/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts +++ b/packages/docusaurus-plugin-client-redirects/src/__tests__/collectRedirects.test.ts @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {removeTrailingSlash} from '@docusaurus/utils'; +import {removeTrailingSlash} from '@docusaurus/utils-common'; import {normalizePluginOptions} from '@docusaurus/utils-validation'; import collectRedirects from '../collectRedirects'; import {validateOptions} from '../options'; diff --git a/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts b/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts index cf31aca0919c..1e3ac44e4405 100644 --- a/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts +++ b/packages/docusaurus-plugin-client-redirects/src/collectRedirects.ts @@ -7,8 +7,11 @@ import _ from 'lodash'; import logger from '@docusaurus/logger'; -import {removeTrailingSlash} from '@docusaurus/utils'; -import {applyTrailingSlash, addTrailingSlash} from '@docusaurus/utils-common'; +import { + applyTrailingSlash, + addTrailingSlash, + removeTrailingSlash, +} from '@docusaurus/utils-common'; import { createFromExtensionsRedirects, createToExtensionsRedirects, diff --git a/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts b/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts index 178450d6cd9c..3fc48cdfad49 100644 --- a/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts +++ b/packages/docusaurus-plugin-client-redirects/src/extensionRedirects.ts @@ -5,8 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -import {removeSuffix, removeTrailingSlash} from '@docusaurus/utils'; -import {addTrailingSlash} from '@docusaurus/utils-common'; +import { + addTrailingSlash, + removeSuffix, + removeTrailingSlash, +} from '@docusaurus/utils-common'; import type {RedirectItem} from './types'; const ExtensionAdditionalMessage = diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index 5b64560bac04..80612b23b22c 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -5,7 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -import {removePrefix, addLeadingSlash} from '@docusaurus/utils'; +import {removePrefix} from '@docusaurus/utils'; +import {addLeadingSlash} from '@docusaurus/utils-common'; import collectRedirects from './collectRedirects'; import writeRedirectFiles, { toRedirectFiles, diff --git a/packages/docusaurus-plugin-content-docs/src/slug.ts b/packages/docusaurus-plugin-content-docs/src/slug.ts index 2183f9186e86..65887029e8bd 100644 --- a/packages/docusaurus-plugin-content-docs/src/slug.ts +++ b/packages/docusaurus-plugin-content-docs/src/slug.ts @@ -5,12 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -import { - addLeadingSlash, - isValidPathname, - resolvePathname, -} from '@docusaurus/utils'; -import {addTrailingSlash} from '@docusaurus/utils-common'; +import {isValidPathname, resolvePathname} from '@docusaurus/utils'; +import {addLeadingSlash, addTrailingSlash} from '@docusaurus/utils-common'; import { DefaultNumberPrefixParser, stripPathNumberPrefixes, diff --git a/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts b/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts index 4096b8273fca..225d235e07e7 100644 --- a/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts +++ b/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts @@ -6,7 +6,11 @@ */ import applyTrailingSlash, { + addTrailingSlash, type ApplyTrailingSlashParams, + addLeadingSlash, + removeSuffix, + removeTrailingSlash, } from '../applyTrailingSlash'; function params( @@ -176,3 +180,44 @@ describe('applyTrailingSlash', () => { ).toBe('https://xyz.com/abc/?search#anchor'); }); }); + +describe('addTrailingSlash', () => { + it('is no-op for path with trailing slash', () => { + expect(addTrailingSlash('/abcd/')).toBe('/abcd/'); + }); + it('adds / for path without trailing slash', () => { + expect(addTrailingSlash('/abcd')).toBe('/abcd/'); + }); +}); + +describe('addLeadingSlash', () => { + it('is no-op for path with leading slash', () => { + expect(addLeadingSlash('/abc')).toBe('/abc'); + }); + it('adds / for path without leading slash', () => { + expect(addLeadingSlash('abc')).toBe('/abc'); + }); +}); + +describe('removeTrailingSlash', () => { + it('is no-op for path without trailing slash', () => { + expect(removeTrailingSlash('/abcd')).toBe('/abcd'); + }); + it('removes / for path with trailing slash', () => { + expect(removeTrailingSlash('/abcd/')).toBe('/abcd'); + }); +}); + +describe('removeSuffix', () => { + it("is no-op when suffix doesn't exist", () => { + expect(removeSuffix('abcdef', 'ijk')).toBe('abcdef'); + expect(removeSuffix('abcdef', 'abc')).toBe('abcdef'); + expect(removeSuffix('abcdef', '')).toBe('abcdef'); + }); + it('removes suffix', () => { + expect(removeSuffix('abcdef', 'ef')).toBe('abcd'); + }); + it('removes empty suffix', () => { + expect(removeSuffix('abcdef', '')).toBe('abcdef'); + }); +}); diff --git a/packages/docusaurus-utils-common/src/applyTrailingSlash.ts b/packages/docusaurus-utils-common/src/applyTrailingSlash.ts index 7e80992d203d..172857c43e3b 100644 --- a/packages/docusaurus-utils-common/src/applyTrailingSlash.ts +++ b/packages/docusaurus-utils-common/src/applyTrailingSlash.ts @@ -28,9 +28,6 @@ export default function applyTrailingSlash( return path; } - function removeTrailingSlash(str: string): string { - return str.endsWith('/') ? str.slice(0, -1) : str; - } function handleTrailingSlash(str: string, trailing: boolean): string { return trailing ? addTrailingSlash(str) : removeTrailingSlash(str); } @@ -55,3 +52,27 @@ export default function applyTrailingSlash( return path.replace(pathname, newPathname); } + +/** Adds a given string prefix to `str`. */ +export function addPrefix(str: string, prefix: string): string { + return str.startsWith(prefix) ? str : `${prefix}${str}`; +} + +/** Removes a given string suffix from `str`. */ +export function removeSuffix(str: string, suffix: string): string { + if (suffix === '') { + // str.slice(0, 0) is "" + return str; + } + return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str; +} + +/** Appends a leading slash to `str`, if one doesn't exist. */ +export function addLeadingSlash(str: string): string { + return addPrefix(str, '/'); +} + +/** Removes the trailing slash from `str`. */ +export function removeTrailingSlash(str: string): string { + return removeSuffix(str, '/'); +} diff --git a/packages/docusaurus-utils-common/src/index.ts b/packages/docusaurus-utils-common/src/index.ts index a69d3eca81e3..504e834d2732 100644 --- a/packages/docusaurus-utils-common/src/index.ts +++ b/packages/docusaurus-utils-common/src/index.ts @@ -12,6 +12,10 @@ export const blogPostContainerID = '__blog-post-container'; export { default as applyTrailingSlash, addTrailingSlash, + addLeadingSlash, + addPrefix, + removeSuffix, + removeTrailingSlash, type ApplyTrailingSlashParams, } from './applyTrailingSlash'; export {getErrorCausalChain} from './errorUtils'; diff --git a/packages/docusaurus-utils-validation/package.json b/packages/docusaurus-utils-validation/package.json index f37c918de7b5..7a90a9aa891a 100644 --- a/packages/docusaurus-utils-validation/package.json +++ b/packages/docusaurus-utils-validation/package.json @@ -20,6 +20,7 @@ "dependencies": { "@docusaurus/logger": "3.0.0", "@docusaurus/utils": "3.0.0", + "@docusaurus/utils-common": "3.0.0", "joi": "^17.9.2", "js-yaml": "^4.1.0", "tslib": "^2.6.0" diff --git a/packages/docusaurus-utils-validation/src/validationSchemas.ts b/packages/docusaurus-utils-validation/src/validationSchemas.ts index 2e624458f5bc..69b4ff5c9996 100644 --- a/packages/docusaurus-utils-validation/src/validationSchemas.ts +++ b/packages/docusaurus-utils-validation/src/validationSchemas.ts @@ -5,12 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -import { - isValidPathname, - DEFAULT_PLUGIN_ID, - type Tag, - addLeadingSlash, -} from '@docusaurus/utils'; +import {isValidPathname, DEFAULT_PLUGIN_ID, type Tag} from '@docusaurus/utils'; +import {addLeadingSlash} from '@docusaurus/utils-common'; import Joi from './Joi'; import {JoiFrontMatter} from './JoiFrontMatter'; diff --git a/packages/docusaurus-utils/package.json b/packages/docusaurus-utils/package.json index 9f7adc9cbe3b..b3fa5dee583d 100644 --- a/packages/docusaurus-utils/package.json +++ b/packages/docusaurus-utils/package.json @@ -19,7 +19,6 @@ "license": "MIT", "dependencies": { "@docusaurus/logger": "3.0.0", - "@docusaurus/utils-common": "3.0.0", "@svgr/webpack": "^6.5.1", "escape-string-regexp": "^4.0.0", "file-loader": "^6.2.0", diff --git a/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts b/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts index 9ba72646964c..f45d2efbfc0c 100644 --- a/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts @@ -8,23 +8,11 @@ import {jest} from '@jest/globals'; import _ from 'lodash'; import { - removeSuffix, removePrefix, mapAsyncSequential, findAsyncSequential, } from '../jsUtils'; -describe('removeSuffix', () => { - it("is no-op when suffix doesn't exist", () => { - expect(removeSuffix('abcdef', 'ijk')).toBe('abcdef'); - expect(removeSuffix('abcdef', 'abc')).toBe('abcdef'); - expect(removeSuffix('abcdef', '')).toBe('abcdef'); - }); - it('removes suffix', () => { - expect(removeSuffix('abcdef', 'ef')).toBe('abcd'); - }); -}); - describe('removePrefix', () => { it("is no-op when prefix doesn't exist", () => { expect(removePrefix('abcdef', 'ijk')).toBe('abcdef'); diff --git a/packages/docusaurus-utils/src/__tests__/urlUtils.test.ts b/packages/docusaurus-utils/src/__tests__/urlUtils.test.ts index 8bca1ee126d1..1f63138d9780 100644 --- a/packages/docusaurus-utils/src/__tests__/urlUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/urlUtils.test.ts @@ -5,14 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -import {addTrailingSlash} from '@docusaurus/utils-common'; import { normalizeUrl, getEditUrl, fileToPath, isValidPathname, - addLeadingSlash, - removeTrailingSlash, resolvePathname, encodePath, buildSshUrl, @@ -207,33 +204,6 @@ describe('isValidPathname', () => { }); }); -describe('addTrailingSlash', () => { - it('is no-op for path with trailing slash', () => { - expect(addTrailingSlash('/abcd/')).toBe('/abcd/'); - }); - it('adds / for path without trailing slash', () => { - expect(addTrailingSlash('/abcd')).toBe('/abcd/'); - }); -}); - -describe('addLeadingSlash', () => { - it('is no-op for path with leading slash', () => { - expect(addLeadingSlash('/abc')).toBe('/abc'); - }); - it('adds / for path without leading slash', () => { - expect(addLeadingSlash('abc')).toBe('/abc'); - }); -}); - -describe('removeTrailingSlash', () => { - it('is no-op for path without trailing slash', () => { - expect(removeTrailingSlash('/abcd')).toBe('/abcd'); - }); - it('removes / for path with trailing slash', () => { - expect(removeTrailingSlash('/abcd/')).toBe('/abcd'); - }); -}); - describe('parseURLPath', () => { it('parse and resolve pathname', () => { expect(parseURLPath('')).toEqual({ diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 19129803c217..c793de4afabb 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -35,12 +35,7 @@ export { getPluginI18nPath, localizePath, } from './i18nUtils'; -export { - removeSuffix, - removePrefix, - mapAsyncSequential, - findAsyncSequential, -} from './jsUtils'; +export {removePrefix, mapAsyncSequential, findAsyncSequential} from './jsUtils'; export { normalizeUrl, getEditUrl, @@ -50,8 +45,6 @@ export { resolvePathname, parseURLPath, serializeURLPath, - addLeadingSlash, - removeTrailingSlash, hasSSHProtocol, buildHttpsUrl, buildSshUrl, diff --git a/packages/docusaurus-utils/src/jsUtils.ts b/packages/docusaurus-utils/src/jsUtils.ts index 2540d4166d69..6564c6e2f301 100644 --- a/packages/docusaurus-utils/src/jsUtils.ts +++ b/packages/docusaurus-utils/src/jsUtils.ts @@ -5,25 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -/** Adds a given string prefix to `str`. */ -export function addPrefix(str: string, prefix: string): string { - return str.startsWith(prefix) ? str : `${prefix}${str}`; -} - /** Adds a given string suffix to `str`. */ export function addSuffix(str: string, suffix: string): string { return str.endsWith(suffix) ? str : `${str}${suffix}`; } -/** Removes a given string suffix from `str`. */ -export function removeSuffix(str: string, suffix: string): string { - if (suffix === '') { - // str.slice(0, 0) is "" - return str; - } - return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str; -} - /** Removes a given string prefix from `str`. */ export function removePrefix(str: string, prefix: string): string { return str.startsWith(prefix) ? str.slice(prefix.length) : str; diff --git a/packages/docusaurus-utils/src/urlUtils.ts b/packages/docusaurus-utils/src/urlUtils.ts index dbad79892bb8..ddfc04272c36 100644 --- a/packages/docusaurus-utils/src/urlUtils.ts +++ b/packages/docusaurus-utils/src/urlUtils.ts @@ -6,7 +6,6 @@ */ import resolvePathnameUnsafe from 'resolve-pathname'; -import {addPrefix, removeSuffix} from './jsUtils'; /** * Much like `path.join`, but much better. Takes an array of URL segments, and @@ -232,16 +231,6 @@ export function resolvePathname(to: string, from?: string): string { return resolvePathnameUnsafe(to, from); } -/** Appends a leading slash to `str`, if one doesn't exist. */ -export function addLeadingSlash(str: string): string { - return addPrefix(str, '/'); -} - -/** Removes the trailing slash from `str`. */ -export function removeTrailingSlash(str: string): string { - return removeSuffix(str, '/'); -} - /** Constructs an SSH URL that can be used to push to GitHub. */ export function buildSshUrl( githubHost: string, diff --git a/packages/docusaurus/src/server/brokenLinks.ts b/packages/docusaurus/src/server/brokenLinks.ts index d03a4d771ad7..9e940f7b8c6b 100644 --- a/packages/docusaurus/src/server/brokenLinks.ts +++ b/packages/docusaurus/src/server/brokenLinks.ts @@ -10,12 +10,11 @@ import logger from '@docusaurus/logger'; import {matchRoutes as reactRouterMatchRoutes} from 'react-router-config'; import { parseURLPath, - removeTrailingSlash, serializeURLPath, flattenRoutes, type URLPath, } from '@docusaurus/utils'; -import {addTrailingSlash} from '@docusaurus/utils-common'; +import {addTrailingSlash, removeTrailingSlash} from '@docusaurus/utils-common'; import type {RouteConfig, ReportingSeverity} from '@docusaurus/types'; function matchRoutes(routeConfig: RouteConfig[], pathname: string) { diff --git a/packages/docusaurus/src/server/configValidation.ts b/packages/docusaurus/src/server/configValidation.ts index 7d3d1ac258dd..cf2d16aa59f5 100644 --- a/packages/docusaurus/src/server/configValidation.ts +++ b/packages/docusaurus/src/server/configValidation.ts @@ -9,11 +9,13 @@ import { DEFAULT_PARSE_FRONT_MATTER, DEFAULT_STATIC_DIR_NAME, DEFAULT_I18N_DIR_NAME, - addLeadingSlash, - removeTrailingSlash, } from '@docusaurus/utils'; import {Joi, printWarning} from '@docusaurus/utils-validation'; -import {addTrailingSlash} from '@docusaurus/utils-common'; +import { + addTrailingSlash, + addLeadingSlash, + removeTrailingSlash, +} from '@docusaurus/utils-common'; import type { DocusaurusConfig, I18nConfig, From 987472d9a914333fa95982d968e78ddddb41032e Mon Sep 17 00:00:00 2001 From: ozakione <29860391+OzakIOne@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:01:26 +0100 Subject: [PATCH 3/3] refactor review comments --- .../src/index.ts | 3 +- .../src/__tests__/applyTrailingSlash.test.ts | 15 ----- .../src/__tests__/stringUtils.test.ts | 55 +++++++++++++++++++ .../src/applyTrailingSlash.ts | 15 +---- packages/docusaurus-utils-common/src/index.ts | 3 +- .../src/stringUtils.ts | 30 ++++++++++ packages/docusaurus-utils/package.json | 1 + .../src/__tests__/jsUtils.test.ts | 17 +----- packages/docusaurus-utils/src/globUtils.ts | 3 +- packages/docusaurus-utils/src/index.ts | 2 +- packages/docusaurus-utils/src/jsUtils.ts | 10 ---- 11 files changed, 92 insertions(+), 62 deletions(-) create mode 100644 packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts create mode 100644 packages/docusaurus-utils-common/src/stringUtils.ts diff --git a/packages/docusaurus-plugin-client-redirects/src/index.ts b/packages/docusaurus-plugin-client-redirects/src/index.ts index 80612b23b22c..e7aa97961f37 100644 --- a/packages/docusaurus-plugin-client-redirects/src/index.ts +++ b/packages/docusaurus-plugin-client-redirects/src/index.ts @@ -5,8 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -import {removePrefix} from '@docusaurus/utils'; -import {addLeadingSlash} from '@docusaurus/utils-common'; +import {addLeadingSlash, removePrefix} from '@docusaurus/utils-common'; import collectRedirects from './collectRedirects'; import writeRedirectFiles, { toRedirectFiles, diff --git a/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts b/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts index 225d235e07e7..c86013f7fd37 100644 --- a/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts +++ b/packages/docusaurus-utils-common/src/__tests__/applyTrailingSlash.test.ts @@ -9,7 +9,6 @@ import applyTrailingSlash, { addTrailingSlash, type ApplyTrailingSlashParams, addLeadingSlash, - removeSuffix, removeTrailingSlash, } from '../applyTrailingSlash'; @@ -207,17 +206,3 @@ describe('removeTrailingSlash', () => { expect(removeTrailingSlash('/abcd/')).toBe('/abcd'); }); }); - -describe('removeSuffix', () => { - it("is no-op when suffix doesn't exist", () => { - expect(removeSuffix('abcdef', 'ijk')).toBe('abcdef'); - expect(removeSuffix('abcdef', 'abc')).toBe('abcdef'); - expect(removeSuffix('abcdef', '')).toBe('abcdef'); - }); - it('removes suffix', () => { - expect(removeSuffix('abcdef', 'ef')).toBe('abcd'); - }); - it('removes empty suffix', () => { - expect(removeSuffix('abcdef', '')).toBe('abcdef'); - }); -}); diff --git a/packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts b/packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts new file mode 100644 index 000000000000..9d01520300bf --- /dev/null +++ b/packages/docusaurus-utils-common/src/__tests__/stringUtils.test.ts @@ -0,0 +1,55 @@ +/** + * 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 {addPrefix, addSuffix, removePrefix, removeSuffix} from '../stringUtils'; + +describe('removePrefix', () => { + it("is no-op when prefix doesn't exist", () => { + expect(removePrefix('abcdef', 'ijk')).toBe('abcdef'); + expect(removePrefix('abcdef', 'def')).toBe('abcdef'); + expect(removePrefix('abcdef', '')).toBe('abcdef'); + }); + it('removes prefix', () => { + expect(removePrefix('prefix', 'pre')).toBe('fix'); + }); +}); + +describe('removeSuffix', () => { + it("is no-op when suffix doesn't exist", () => { + expect(removeSuffix('abcdef', 'ijk')).toBe('abcdef'); + expect(removeSuffix('abcdef', 'abc')).toBe('abcdef'); + expect(removeSuffix('abcdef', '')).toBe('abcdef'); + }); + it('removes suffix', () => { + expect(removeSuffix('abcdef', 'ef')).toBe('abcd'); + }); + it('removes empty suffix', () => { + expect(removeSuffix('abcdef', '')).toBe('abcdef'); + }); +}); + +describe('addPrefix', () => { + it('is no-op when prefix already exists', () => { + expect(addPrefix('abcdef', 'abc')).toBe('abcdef'); + expect(addPrefix('abc', '')).toBe('abc'); + expect(addPrefix('', '')).toBe(''); + }); + it('adds prefix', () => { + expect(addPrefix('def', 'abc')).toBe('abcdef'); + }); +}); + +describe('addSuffix', () => { + it('is no-op when suffix already exists', () => { + expect(addSuffix('abcdef', 'def')).toBe('abcdef'); + expect(addSuffix('abc', '')).toBe('abc'); + expect(addSuffix('', '')).toBe(''); + }); + it('adds suffix', () => { + expect(addSuffix('abc', 'def')).toBe('abcdef'); + }); +}); diff --git a/packages/docusaurus-utils-common/src/applyTrailingSlash.ts b/packages/docusaurus-utils-common/src/applyTrailingSlash.ts index 172857c43e3b..f83e5ce9c076 100644 --- a/packages/docusaurus-utils-common/src/applyTrailingSlash.ts +++ b/packages/docusaurus-utils-common/src/applyTrailingSlash.ts @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +import {addPrefix, removeSuffix} from './stringUtils'; import type {DocusaurusConfig} from '@docusaurus/types'; export type ApplyTrailingSlashParams = Pick< @@ -53,20 +54,6 @@ export default function applyTrailingSlash( return path.replace(pathname, newPathname); } -/** Adds a given string prefix to `str`. */ -export function addPrefix(str: string, prefix: string): string { - return str.startsWith(prefix) ? str : `${prefix}${str}`; -} - -/** Removes a given string suffix from `str`. */ -export function removeSuffix(str: string, suffix: string): string { - if (suffix === '') { - // str.slice(0, 0) is "" - return str; - } - return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str; -} - /** Appends a leading slash to `str`, if one doesn't exist. */ export function addLeadingSlash(str: string): string { return addPrefix(str, '/'); diff --git a/packages/docusaurus-utils-common/src/index.ts b/packages/docusaurus-utils-common/src/index.ts index 504e834d2732..0c4aae428147 100644 --- a/packages/docusaurus-utils-common/src/index.ts +++ b/packages/docusaurus-utils-common/src/index.ts @@ -13,9 +13,8 @@ export { default as applyTrailingSlash, addTrailingSlash, addLeadingSlash, - addPrefix, - removeSuffix, removeTrailingSlash, type ApplyTrailingSlashParams, } from './applyTrailingSlash'; +export {addPrefix, removeSuffix, addSuffix, removePrefix} from './stringUtils'; export {getErrorCausalChain} from './errorUtils'; diff --git a/packages/docusaurus-utils-common/src/stringUtils.ts b/packages/docusaurus-utils-common/src/stringUtils.ts new file mode 100644 index 000000000000..29dea13c3210 --- /dev/null +++ b/packages/docusaurus-utils-common/src/stringUtils.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. + */ + +/** Adds a given string prefix to `str`. */ +export function addPrefix(str: string, prefix: string): string { + return str.startsWith(prefix) ? str : `${prefix}${str}`; +} + +/** Removes a given string suffix from `str`. */ +export function removeSuffix(str: string, suffix: string): string { + if (suffix === '') { + // str.slice(0, 0) is "" + return str; + } + return str.endsWith(suffix) ? str.slice(0, -suffix.length) : str; +} + +/** Adds a given string suffix to `str`. */ +export function addSuffix(str: string, suffix: string): string { + return str.endsWith(suffix) ? str : `${str}${suffix}`; +} + +/** Removes a given string prefix from `str`. */ +export function removePrefix(str: string, prefix: string): string { + return str.startsWith(prefix) ? str.slice(prefix.length) : str; +} diff --git a/packages/docusaurus-utils/package.json b/packages/docusaurus-utils/package.json index b3fa5dee583d..9f7adc9cbe3b 100644 --- a/packages/docusaurus-utils/package.json +++ b/packages/docusaurus-utils/package.json @@ -19,6 +19,7 @@ "license": "MIT", "dependencies": { "@docusaurus/logger": "3.0.0", + "@docusaurus/utils-common": "3.0.0", "@svgr/webpack": "^6.5.1", "escape-string-regexp": "^4.0.0", "file-loader": "^6.2.0", diff --git a/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts b/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts index f45d2efbfc0c..fb3750e0eae7 100644 --- a/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/jsUtils.test.ts @@ -7,22 +7,7 @@ import {jest} from '@jest/globals'; import _ from 'lodash'; -import { - removePrefix, - mapAsyncSequential, - findAsyncSequential, -} from '../jsUtils'; - -describe('removePrefix', () => { - it("is no-op when prefix doesn't exist", () => { - expect(removePrefix('abcdef', 'ijk')).toBe('abcdef'); - expect(removePrefix('abcdef', 'def')).toBe('abcdef'); - expect(removePrefix('abcdef', '')).toBe('abcdef'); - }); - it('removes prefix', () => { - expect(removePrefix('prefix', 'pre')).toBe('fix'); - }); -}); +import {mapAsyncSequential, findAsyncSequential} from '../jsUtils'; describe('mapAsyncSequential', () => { function sleep(timeout: number): Promise { diff --git a/packages/docusaurus-utils/src/globUtils.ts b/packages/docusaurus-utils/src/globUtils.ts index 777eb84ccc50..72b65d75d7f9 100644 --- a/packages/docusaurus-utils/src/globUtils.ts +++ b/packages/docusaurus-utils/src/globUtils.ts @@ -9,8 +9,7 @@ import path from 'path'; import Micromatch from 'micromatch'; // Note: Micromatch is used by Globby -import {addSuffix} from './jsUtils'; - +import {addSuffix} from '@docusaurus/utils-common'; /** A re-export of the globby instance. */ export {default as Globby} from 'globby'; diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index c793de4afabb..405da5258dd4 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -35,7 +35,7 @@ export { getPluginI18nPath, localizePath, } from './i18nUtils'; -export {removePrefix, mapAsyncSequential, findAsyncSequential} from './jsUtils'; +export {mapAsyncSequential, findAsyncSequential} from './jsUtils'; export { normalizeUrl, getEditUrl, diff --git a/packages/docusaurus-utils/src/jsUtils.ts b/packages/docusaurus-utils/src/jsUtils.ts index 6564c6e2f301..240798b7ef82 100644 --- a/packages/docusaurus-utils/src/jsUtils.ts +++ b/packages/docusaurus-utils/src/jsUtils.ts @@ -5,16 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -/** Adds a given string suffix to `str`. */ -export function addSuffix(str: string, suffix: string): string { - return str.endsWith(suffix) ? str : `${str}${suffix}`; -} - -/** Removes a given string prefix from `str`. */ -export function removePrefix(str: string, prefix: string): string { - return str.startsWith(prefix) ? str.slice(prefix.length) : str; -} - /** * `Array#map` for async operations where order matters. * @param array The array to traverse.