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

Core: Fix storySort order with whitespace in story paths #15038

Merged
merged 2 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions addons/docs/src/blocks/Title.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { DocsContext, DocsContextProps } from './DocsContext';
interface TitleProps {
children?: JSX.Element | string;
}
export const extractTitle = ({ kind, parameters }: DocsContextProps) => {
const groups = kind.split('/');

const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/;

export const extractTitle = ({ kind }: DocsContextProps) => {
const groups = kind.trim().split(STORY_KIND_PATH_SEPARATOR);
return (groups && groups[groups.length - 1]) || kind;
};

Expand Down
4 changes: 3 additions & 1 deletion lib/api/src/lib/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ export const denormalizeStoryParameters = ({
}));
};

const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/;

export const transformStoriesRawToStoriesHash = (
input: StoriesRaw,
{ provider }: { provider: Provider }
Expand All @@ -168,7 +170,7 @@ export const transformStoriesRawToStoriesHash = (
warnChangedDefaultHierarchySeparators();
}

const groups = kind.split('/').map((part) => part.trim());
const groups = kind.trim().split(STORY_KIND_PATH_SEPARATOR);
const root = (!setShowRoots || showRoots) && groups.length > 1 ? [groups.shift()] : [];

const rootAndGroups = [...root, ...groups].reduce((list, name, index) => {
Expand Down
16 changes: 8 additions & 8 deletions lib/client-api/src/storySort.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ describe('preview.storySort', () => {
á: ['', { kind: 'á' }],
A: ['', { kind: 'A' }],
b: ['', { kind: 'b' }],
a_a: ['', { kind: 'a/a' }],
a_b: ['', { kind: 'a/b' }],
a_c: ['', { kind: 'a/c' }],
b_a_a: ['', { kind: 'b/a/a' }],
b_b: ['', { kind: 'b/b' }],
a_a: ['', { kind: 'a / a' }],
a_b: ['', { kind: 'a / b' }],
a_c: ['', { kind: 'a / c' }],
b_a_a: ['', { kind: 'b / a / a' }],
b_b: ['', { kind: 'b / b' }],
c: ['', { kind: 'c' }],
locale1: ['', { kind: 'Б' }],
locale2: ['', { kind: 'Г' }],
c__a: ['', { kind: 'c', name: 'a' }],
c_b__a: ['', { kind: 'c/b', name: 'a' }],
c_b__b: ['', { kind: 'c/b', name: 'b' }],
c_b__c: ['', { kind: 'c/b', name: 'c' }],
c_b__a: ['', { kind: 'c / b', name: 'a' }],
c_b__b: ['', { kind: 'c / b', name: 'b' }],
c_b__c: ['', { kind: 'c / b', name: 'c' }],
c__c: ['', { kind: 'c', name: 'c' }],
};

Expand Down
11 changes: 9 additions & 2 deletions lib/client-api/src/storySort.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { StorySortObjectParameter, StorySortComparator } from '@storybook/addons';

const STORY_KIND_PATH_SEPARATOR = /\s*\/\s*/;

export const storySort = (options: StorySortObjectParameter = {}): StorySortComparator => (
a: any,
b: any
Expand All @@ -16,8 +18,13 @@ export const storySort = (options: StorySortObjectParameter = {}): StorySortComp
let order = options.order || [];

// Examine each part of the story kind in turn.
const storyKindA = [...a[1].kind.split('/'), ...(options.includeNames ? a[1].name : [])];
const storyKindB = [...b[1].kind.split('/'), ...(options.includeNames ? b[1].name : [])];
const storyKindA = a[1].kind.trim().split(STORY_KIND_PATH_SEPARATOR);
const storyKindB = b[1].kind.trim().split(STORY_KIND_PATH_SEPARATOR);
if (options.includeNames) {
storyKindA.push(a[1].name);
storyKindB.push(b[1].name);
}

let depth = 0;
while (storyKindA[depth] || storyKindB[depth]) {
// Stories with a shorter depth should go first.
Expand Down