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

Tags: Fix MDX tags handling in index #26633

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions code/addons/docs/template/stories/docs2/Tags.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Meta } from '@storybook/addon-docs';

<Meta tags={['foo', 'bar']} />

# Docs with tags

hello docs
2 changes: 1 addition & 1 deletion code/lib/core-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"@storybook/core-events": "workspace:*",
"@storybook/csf": "^0.1.2",
"@storybook/csf-tools": "workspace:*",
"@storybook/docs-mdx": "3.0.0",
"@storybook/docs-mdx": "3.0.1--canary.a2e38ab.0",
"@storybook/global": "^5.0.0",
"@storybook/manager": "workspace:*",
"@storybook/manager-api": "workspace:*",
Expand Down
41 changes: 36 additions & 5 deletions code/lib/core-server/src/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,20 @@ describe('StoryIndexGenerator', () => {
"title": "NoTitle",
"type": "docs",
},
"tags--docs": {
"id": "tags--docs",
"importPath": "./src/docs2/Tags.mdx",
"name": "docs",
"storiesImports": [],
"tags": [
"foo",
"bar",
"unattached-mdx",
"docs",
],
"title": "Tags",
"type": "docs",
},
},
"v": 4,
}
Expand Down Expand Up @@ -910,6 +924,7 @@ describe('StoryIndexGenerator', () => {
"A",
"titlePrefix/NoTitle",
"A",
"titlePrefix/Tags",
"titlePrefix/docs2/Yabbadabbadooo",
]
`);
Expand Down Expand Up @@ -1004,6 +1019,20 @@ describe('StoryIndexGenerator', () => {
"title": "NoTitle",
"type": "docs",
},
"tags--info": {
"id": "tags--info",
"importPath": "./src/docs2/Tags.mdx",
"name": "Info",
"storiesImports": [],
"tags": [
"foo",
"bar",
"unattached-mdx",
"docs",
],
"title": "Tags",
"type": "docs",
},
},
"v": 4,
}
Expand Down Expand Up @@ -1228,6 +1257,7 @@ describe('StoryIndexGenerator', () => {
"a--metaof",
"notitle--docs",
"a--second-docs",
"tags--docs",
"docs2-yabbadabbadooo--docs",
]
`);
Expand Down Expand Up @@ -1281,6 +1311,7 @@ describe('StoryIndexGenerator', () => {
"second-nested-g--story-one",
"componentreference--docs",
"notitle--docs",
"tags--docs",
"h--story-one",
"first-nested-deeply-f--story-one",
]
Expand Down Expand Up @@ -1320,7 +1351,7 @@ describe('StoryIndexGenerator', () => {
const generator = new StoryIndexGenerator([storiesSpecifier, docsSpecifier], options);
await generator.initialize();
await generator.getIndex();
expect(toId).toHaveBeenCalledTimes(5);
expect(toId).toHaveBeenCalledTimes(6);

toIdMock.mockClear();
await generator.getIndex();
Expand Down Expand Up @@ -1379,7 +1410,7 @@ describe('StoryIndexGenerator', () => {
const generator = new StoryIndexGenerator([storiesSpecifier, docsSpecifier], options);
await generator.initialize();
await generator.getIndex();
expect(toId).toHaveBeenCalledTimes(5);
expect(toId).toHaveBeenCalledTimes(6);

generator.invalidate(docsSpecifier, './src/docs2/Title.mdx', false);

Expand All @@ -1401,7 +1432,7 @@ describe('StoryIndexGenerator', () => {
const generator = new StoryIndexGenerator([storiesSpecifier, docsSpecifier], options);
await generator.initialize();
await generator.getIndex();
expect(toId).toHaveBeenCalledTimes(5);
expect(toId).toHaveBeenCalledTimes(6);

generator.invalidate(storiesSpecifier, './src/A.stories.js', false);

Expand Down Expand Up @@ -1501,7 +1532,7 @@ describe('StoryIndexGenerator', () => {
const generator = new StoryIndexGenerator([docsSpecifier, storiesSpecifier], options);
await generator.initialize();
await generator.getIndex();
expect(toId).toHaveBeenCalledTimes(5);
expect(toId).toHaveBeenCalledTimes(6);

expect(Object.keys((await generator.getIndex()).entries)).toContain('notitle--docs');

Expand All @@ -1523,7 +1554,7 @@ describe('StoryIndexGenerator', () => {
const generator = new StoryIndexGenerator([docsSpecifier, storiesSpecifier], options);
await generator.initialize();
await generator.getIndex();
expect(toId).toHaveBeenCalledTimes(5);
expect(toId).toHaveBeenCalledTimes(6);

expect(Object.keys((await generator.getIndex()).entries)).toContain('a--metaof');

Expand Down
11 changes: 2 additions & 9 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,7 @@ export class StoryIndexGenerator {

const content = await fs.readFile(absolutePath, 'utf8');

const result: {
title?: ComponentTitle;
of?: Path;
name?: StoryName;
isTemplate?: boolean;
imports?: Path[];
tags?: Tag[];
} = analyze(content);
const result = analyze(content);

// Templates are not indexed
if (result.isTemplate) return false;
Expand Down Expand Up @@ -433,7 +426,7 @@ export class StoryIndexGenerator {
storiesImports: sortedDependencies.map((dep) => dep.entries[0].importPath),
type: 'docs',
// FIXME: update this to use the index entry's metaTags once we update this to run on `IndexInputs`
tags: [...(result.tags || []), csfEntry ? 'attached-mdx' : 'unattached-mdx', 'docs'],
tags: [...(result.metaTags || []), csfEntry ? 'attached-mdx' : 'unattached-mdx', 'docs'],
Copy link
Member

Choose a reason for hiding this comment

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

What does the FIXME here mean?

};
return docsEntry;
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Meta } from '@storybook/addon-docs';

<Meta tags={['foo', 'bar']} />

# Docs with tags

hello docs
14 changes: 14 additions & 0 deletions code/lib/core-server/src/utils/stories-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,20 @@ describe('useStoriesJson', () => {
"title": "docs2/NoTitle",
"type": "docs",
},
"docs2-tags--docs": {
"id": "docs2-tags--docs",
"importPath": "./src/docs2/Tags.mdx",
"name": "docs",
"storiesImports": [],
"tags": [
"foo",
"bar",
"unattached-mdx",
"docs",
],
"title": "docs2/Tags",
"type": "docs",
},
"docs2-yabbadabbadooo--docs": {
"id": "docs2-yabbadabbadooo--docs",
"importPath": "./src/docs2/Title.mdx",
Expand Down
10 changes: 5 additions & 5 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5886,7 +5886,7 @@ __metadata:
"@storybook/core-events": "workspace:*"
"@storybook/csf": "npm:^0.1.2"
"@storybook/csf-tools": "workspace:*"
"@storybook/docs-mdx": "npm:3.0.0"
"@storybook/docs-mdx": "npm:3.0.1--canary.a2e38ab.0"
"@storybook/global": "npm:^5.0.0"
"@storybook/manager": "workspace:*"
"@storybook/manager-api": "workspace:*"
Expand Down Expand Up @@ -5994,10 +5994,10 @@ __metadata:
languageName: node
linkType: hard

"@storybook/docs-mdx@npm:3.0.0":
version: 3.0.0
resolution: "@storybook/docs-mdx@npm:3.0.0"
checksum: 10c0/4f4242fc05b57e8dc239204c71fd0d1481c9abbf20d12dd0f3dace74f77a7ff7cbe0bd07d7d785873b45747be64cad273423d3dc0cf89b52e9f117592a4b054f
"@storybook/docs-mdx@npm:3.0.1--canary.a2e38ab.0":
version: 3.0.1--canary.a2e38ab.0
resolution: "@storybook/docs-mdx@npm:3.0.1--canary.a2e38ab.0"
checksum: 10c0/31ca2b9279bc5694be9e713967bc6fd76461968d8eaf76be3969b4b399bea877ef7d993690b4c74997f6f35053fcfc3337c2f9082f6e0d1db26625bdfb1f483a
languageName: node
linkType: hard

Expand Down
Loading