Skip to content

Commit 471bb2c

Browse files
authored
Merge pull request #20 from storybookjs/jeppe/support-summary
Support summary extraction
2 parents d40d7ec + 3700111 commit 471bb2c

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

src/analyze.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('analyze', () => {
5050
"metaTags": undefined,
5151
"name": undefined,
5252
"of": undefined,
53+
"summary": undefined,
5354
"title": "foobar",
5455
}
5556
`);
@@ -80,6 +81,7 @@ describe('analyze', () => {
8081
"metaTags": undefined,
8182
"name": "foobar",
8283
"of": undefined,
84+
"summary": undefined,
8385
"title": undefined,
8486
}
8587
`);
@@ -114,6 +116,7 @@ describe('analyze', () => {
114116
"metaTags": undefined,
115117
"name": undefined,
116118
"of": "./Button.stories",
119+
"summary": undefined,
117120
"title": undefined,
118121
}
119122
`);
@@ -154,6 +157,7 @@ describe('analyze', () => {
154157
"metaTags": undefined,
155158
"name": undefined,
156159
"of": "./Button.stories",
160+
"summary": undefined,
157161
"title": undefined,
158162
}
159163
`);
@@ -182,6 +186,7 @@ describe('analyze', () => {
182186
"metaTags": undefined,
183187
"name": "Story One",
184188
"of": "../src/A.stories",
189+
"summary": undefined,
185190
"title": undefined,
186191
}
187192
`);
@@ -199,6 +204,33 @@ describe('analyze', () => {
199204
});
200205
});
201206

207+
describe('summary', () => {
208+
it('string literal summary', async () => {
209+
const input = dedent`
210+
<Meta summary="This is a summary." />
211+
`;
212+
await expect(analyze(input)).resolves.toMatchInlineSnapshot(`
213+
{
214+
"imports": [],
215+
"isTemplate": false,
216+
"metaTags": undefined,
217+
"name": undefined,
218+
"of": undefined,
219+
"summary": "This is a summary.",
220+
"title": undefined,
221+
}
222+
`);
223+
});
224+
it('template literal summary', async () => {
225+
const input = dedent`
226+
<Meta summary={\`This is a summary.\`} />
227+
`;
228+
await expect(() => analyze(input)).rejects.toThrowErrorMatchingInlineSnapshot(
229+
`[Error: Expected string literal summary, received JSXExpressionContainer]`
230+
);
231+
});
232+
});
233+
202234
describe('isTemplate', () => {
203235
it('boolean implicit', async () => {
204236
const input = dedent`
@@ -211,6 +243,7 @@ describe('analyze', () => {
211243
"metaTags": undefined,
212244
"name": undefined,
213245
"of": undefined,
246+
"summary": undefined,
214247
"title": undefined,
215248
}
216249
`);
@@ -226,6 +259,7 @@ describe('analyze', () => {
226259
"metaTags": undefined,
227260
"name": undefined,
228261
"of": undefined,
262+
"summary": undefined,
229263
"title": undefined,
230264
}
231265
`);
@@ -241,6 +275,7 @@ describe('analyze', () => {
241275
"metaTags": undefined,
242276
"name": undefined,
243277
"of": undefined,
278+
"summary": undefined,
244279
"title": undefined,
245280
}
246281
`);
@@ -285,6 +320,7 @@ describe('analyze', () => {
285320
],
286321
"name": undefined,
287322
"of": "./Button.stories",
323+
"summary": undefined,
288324
"title": undefined,
289325
}
290326
`);
@@ -327,6 +363,7 @@ describe('analyze', () => {
327363
"metaTags": undefined,
328364
"name": undefined,
329365
"of": undefined,
366+
"summary": undefined,
330367
"title": undefined,
331368
}
332369
`);
@@ -346,6 +383,7 @@ describe('analyze', () => {
346383
"metaTags": undefined,
347384
"name": undefined,
348385
"of": undefined,
386+
"summary": undefined,
349387
"title": undefined,
350388
}
351389
`);
@@ -389,6 +427,7 @@ describe('analyze', () => {
389427
"metaTags": undefined,
390428
"name": undefined,
391429
"of": "./Button.stories",
430+
"summary": undefined,
392431
"title": undefined,
393432
}
394433
`);

src/analyze.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,17 @@ const getIsTemplate = (elt: JSXOpeningElement): boolean => {
9494
};
9595

9696
const extractTitle = (root: Program, varToImport: Record<string, string>) => {
97-
const result = { title: undefined, of: undefined, name: undefined, isTemplate: false } as {
97+
const result = {
98+
title: undefined,
99+
of: undefined,
100+
name: undefined,
101+
summary: undefined,
102+
isTemplate: false,
103+
} as {
98104
title: string | undefined;
99105
of: string | undefined;
100106
name: string | undefined;
107+
summary: string | undefined;
101108
isTemplate: boolean;
102109
metaTags: string[] | undefined;
103110
};
@@ -119,6 +126,7 @@ const extractTitle = (root: Program, varToImport: Record<string, string>) => {
119126
}
120127
result.title = getAttrLiteral(openingElement, 'title');
121128
result.name = getAttrLiteral(openingElement, 'name');
129+
result.summary = getAttrLiteral(openingElement, 'summary');
122130
result.of = getOf(openingElement, varToImport);
123131
result.isTemplate = getIsTemplate(openingElement);
124132
result.metaTags = getTags(openingElement);
@@ -153,10 +161,11 @@ export const extractImports = (root: Program) => {
153161
export const plugin = (store: any) => (root: any) => {
154162
const estree = toEstree(root);
155163
const varToImport = extractImports(estree);
156-
const { title, of, name, isTemplate, metaTags } = extractTitle(estree, varToImport);
164+
const { title, of, name, summary, isTemplate, metaTags } = extractTitle(estree, varToImport);
157165
store.title = title;
158166
store.of = of;
159167
store.name = name;
168+
store.summary = summary;
160169
store.isTemplate = isTemplate;
161170
store.metaTags = metaTags;
162171
store.imports = Array.from(new Set(Object.values(varToImport)));
@@ -177,6 +186,6 @@ export const analyze = async (code: string) => {
177186
await compile(code, {
178187
rehypePlugins: [[plugin, store]],
179188
});
180-
const { title, of, name, isTemplate, metaTags, imports = [] } = store;
181-
return { title, of, name, isTemplate, metaTags, imports };
189+
const { title, of, name, summary, isTemplate, metaTags, imports = [] } = store;
190+
return { title, of, name, summary, isTemplate, metaTags, imports };
182191
};

0 commit comments

Comments
 (0)