Skip to content

Commit 40827c6

Browse files
authored
fix(utils): always match exclusion root dirs as complete folder paths (facebook#7864)
* fix(utils): always match exclusion root dirs as complete folder paths * fix * fix? * fix for real
1 parent 3a0e90e commit 40827c6

File tree

5 files changed

+35
-5
lines changed

5 files changed

+35
-5
lines changed

packages/docusaurus-utils/src/__tests__/globUtils.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,21 @@ describe('createAbsoluteFilePathMatcher', () => {
113113
`"createAbsoluteFilePathMatcher unexpected error, absoluteFilePath=/bad/path/myDoc.md was not contained in any of the root folders: /_root/docs, /root/_docs/, /__test__/website/src"`,
114114
);
115115
});
116+
117+
it('matches paths with overlapping paths', () => {
118+
const overlapMatcher = createAbsoluteFilePathMatcher(GlobExcludeDefault, [
119+
'/root/docs',
120+
'/root/versioned_docs/version-2.0.0',
121+
'/root/versioned_docs/version-2.0.0-rc.1',
122+
]);
123+
expect(
124+
overlapMatcher('/root/versioned_docs/version-2.0.0-rc.1/_partial.mdx'),
125+
).toBe(true);
126+
expect(
127+
overlapMatcher('/root/versioned_docs/version-2.0.0/_partial.mdx'),
128+
).toBe(true);
129+
expect(
130+
overlapMatcher('/root/versioned_docs/version-2.0.0/no-partial.mdx'),
131+
).toBe(false);
132+
});
116133
});

packages/docusaurus-utils/src/globUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import path from 'path';
1111
import Micromatch from 'micromatch'; // Note: Micromatch is used by Globby
12+
import {addSuffix} from './jsUtils';
1213

1314
/** A re-export of the globby instance. */
1415
export {default as Globby} from 'globby';
@@ -68,7 +69,9 @@ export function createAbsoluteFilePathMatcher(
6869

6970
function getRelativeFilePath(absoluteFilePath: string) {
7071
const rootFolder = rootFolders.find((folderPath) =>
71-
absoluteFilePath.startsWith(folderPath),
72+
[addSuffix(folderPath, '/'), addSuffix(folderPath, '\\')].some((p) =>
73+
absoluteFilePath.startsWith(p),
74+
),
7275
);
7376
if (!rootFolder) {
7477
throw new Error(

packages/docusaurus-utils/src/jsUtils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
/** Adds a given string prefix to `str`. */
9+
export function addPrefix(str: string, prefix: string): string {
10+
return str.startsWith(prefix) ? str : `${prefix}${str}`;
11+
}
12+
13+
/** Adds a given string suffix to `str`. */
14+
export function addSuffix(str: string, suffix: string): string {
15+
return str.endsWith(suffix) ? str : `${str}${suffix}`;
16+
}
17+
818
/** Removes a given string suffix from `str`. */
919
export function removeSuffix(str: string, suffix: string): string {
1020
if (suffix === '') {

packages/docusaurus-utils/src/pathUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,5 @@ export function addTrailingPathSeparator(str: string): string {
119119
return str.endsWith(path.sep)
120120
? str
121121
: // If this is Windows, we need to change the forward slash to backward
122-
`${str.replace(/\/$/, '')}${path.sep}`;
122+
`${str.replace(/[\\/]$/, '')}${path.sep}`;
123123
}

packages/docusaurus-utils/src/urlUtils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import resolvePathnameUnsafe from 'resolve-pathname';
9-
import {removeSuffix} from './jsUtils';
9+
import {addPrefix, addSuffix, removeSuffix} from './jsUtils';
1010

1111
/**
1212
* 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 {
175175
}
176176
/** Appends a leading slash to `str`, if one doesn't exist. */
177177
export function addLeadingSlash(str: string): string {
178-
return str.startsWith('/') ? str : `/${str}`;
178+
return addPrefix(str, '/');
179179
}
180180

181181
// TODO deduplicate: also present in @docusaurus/utils-common
182182
/** Appends a trailing slash to `str`, if one doesn't exist. */
183183
export function addTrailingSlash(str: string): string {
184-
return str.endsWith('/') ? str : `${str}/`;
184+
return addSuffix(str, '/');
185185
}
186186

187187
/** Removes the trailing slash from `str`. */

0 commit comments

Comments
 (0)