Skip to content

Commit

Permalink
Merge pull request #17185 from storybookjs/autotitle-path
Browse files Browse the repository at this point in the history
CSF3: Remove `path` from autoTitle browser code
  • Loading branch information
shilman committed Jan 10, 2022
2 parents 5947eef + 6e98cec commit e7aefb3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions 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
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit e7aefb3

Please sign in to comment.