Skip to content

Commit

Permalink
Merge pull request #22120 from storybookjs/tom/21798-add-attached-mdx…
Browse files Browse the repository at this point in the history
…-to-index

Core: Add new tags to distinguish docs attachment
  • Loading branch information
shilman committed Apr 18, 2023
2 parents 510a130 + e7b6a5b commit fdadeb7
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
69 changes: 69 additions & 0 deletions code/lib/core-server/src/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ describe('StoryIndexGenerator', () => {
"./src/B.stories.ts",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "B",
Expand Down Expand Up @@ -566,6 +567,7 @@ describe('StoryIndexGenerator', () => {
"./src/B.stories.ts",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "B",
Expand Down Expand Up @@ -616,6 +618,7 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand Down Expand Up @@ -727,6 +730,7 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand All @@ -740,6 +744,7 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand All @@ -762,6 +767,7 @@ describe('StoryIndexGenerator', () => {
"name": "docs",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "ComponentReference",
Expand All @@ -773,6 +779,7 @@ describe('StoryIndexGenerator', () => {
"name": "docs",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "docs2/Yabbadabbadooo",
Expand All @@ -784,6 +791,7 @@ describe('StoryIndexGenerator', () => {
"name": "docs",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "NoTitle",
Expand Down Expand Up @@ -844,6 +852,7 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand All @@ -857,6 +866,7 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand All @@ -879,6 +889,7 @@ describe('StoryIndexGenerator', () => {
"name": "Info",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "ComponentReference",
Expand All @@ -890,6 +901,7 @@ describe('StoryIndexGenerator', () => {
"name": "Info",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "docs2/Yabbadabbadooo",
Expand All @@ -901,6 +913,7 @@ describe('StoryIndexGenerator', () => {
"name": "Info",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "NoTitle",
Expand All @@ -911,6 +924,62 @@ describe('StoryIndexGenerator', () => {
}
`);
});

it('pulls the attached story file to the front of the list', async () => {
const generator = new StoryIndexGenerator(
[
normalizeStoriesEntry('./src/A.stories.js', options),
normalizeStoriesEntry('./src/B.stories.ts', options),
normalizeStoriesEntry('./complex/TwoStoryReferences.mdx', options),
],
options
);
await generator.initialize();
expect(await generator.getIndex()).toMatchInlineSnapshot(`
Object {
"entries": Object {
"a--story-one": Object {
"id": "a--story-one",
"importPath": "./src/A.stories.js",
"name": "Story One",
"tags": Array [
"story-tag",
"story",
],
"title": "A",
"type": "story",
},
"b--story-one": Object {
"id": "b--story-one",
"importPath": "./src/B.stories.ts",
"name": "Story One",
"tags": Array [
"autodocs",
"story",
],
"title": "B",
"type": "story",
},
"b--twostoryreferences": Object {
"id": "b--twostoryreferences",
"importPath": "./complex/TwoStoryReferences.mdx",
"name": "TwoStoryReferences",
"storiesImports": Array [
"./src/B.stories.ts",
"./src/A.stories.js",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "B",
"type": "docs",
},
},
"v": 4,
}
`);
});
});

describe('errors', () => {
Expand Down
10 changes: 8 additions & 2 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ export class StoryIndexGenerator {
// are invalidated.f
const dependencies = this.findDependencies(absoluteImports);

// To ensure the `<Meta of={}/>` import is always first in the list, we'll bring the dependency
// that contains it to the front of the list.
let sortedDependencies = dependencies;

// Also, if `result.of` is set, it means that we're using the `<Meta of={XStories} />` syntax,
// so find the `title` defined the file that `meta` points to.
let csfEntry: StoryIndexEntry;
Expand All @@ -342,6 +346,8 @@ export class StoryIndexGenerator {
csfEntry = first;
}
}

sortedDependencies = [dep, ...dependencies.filter((d) => d !== dep)];
});

if (!csfEntry)
Expand Down Expand Up @@ -372,9 +378,9 @@ export class StoryIndexGenerator {
title,
name,
importPath,
storiesImports: dependencies.map((dep) => dep.entries[0].importPath),
storiesImports: sortedDependencies.map((dep) => dep.entries[0].importPath),
type: 'docs',
tags: [...(result.tags || []), 'docs'],
tags: [...(result.tags || []), csfEntry ? 'attached-mdx' : 'unattached-mdx', 'docs'],
};
return docsEntry;
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{/* References AStories first, but is attached to B */}
import * as AStories from '../src/A.stories';
import * as BStories from '../src/B.stories';

<Meta of={BStories}/>

# This file references two story files

It is important that B.stories is the first listed in `storiesImports` (so we can tell what it is attached to)
10 changes: 10 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 @@ -126,6 +126,7 @@ describe('useStoriesJson', () => {
"./src/A.stories.js",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand All @@ -139,6 +140,7 @@ describe('useStoriesJson', () => {
"./src/A.stories.js",
],
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand Down Expand Up @@ -183,6 +185,7 @@ describe('useStoriesJson', () => {
"name": "docs",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "docs2/ComponentReference",
Expand All @@ -194,6 +197,7 @@ describe('useStoriesJson', () => {
"name": "docs",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "docs2/NoTitle",
Expand All @@ -205,6 +209,7 @@ describe('useStoriesJson', () => {
"name": "docs",
"storiesImports": Array [],
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "docs2/Yabbadabbadooo",
Expand Down Expand Up @@ -304,6 +309,7 @@ describe('useStoriesJson', () => {
],
"story": "MetaOf",
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand All @@ -323,6 +329,7 @@ describe('useStoriesJson', () => {
],
"story": "Second Docs",
"tags": Array [
"attached-mdx",
"docs",
],
"title": "A",
Expand Down Expand Up @@ -391,6 +398,7 @@ describe('useStoriesJson', () => {
"storiesImports": Array [],
"story": "docs",
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "docs2/ComponentReference",
Expand All @@ -408,6 +416,7 @@ describe('useStoriesJson', () => {
"storiesImports": Array [],
"story": "docs",
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "docs2/NoTitle",
Expand All @@ -425,6 +434,7 @@ describe('useStoriesJson', () => {
"storiesImports": Array [],
"story": "docs",
"tags": Array [
"unattached-mdx",
"docs",
],
"title": "docs2/Yabbadabbadooo",
Expand Down

0 comments on commit fdadeb7

Please sign in to comment.