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: Support parameters.__id and metaId in indexers #23656

Merged
merged 5 commits into from
Jul 30, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1164,14 +1164,15 @@ describe('StoryIndexGenerator with deprecated indexer API', () => {
const csf = loadCsf(code, { ...options, fileName }).parse();

// eslint-disable-next-line no-underscore-dangle
return Object.entries(csf._stories).map(([key, story]) => ({
key,
id: story.id,
return Object.entries(csf._stories).map(([exportName, story]) => ({
type: 'story',
importPath: fileName,
exportName,
name: story.name,
title: csf.meta.title,
importPath: fileName,
type: 'story',
metaId: csf.meta.id,
tags: story.tags ?? csf.meta.tags,
__id: story.id,
}));
},
},
Expand Down
22 changes: 12 additions & 10 deletions code/lib/core-server/src/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ const storiesMdxIndexer: Indexer = {
const csf = loadCsf(code, { ...opts, fileName }).parse();

// eslint-disable-next-line no-underscore-dangle
return Object.entries(csf._stories).map(([key, story]) => ({
key,
id: story.id,
return Object.entries(csf._stories).map(([exportName, story]) => ({
type: 'story',
importPath: fileName,
exportName,
name: story.name,
title: csf.meta.title,
importPath: fileName,
type: 'story',
metaId: csf.meta.id,
tags: story.tags ?? csf.meta.tags,
__id: story.id,
}));
},
};
Expand All @@ -61,14 +62,15 @@ const csfIndexer: Indexer = {
const csf = loadCsf(code, { ...options, fileName }).parse();

// eslint-disable-next-line no-underscore-dangle
return Object.entries(csf._stories).map(([key, story]) => ({
key,
id: story.id,
return Object.entries(csf._stories).map(([exportName, story]) => ({
type: 'story',
importPath: fileName,
exportName,
name: story.name,
title: csf.meta.title,
importPath: fileName,
type: 'story',
metaId: csf.meta.id,
tags: story.tags ?? csf.meta.tags,
__id: story.id,
}));
},
};
Expand Down
5 changes: 3 additions & 2 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,10 @@ export class StoryIndexGenerator {

const entries: ((StoryIndexEntry | DocsCacheEntry) & { tags: Tag[] })[] = indexInputs.map(
(input) => {
const name = input.name ?? storyNameFromExport(input.key);
const name = input.name ?? storyNameFromExport(input.exportName);
const title = input.title ?? defaultMakeTitle();
const id = input.id ?? toId(title, name);
// eslint-disable-next-line no-underscore-dangle
const id = input.__id ?? toId(input.metaId ?? title, storyNameFromExport(input.exportName));
const tags = (input.tags || []).concat('story');

return {
Expand Down
105 changes: 85 additions & 20 deletions code/lib/core-server/src/utils/__tests__/index-extraction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,27 @@ describe('story extraction', () => {
{
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
// properties identical to the auto-generated ones, eg. 'StoryOne' -> 'Story One'
{
key: 'StoryOne',
id: 'a--story-one',
type: 'story',
importPath: fileName,
exportName: 'StoryOne',
name: 'Story One',
title: 'A',
metaId: 'a',
tags: ['story-tag-from-indexer'],
importPath: fileName,
__id: 'a--story-one',
},
// properties different from the auto-generated ones, eg. 'StoryOne' -> 'Another Story Name'
{
type: 'story',
importPath: fileName,
exportName: 'StoryOne',
name: 'Another Story Name',
title: 'Custom Title',
metaId: 'custom-id',
tags: ['story-tag-from-indexer'],
__id: 'some-fully-custom-id',
},
],
},
Expand All @@ -65,6 +78,17 @@ describe('story extraction', () => {
"title": "A",
"type": "story",
},
Object {
"id": "some-fully-custom-id",
"importPath": "./src/A.stories.js",
"name": "Another Story Name",
"tags": Array [
"story-tag-from-indexer",
"story",
],
"title": "Custom Title",
"type": "story",
},
],
"type": "stories",
}
Expand All @@ -83,7 +107,7 @@ describe('story extraction', () => {
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
{
key: 'StoryOne',
exportName: 'StoryOne',
importPath: fileName,
type: 'story',
},
Expand Down Expand Up @@ -125,8 +149,8 @@ describe('story extraction', () => {
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
{
key: 'StoryOne',
id: 'a--story-one',
exportName: 'StoryOne',
__id: 'a--story-one',
name: 'Story One',
tags: ['story-tag-from-indexer'],
importPath: fileName,
Expand Down Expand Up @@ -171,8 +195,8 @@ describe('story extraction', () => {
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
{
key: 'StoryOne',
id: 'a--story-one',
exportName: 'StoryOne',
__id: 'a--story-one',
title: 'A',
tags: ['story-tag-from-indexer'],
importPath: fileName,
Expand Down Expand Up @@ -205,7 +229,7 @@ describe('story extraction', () => {
`);
});

it('auto-generates id from name and title inputs', async () => {
it('auto-generates id', async () => {
const relativePath = './src/A.stories.js';
const absolutePath = path.join(options.workingDir, relativePath);
const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(relativePath, options);
Expand All @@ -216,14 +240,33 @@ describe('story extraction', () => {
{
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
// exportName + title -> id
{
key: 'StoryOne',
exportName: 'StoryOne',
name: 'Story One',
title: 'A',
tags: ['story-tag-from-indexer'],
importPath: fileName,
type: 'story',
},
// exportName + custom title (ignoring custom name) -> id
{
exportName: 'StoryTwo',
name: 'Custom Name For Second Story',
title: 'Custom Title',
tags: ['story-tag-from-indexer'],
importPath: fileName,
type: 'story',
},
// exportName + custom metaId (ignoring custom title and name) -> id
{
exportName: 'StoryThree',
metaId: 'custom-meta-id',
title: 'Custom Title',
tags: ['story-tag-from-indexer'],
importPath: fileName,
type: 'story',
},
],
},
],
Expand All @@ -245,13 +288,35 @@ describe('story extraction', () => {
"title": "A",
"type": "story",
},
Object {
"id": "custom-title--story-two",
"importPath": "./src/A.stories.js",
"name": "Custom Name For Second Story",
"tags": Array [
"story-tag-from-indexer",
"story",
],
"title": "Custom Title",
"type": "story",
},
Object {
"id": "custom-meta-id--story-three",
"importPath": "./src/A.stories.js",
"name": "Story Three",
"tags": Array [
"story-tag-from-indexer",
"story",
],
"title": "Custom Title",
"type": "story",
},
],
"type": "stories",
}
`);
});

it('auto-generates id, title and name from key input', async () => {
it('auto-generates id, title and name from exportName input', async () => {
const relativePath = './src/A.stories.js';
const absolutePath = path.join(options.workingDir, relativePath);
const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(relativePath, options);
Expand All @@ -263,7 +328,7 @@ describe('story extraction', () => {
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
{
key: 'StoryOne',
exportName: 'StoryOne',
tags: ['story-tag-from-indexer'],
importPath: fileName,
type: 'story',
Expand Down Expand Up @@ -309,8 +374,8 @@ describe('docs entries from story extraction', () => {
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
{
key: 'StoryOne',
id: 'a--story-one',
exportName: 'StoryOne',
__id: 'a--story-one',
name: 'Story One',
title: 'A',
tags: ['story-tag-from-indexer'],
Expand Down Expand Up @@ -369,8 +434,8 @@ describe('docs entries from story extraction', () => {
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
{
key: 'StoryOne',
id: 'a--story-one',
exportName: 'StoryOne',
__id: 'a--story-one',
name: 'Story One',
title: 'A',
tags: [AUTODOCS_TAG, 'story-tag-from-indexer'],
Expand Down Expand Up @@ -430,8 +495,8 @@ describe('docs entries from story extraction', () => {
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
{
key: 'StoryOne',
id: 'a--story-one',
exportName: 'StoryOne',
__id: 'a--story-one',
name: 'Story One',
title: 'A',
tags: [AUTODOCS_TAG, 'story-tag-from-indexer'],
Expand Down Expand Up @@ -478,8 +543,8 @@ describe('docs entries from story extraction', () => {
test: /\.stories\.(m?js|ts)x?$/,
index: async (fileName) => [
{
key: 'StoryOne',
id: 'a--story-one',
exportName: 'StoryOne',
__id: 'a--story-one',
name: 'Story One',
title: 'A',
tags: [STORIES_MDX_TAG, 'story-tag-from-indexer'],
Expand Down
14 changes: 8 additions & 6 deletions code/lib/core-server/src/utils/stories-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ const storiesMdxIndexer: Indexer = {
const csf = loadCsf(code, { ...opts, fileName }).parse();

// eslint-disable-next-line no-underscore-dangle
return Object.entries(csf._stories).map(([key, story]) => ({
key,
id: story.id,
return Object.entries(csf._stories).map(([exportName, story]) => ({
exportName,
__id: story.id,
metaId: csf.meta.id,
name: story.name,
title: csf.meta.title,
importPath: fileName,
Expand All @@ -67,10 +68,11 @@ const csfIndexer: Indexer = {
const csf = loadCsf(code, { ...options, fileName }).parse();

// eslint-disable-next-line no-underscore-dangle
return Object.entries(csf._stories).map(([key, story]) => ({
key,
id: story.id,
return Object.entries(csf._stories).map(([exportName, story]) => ({
exportName,
__id: story.id,
name: story.name,
metaId: csf.meta.id,
title: csf.meta.title,
importPath: fileName,
type: 'story',
Expand Down
21 changes: 21 additions & 0 deletions code/lib/csf-tools/src/CsfFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,27 @@ describe('CsfFile', () => {
`);
});

it('custom parameters.__id', () => {
expect(
parse(
dedent`
export default { title: 'foo/bar', id: 'custom-meta-id' };
export const JustCustomMetaId = {};
export const CustomParemetersId = { parameters: { __id: 'custom-id' } };
`
)
).toMatchInlineSnapshot(`
meta:
title: foo/bar
id: custom-meta-id
stories:
- id: custom-meta-id--just-custom-meta-id
name: Just Custom Meta Id
- id: custom-id
name: Custom Paremeters Id
`);
});

it('typescript', () => {
expect(
parse(
Expand Down
Loading