Skip to content

Commit

Permalink
fix(v2): Fix writeHeadingIds on Windows due to non-posix paths (#4444)
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber authored Mar 16, 2021
1 parent 66e621d commit d5cad5b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/docusaurus/src/commands/writeHeadingIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* LICENSE file in the root directory of this source tree.
*/

import globby from 'globby';
import fs from 'fs-extra';
import GithubSlugger from 'github-slugger';
import chalk from 'chalk';
Expand All @@ -14,6 +13,7 @@ import initPlugins from '../server/plugins/init';

import {flatten} from 'lodash';
import {parseMarkdownHeadingId} from '@docusaurus/utils';
import {safeGlobby} from '../server/utils';

export function unwrapMarkdownLinks(line: string): string {
return line.replace(/\[([^\]]+)\]\([^)]+\)/g, (match, p1) => p1);
Expand Down Expand Up @@ -107,7 +107,7 @@ async function getPathsToWatch(siteDir: string): Promise<string[]> {
}

export default async function writeHeadingIds(siteDir: string): Promise<void> {
const markdownFiles = await globby(await getPathsToWatch(siteDir), {
const markdownFiles = await safeGlobby(await getPathsToWatch(siteDir), {
expandDirectories: ['**/*.{md,mdx}'],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ import chalk from 'chalk';
import {parse, types as t, NodePath, TransformOptions} from '@babel/core';
import {flatten} from 'lodash';
import {TranslationFileContent, TranslationMessage} from '@docusaurus/types';
import globby from 'globby';
import nodePath from 'path';
import {InitPlugin} from '../plugins/init';
import {posixPath} from '@docusaurus/utils';
import {SRC_DIR_NAME} from '../../constants';
import {safeGlobby} from '../utils';

// We only support extracting source code translations from these kind of files
const TranslatableSourceCodeExtension = new Set([
Expand Down Expand Up @@ -54,13 +53,7 @@ function getPluginSourceCodeFilePaths(plugin: InitPlugin): string[] {
export async function globSourceCodeFilePaths(
dirPaths: string[],
): Promise<string[]> {
// Required for Windows support, as paths using \ should not be used by globby
// (also using the windows hard drive prefix like c: is not a good idea)
const globPaths = dirPaths.map((dirPath) =>
posixPath(nodePath.relative(process.cwd(), dirPath)),
);

const filePaths = await globby(globPaths);
const filePaths = await safeGlobby(dirPaths);
return filePaths.filter(isTranslatableSourceCodePath);
}

Expand Down
18 changes: 18 additions & 0 deletions packages/docusaurus/src/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
import {flatMap} from 'lodash';
import {RouteConfig} from '@docusaurus/types';
import globby from 'globby';
import nodePath from 'path';
import {posixPath} from '@docusaurus/utils';

// Recursively get the final routes (routes with no subroutes)
export function getAllFinalRoutes(routeConfig: RouteConfig[]): RouteConfig[] {
Expand All @@ -14,3 +17,18 @@ export function getAllFinalRoutes(routeConfig: RouteConfig[]): RouteConfig[] {
}
return flatMap(routeConfig, getFinalRoutes);
}

// Globby that fix Windows path patterns
// See https://github.com/facebook/docusaurus/pull/4222#issuecomment-795517329
export async function safeGlobby(
patterns: string[],
options?: globby.GlobbyOptions,
) {
// Required for Windows support, as paths using \ should not be used by globby
// (also using the windows hard drive prefix like c: is not a good idea)
const globPaths = patterns.map((dirPath) =>
posixPath(nodePath.relative(process.cwd(), dirPath)),
);

return globby(globPaths, options);
}

0 comments on commit d5cad5b

Please sign in to comment.