Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSF3: Remove path from autoTitle browser code #17185

Merged
merged 2 commits into from Jan 10, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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