Skip to content

Commit 65deb79

Browse files
committed
Add tags extraction
1 parent 7638e5a commit 65deb79

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

src/analyze.test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('analyze', () => {
5050
{
5151
"imports": [],
5252
"isTemplate": false,
53+
"metaTags": undefined,
5354
"name": undefined,
5455
"of": undefined,
5556
"title": "foobar",
@@ -80,6 +81,7 @@ describe('analyze', () => {
8081
{
8182
"imports": [],
8283
"isTemplate": false,
84+
"metaTags": undefined,
8385
"name": "foobar",
8486
"of": undefined,
8587
"title": undefined,
@@ -113,6 +115,7 @@ describe('analyze', () => {
113115
"./Button.stories",
114116
],
115117
"isTemplate": false,
118+
"metaTags": undefined,
116119
"name": undefined,
117120
"of": "./Button.stories",
118121
"title": undefined,
@@ -152,6 +155,7 @@ describe('analyze', () => {
152155
"./Button.stories",
153156
],
154157
"isTemplate": false,
158+
"metaTags": undefined,
155159
"name": undefined,
156160
"of": "./Button.stories",
157161
"title": undefined,
@@ -179,6 +183,7 @@ describe('analyze', () => {
179183
"../src/A.stories",
180184
],
181185
"isTemplate": false,
186+
"metaTags": undefined,
182187
"name": "Story One",
183188
"of": "../src/A.stories",
184189
"title": undefined,
@@ -207,6 +212,7 @@ describe('analyze', () => {
207212
{
208213
"imports": [],
209214
"isTemplate": true,
215+
"metaTags": undefined,
210216
"name": undefined,
211217
"of": undefined,
212218
"title": undefined,
@@ -225,6 +231,7 @@ describe('analyze', () => {
225231
{
226232
"imports": [],
227233
"isTemplate": true,
234+
"metaTags": undefined,
228235
"name": undefined,
229236
"of": undefined,
230237
"title": undefined,
@@ -240,6 +247,7 @@ describe('analyze', () => {
240247
{
241248
"imports": [],
242249
"isTemplate": false,
250+
"metaTags": undefined,
243251
"name": undefined,
244252
"of": undefined,
245253
"title": undefined,
@@ -275,6 +283,7 @@ describe('analyze', () => {
275283
{
276284
"imports": [],
277285
"isTemplate": false,
286+
"metaTags": undefined,
278287
"name": undefined,
279288
"of": undefined,
280289
"title": undefined,
@@ -293,6 +302,7 @@ describe('analyze', () => {
293302
"./Button.stories",
294303
],
295304
"isTemplate": false,
305+
"metaTags": undefined,
296306
"name": undefined,
297307
"of": undefined,
298308
"title": undefined,
@@ -337,6 +347,32 @@ describe('analyze', () => {
337347
"./Button.stories",
338348
],
339349
"isTemplate": false,
350+
"metaTags": undefined,
351+
"name": undefined,
352+
"of": "./Button.stories",
353+
"title": undefined,
354+
}
355+
`);
356+
});
357+
it('Meta tags', () => {
358+
const input = dedent`
359+
import meta, { Basic } from './Button.stories';
360+
361+
<Meta of={meta} tags={['a', 'b', 'c']} />
362+
363+
{/* whatever */}
364+
`;
365+
expect(analyze(input)).toMatchInlineSnapshot(`
366+
{
367+
"imports": [
368+
"./Button.stories",
369+
],
370+
"isTemplate": false,
371+
"metaTags": [
372+
"a",
373+
"b",
374+
"c",
375+
],
340376
"name": undefined,
341377
"of": "./Button.stories",
342378
"title": undefined,

src/analyze.ts

+26-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const extractTitle = (root: t.File, varToImport: Record<string, string>) => {
2323
of: string | undefined;
2424
name: string | undefined;
2525
isTemplate: boolean;
26+
metaTags: string[] | undefined;
2627
};
2728
let contents: t.ExpressionStatement;
2829
root.program.body.forEach((child) => {
@@ -93,6 +94,26 @@ const extractTitle = (root: t.File, varToImport: Record<string, string>) => {
9394
);
9495
}
9596
}
97+
const tagsAttr = getAttr(child.openingElement, 'tags');
98+
if (tagsAttr) {
99+
if (t.isJSXExpressionContainer(tagsAttr.value)) {
100+
const tags = tagsAttr.value.expression;
101+
if (t.isArrayExpression(tags)) {
102+
const metaTags = tags.elements.map((tag) => {
103+
if (t.isStringLiteral(tag)) {
104+
return tag.value;
105+
} else {
106+
throw new Error(`Expected string literal tag, received ${tag.type}`);
107+
}
108+
});
109+
result.metaTags = metaTags;
110+
} else {
111+
throw new Error(`Expected array tags, received ${tags.type}`);
112+
}
113+
} else {
114+
throw new Error(`Expected JSX expression tags, received ${tagsAttr.value.type}`);
115+
}
116+
}
96117
}
97118
}
98119
} else if (t.isJSXExpressionContainer(child)) {
@@ -156,11 +177,12 @@ export const plugin = (store: any) => (root: any) => {
156177
const clone = cloneDeep(estree);
157178
const babel = toBabel(clone);
158179
const varToImport = extractImports(babel);
159-
const { title, of, name, isTemplate } = extractTitle(babel, varToImport);
180+
const { title, of, name, isTemplate, metaTags } = extractTitle(babel, varToImport);
160181
store.title = title;
161182
store.of = of;
162183
store.name = name;
163184
store.isTemplate = isTemplate;
185+
store.metaTags = metaTags;
164186
store.imports = Array.from(new Set(Object.values(varToImport)));
165187

166188
return root;
@@ -172,12 +194,13 @@ export const analyze = (code: string) => {
172194
of: undefined,
173195
name: undefined,
174196
isTemplate: false,
197+
metaTags: undefined,
175198
imports: undefined,
176199
toEstree,
177200
} as any;
178201
compileSync(code, {
179202
rehypePlugins: [[plugin, store]],
180203
});
181-
const { title, of, name, isTemplate, imports = [] } = store;
182-
return { title, of, name, isTemplate, imports };
204+
const { title, of, name, isTemplate, metaTags, imports = [] } = store;
205+
return { title, of, name, isTemplate, metaTags, imports };
183206
};

0 commit comments

Comments
 (0)