Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/docusaurus-utils/src/__tests__/globUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
5 changes: 4 additions & 1 deletion packages/docusaurus-utils/src/globUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
),
Comment on lines +72 to +74
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hacky but this will work for everyone. Remember that a single config may be used to build sites on both Windows and Unix, so we need to cater to 2×2 = 4 cases. I would merge like this.

);
if (!rootFolder) {
throw new Error(
Expand Down
10 changes: 10 additions & 0 deletions packages/docusaurus-utils/src/jsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 === '') {
Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-utils/src/pathUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
6 changes: 3 additions & 3 deletions packages/docusaurus-utils/src/urlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`. */
Expand Down