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

Index: Fix MDX to override project-level autodocs #28461

Merged
merged 2 commits into from
Jul 5, 2024
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
115 changes: 112 additions & 3 deletions code/core/src/core-server/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,43 @@ describe('StoryIndexGenerator', () => {
`);
});

it('adds the autodocs tag to the autogenerated docs entries', async () => {
it('generates an entry for every CSF file when projectTags contains autodocs', async () => {
const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./src/**/*.stories.(ts|js|mjs|jsx)',
options
);

const generator = new StoryIndexGenerator([specifier], autodocsOptions);
generator.getProjectTags = () => ['dev', 'test', 'autodocs'];
await generator.initialize();

expect(Object.keys((await generator.getIndex()).entries)).toMatchInlineSnapshot(`
[
"a--docs",
"a--story-one",
"b--docs",
"b--story-one",
"d--docs",
"d--story-one",
"h--docs",
"h--story-one",
"componentpath-extension--docs",
"componentpath-extension--story-one",
"componentpath-noextension--docs",
"componentpath-noextension--story-one",
"componentpath-package--docs",
"componentpath-package--story-one",
"first-nested-deeply-f--docs",
"first-nested-deeply-f--story-one",
"nested-button--docs",
"nested-button--story-one",
"second-nested-g--docs",
"second-nested-g--story-one",
]
`);
});

it('adds the autodocs tag to the autogenerated docs entries when docsOptions.autodocs = true', async () => {
const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./src/**/*.stories.(ts|js|mjs|jsx)',
options
Expand All @@ -626,6 +662,22 @@ describe('StoryIndexGenerator', () => {
);
});

it('adds the autodocs tag to the autogenerated docs entries when projectTags contains autodocs', async () => {
const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./src/**/*.stories.(ts|js|mjs|jsx)',
options
);

const generator = new StoryIndexGenerator([specifier], autodocsOptions);
generator.getProjectTags = () => ['dev', 'test', 'autodocs'];
await generator.initialize();

const index = await generator.getIndex();
expect(index.entries['first-nested-deeply-f--docs'].tags).toEqual(
expect.arrayContaining(['autodocs'])
);
});

it('throws an error if you attach a named MetaOf entry which clashes with a tagged autodocs entry', async () => {
const csfSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./src/B.stories.ts',
Expand Down Expand Up @@ -793,7 +845,7 @@ describe('StoryIndexGenerator', () => {
`);
});

it('allows you to override autodocs with MetaOf if it is automatic', async () => {
it('allows you to override autodocs with MetaOf when docsOptions.autodocs = true', async () => {
const csfSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./src/A.stories.js',
options
Expand Down Expand Up @@ -852,6 +904,63 @@ describe('StoryIndexGenerator', () => {
`);
});

it('allows you to override autodocs with MetaOf when projectTags contains autodocs', async () => {
const csfSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./src/A.stories.js',
options
);

const docsSpecifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./errors/A.mdx',
options
);

const generator = new StoryIndexGenerator([csfSpecifier, docsSpecifier], autodocsOptions);
generator.getProjectTags = () => ['dev', 'test', 'autodocs'];
await generator.initialize();

expect(await generator.getIndex()).toMatchInlineSnapshot(`
{
"entries": {
"a--docs": {
"id": "a--docs",
"importPath": "./errors/A.mdx",
"name": "docs",
"storiesImports": [
"./src/A.stories.js",
],
"tags": [
"dev",
"test",
"autodocs",
"component-tag",
"story-tag",
"attached-mdx",
],
"title": "A",
"type": "docs",
},
"a--story-one": {
"componentPath": undefined,
"id": "a--story-one",
"importPath": "./src/A.stories.js",
"name": "Story One",
"tags": [
"dev",
"test",
"autodocs",
"component-tag",
"story-tag",
],
"title": "A",
"type": "story",
},
},
"v": 5,
}
`);
});

it('generates a combined entry if there are two stories files for the same title', async () => {
const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(
'./duplicate/*.stories.(ts|js|mjs|jsx)',
Expand Down Expand Up @@ -1418,7 +1527,7 @@ describe('StoryIndexGenerator', () => {
type: 'story',
};
expect(() => {
generator.chooseDuplicate(mockEntry, { ...mockEntry, importPath: 'DifferentPath' });
generator.chooseDuplicate(mockEntry, { ...mockEntry, importPath: 'DifferentPath' }, []);
}).toThrowErrorMatchingInlineSnapshot(`[Error: Duplicate stories with id: StoryId]`);
});

Expand Down
13 changes: 8 additions & 5 deletions code/core/src/core-server/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ export class StoryIndexGenerator {
}
}

chooseDuplicate(firstEntry: IndexEntry, secondEntry: IndexEntry): IndexEntry {
chooseDuplicate(firstEntry: IndexEntry, secondEntry: IndexEntry, projectTags: Tag[]): IndexEntry {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjustment in src/core-server/utils/StoryIndexGenerator.test.ts:1530:21 also necessary

// NOTE: it is possible for the same entry to show up twice (if it matches >1 glob). That's OK.
if (firstEntry.importPath === secondEntry.importPath) {
return firstEntry;
Expand Down Expand Up @@ -555,13 +555,16 @@ export class StoryIndexGenerator {
}

// If you link a file to a tagged CSF file, you have probably made a mistake
if (worseEntry.tags?.includes(AUTODOCS_TAG) && this.options.docs.autodocs !== true)
if (
worseEntry.tags?.includes(AUTODOCS_TAG) &&
!(this.options.docs.autodocs === true || projectTags?.includes(AUTODOCS_TAG))
)
throw new IndexingError(
`You created a component docs page for '${worseEntry.title}', but also tagged the CSF file with '${AUTODOCS_TAG}'. This is probably a mistake.`,
[betterEntry.importPath, worseEntry.importPath]
);

// Otherwise the existing entry is created by `autodocs=true` which allowed to be overridden.
// Otherwise the existing entry is created by project-level autodocs which is allowed to be overridden.
} else {
// If both entries are templates (e.g. you have two CSF files with the same title), then
// we need to merge the entries. We'll use the first one's name and importPath,
Expand Down Expand Up @@ -617,7 +620,7 @@ export class StoryIndexGenerator {
try {
const existing = indexEntries[entry.id];
if (existing) {
indexEntries[entry.id] = this.chooseDuplicate(existing, entry);
indexEntries[entry.id] = this.chooseDuplicate(existing, entry, projectTags);
} else {
indexEntries[entry.id] = entry;
}
Expand Down Expand Up @@ -709,7 +712,7 @@ export class StoryIndexGenerator {
}

getProjectTags(previewCode?: string) {
let projectTags = [];
let projectTags = [] as Tag[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🪶 style: Explicitly typed projectTags as Tag[].

const defaultTags = ['dev', 'test'];
const extraTags = this.options.docs.autodocs === true ? [AUTODOCS_TAG] : [];
if (previewCode) {
Expand Down