From 4458a5b27e0dfe8e1d7179f9bbde4b6e3f0f8040 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Mon, 10 Jan 2022 23:21:40 +0800 Subject: [PATCH] Merge pull request #17185 from storybookjs/autotitle-path CSF3: Remove `path` from autoTitle browser code --- lib/store/src/autoTitle.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/store/src/autoTitle.ts b/lib/store/src/autoTitle.ts index af4b1a22fa5a..a46ba2aeed57 100644 --- a/lib/store/src/autoTitle.ts +++ b/lib/store/src/autoTitle.ts @@ -1,5 +1,4 @@ import startCase from 'lodash/startCase'; -import path from 'path'; import slash from 'slash'; // FIXME: types duplicated type from `core-common', to be @@ -28,6 +27,18 @@ const startCaseTitle = (title: string) => { return title.split('/').map(startCase).join('/'); }; +/** + * Combines path parts together, without duplicating separators (slashes). Used instead of `path.join` + * because this code runs in the browser. + * + * @param paths array of paths to join together. + * @returns joined path string, with single '/' between parts + */ +function pathJoin(paths: string[]): string { + const slashes = new RegExp('/{1,}', 'g'); + return paths.join('/').replace(slashes, '/'); +} + export const autoTitleFromSpecifier = (fileName: string, entry: NormalizedStoriesSpecifier) => { const { directory, importPathMatcher, titlePrefix = '' } = entry || {}; // On Windows, backslashes are used in paths, which can cause problems here @@ -36,7 +47,7 @@ export const autoTitleFromSpecifier = (fileName: string, entry: NormalizedStorie if (importPathMatcher.exec(normalizedFileName)) { const suffix = normalizedFileName.replace(directory, ''); - const titleAndSuffix = slash(path.join(titlePrefix, suffix)); + const titleAndSuffix = slash(pathJoin([titlePrefix, suffix])); return startCaseTitle(stripExtension(titleAndSuffix)); } return undefined;