Skip to content

Commit

Permalink
normalize paths when attempting to create autoTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
yngvebn committed Aug 6, 2021
1 parent 8c32662 commit 8b98db3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
3 changes: 3 additions & 0 deletions examples/angular-cli/.storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ module.exports = {
core: {
builder: 'webpack4',
},
features: {
previewCsfV3: true,
},
angularOptions: {
enableIvy: true,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AppComponent } from '../../../app/app.component';

export default {
component: AppComponent,
};

export const Default = {
render: (props) => ({ props }),
};
1 change: 1 addition & 0 deletions lib/core-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"lodash": "^4.17.20",
"qs": "^6.10.0",
"regenerator-runtime": "^0.13.7",
"slash": "^3.0.0",
"ts-dedent": "^2.0.0",
"unfetch": "^4.2.0",
"util-deprecate": "^1.0.2"
Expand Down
12 changes: 12 additions & 0 deletions lib/core-client/src/preview/autoTitle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ describe('autoTitle', () => {
auto('/path/to_my/file', { glob: '', specifier: { directory: '/path' } })
).toMatchInlineSnapshot(`To My/File`);
});

it('match with windows path', () => {
expect(
auto('/path/to_my/file', { glob: '', specifier: { directory: '\\path' } })
).toMatchInlineSnapshot(`To My/File`);
});
});

describe('trailing slash', () => {
Expand Down Expand Up @@ -85,5 +91,11 @@ describe('autoTitle', () => {
auto('/path/to_my/file', { glob: '', specifier: { directory: '/path/' } })
).toMatchInlineSnapshot(`To My/File`);
});

it('match with windows path', () => {
expect(
auto('/path/to_my/file', { glob: '', specifier: { directory: '\\path\\' } })
).toMatchInlineSnapshot(`To My/File`);
});
});
});
17 changes: 12 additions & 5 deletions lib/core-client/src/preview/autoTitle.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { NormalizedStoriesEntry } from '@storybook/core-common';
import global from 'global';
import path from 'path';
import startCase from 'lodash/startCase';
import type { NormalizedStoriesEntry } from '@storybook/core-common';
import path from 'path';
import slash from 'slash';

const { FEATURES = {}, STORIES = [] } = global;

Expand Down Expand Up @@ -32,9 +33,15 @@ const startCaseTitle = (title: string) => {

export const autoTitleFromEntry = (fileName: string, entry: NormalizedStoriesEntry) => {
const { directory, titlePrefix = '' } = entry.specifier || {};
if (fileName.startsWith(directory)) {
const suffix = fileName.replace(directory, '');
return startCaseTitle(stripExtension(path.join(titlePrefix, suffix)));
// On Windows, backslashes are used in paths, which can cause problems here
// slash makes sure we always handle paths with unix-style forward slash
const normalizedDirectory = directory && slash(directory);
const normalizedFileName = slash(fileName);

if (normalizedFileName.startsWith(normalizedDirectory)) {
const suffix = normalizedFileName.replace(normalizedDirectory, '');
const titleAndSuffix = slash(path.join(titlePrefix, suffix));
return startCaseTitle(stripExtension(titleAndSuffix));
}
return undefined;
};
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6897,6 +6897,7 @@ __metadata:
lodash: ^4.17.20
qs: ^6.10.0
regenerator-runtime: ^0.13.7
slash: ^3.0.0
ts-dedent: ^2.0.0
unfetch: ^4.2.0
util-deprecate: ^1.0.2
Expand Down

0 comments on commit 8b98db3

Please sign in to comment.