From 40827c6c72db6eaa64d9852f19a411e30feccfae Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Mon, 1 Aug 2022 15:23:01 +0800 Subject: [PATCH] fix(utils): always match exclusion root dirs as complete folder paths (#7864) * fix(utils): always match exclusion root dirs as complete folder paths * fix * fix? * fix for real --- .../src/__tests__/globUtils.test.ts | 17 +++++++++++++++++ packages/docusaurus-utils/src/globUtils.ts | 5 ++++- packages/docusaurus-utils/src/jsUtils.ts | 10 ++++++++++ packages/docusaurus-utils/src/pathUtils.ts | 2 +- packages/docusaurus-utils/src/urlUtils.ts | 6 +++--- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus-utils/src/__tests__/globUtils.test.ts b/packages/docusaurus-utils/src/__tests__/globUtils.test.ts index c35e164920..e65837b4f5 100644 --- a/packages/docusaurus-utils/src/__tests__/globUtils.test.ts +++ b/packages/docusaurus-utils/src/__tests__/globUtils.test.ts @@ -113,4 +113,21 @@ describe('createAbsoluteFilePathMatcher', () => { `"createAbsoluteFilePathMatcher unexpected error, absoluteFilePath=/bad/path/myDoc.md was not contained in any of the root folders: /_root/docs, /root/_docs/, /__test__/website/src"`, ); }); + + it('matches paths with overlapping paths', () => { + const overlapMatcher = createAbsoluteFilePathMatcher(GlobExcludeDefault, [ + '/root/docs', + '/root/versioned_docs/version-2.0.0', + '/root/versioned_docs/version-2.0.0-rc.1', + ]); + expect( + overlapMatcher('/root/versioned_docs/version-2.0.0-rc.1/_partial.mdx'), + ).toBe(true); + expect( + overlapMatcher('/root/versioned_docs/version-2.0.0/_partial.mdx'), + ).toBe(true); + expect( + overlapMatcher('/root/versioned_docs/version-2.0.0/no-partial.mdx'), + ).toBe(false); + }); }); diff --git a/packages/docusaurus-utils/src/globUtils.ts b/packages/docusaurus-utils/src/globUtils.ts index d95fa35b54..777eb84ccc 100644 --- a/packages/docusaurus-utils/src/globUtils.ts +++ b/packages/docusaurus-utils/src/globUtils.ts @@ -9,6 +9,7 @@ import path from 'path'; import Micromatch from 'micromatch'; // Note: Micromatch is used by Globby +import {addSuffix} from './jsUtils'; /** A re-export of the globby instance. */ export {default as Globby} from 'globby'; @@ -68,7 +69,9 @@ export function createAbsoluteFilePathMatcher( function getRelativeFilePath(absoluteFilePath: string) { const rootFolder = rootFolders.find((folderPath) => - absoluteFilePath.startsWith(folderPath), + [addSuffix(folderPath, '/'), addSuffix(folderPath, '\\')].some((p) => + absoluteFilePath.startsWith(p), + ), ); if (!rootFolder) { throw new Error( diff --git a/packages/docusaurus-utils/src/jsUtils.ts b/packages/docusaurus-utils/src/jsUtils.ts index d063e04555..2540d4166d 100644 --- a/packages/docusaurus-utils/src/jsUtils.ts +++ b/packages/docusaurus-utils/src/jsUtils.ts @@ -5,6 +5,16 @@ * 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 === '') { diff --git a/packages/docusaurus-utils/src/pathUtils.ts b/packages/docusaurus-utils/src/pathUtils.ts index 35b8bd2656..381cfd1faa 100644 --- a/packages/docusaurus-utils/src/pathUtils.ts +++ b/packages/docusaurus-utils/src/pathUtils.ts @@ -119,5 +119,5 @@ export function addTrailingPathSeparator(str: string): string { return str.endsWith(path.sep) ? str : // If this is Windows, we need to change the forward slash to backward - `${str.replace(/\/$/, '')}${path.sep}`; + `${str.replace(/[\\/]$/, '')}${path.sep}`; } diff --git a/packages/docusaurus-utils/src/urlUtils.ts b/packages/docusaurus-utils/src/urlUtils.ts index 06694a00bd..bb901a291d 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 {removeSuffix} from './jsUtils'; +import {addPrefix, addSuffix, removeSuffix} from './jsUtils'; /** * Much like `path.join`, but much better. Takes an array of URL segments, and @@ -175,13 +175,13 @@ export function resolvePathname(to: string, from?: string): string { } /** Appends a leading slash to `str`, if one doesn't exist. */ export function addLeadingSlash(str: string): string { - return str.startsWith('/') ? str : `/${str}`; + 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 str.endsWith('/') ? str : `${str}/`; + return addSuffix(str, '/'); } /** Removes the trailing slash from `str`. */