Skip to content

Commit

Permalink
refactor: Adopt Storybook internal for extracting description from JS…
Browse files Browse the repository at this point in the history
…Doc comments
  • Loading branch information
xeho91 committed Jun 8, 2024
1 parent 5fefc0a commit 148c4f7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 22 deletions.
36 changes: 18 additions & 18 deletions src/compiler/transform/define-meta/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ describe(transformDefineMeta.name, () => {
});

expect(toJs(defineMetaVariableDeclaration as unknown as Program).value).toMatchInlineSnapshot(`
"const {Story, meta} = defineMeta({
title: 'Example',
component: Example,
tags: ['autodocs'],
args: {
onclick: action("onclick"),
onmouseenter: action("onmouseenter"),
onmouseleave: action("onmouseleave")
},
parameters: {
docs: {
description: {
component: "Description set explicitly in the comment above \`defineMeta\`"
}
}
}
});"
`);
"const {Story, meta} = defineMeta({
title: 'Example',
component: Example,
tags: ['autodocs'],
args: {
onclick: action("onclick"),
onmouseenter: action("onmouseenter"),
onmouseleave: action("onmouseleave")
},
parameters: {
docs: {
description: {
component: "Description set explicitly in the comment above \`defineMeta\`.\\n\\nMultiline supported. And also Markdown syntax:\\n\\n* **Bold**,\\n* _Italic_,\\n* \`Code\`."
}
}
}
});"
`);
});
});
33 changes: 31 additions & 2 deletions src/compiler/transform/define-meta/insert-description.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { logger } from '@storybook/client-logger';
import dedent from 'dedent';
import type { Comment } from 'estree';

import {
createASTObjectExpression,
Expand Down Expand Up @@ -102,7 +102,36 @@ export function insertDefineMetaJSDocCommentAsDescription(params: Params): void
getDescriptionPropertyValue(defineMetaFirstArgumentObjectExpression).properties.push(
createASTProperty('component', {
type: 'Literal',
value: dedent(leadingComments[0].value),
value: extractDescription(leadingComments),
})
);
}

/**
* Adopted from: https://github.com/storybookjs/storybook/blob/next/code/lib/csf-tools/src/enrichCsf.ts/#L148-L164
* Adjusted for this addon, because here we use AST format from `estree`, not `babel`.
*/
function extractDescription(leadingComments: Comment[]) {
const comments = leadingComments
.map((comment) => {
if (
comment.type === 'Line' ||
// is not a JSDoc format - `/**` - by default parser omits the leading `/*` and ending `*/`
!comment.value.startsWith('*')
) {
return null;
}

return (
comment.value
.split('\n')
// remove leading *'s and spaces from the beginning of each line
.map((line) => line.replace(/^(\s+)?(\*+)?(\s)?/, ''))
.join('\n')
.trim()
);
})
.filter(Boolean);

return comments.join('\n');
}
2 changes: 1 addition & 1 deletion src/compiler/transform/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe(transformStoriesCode.name, () => {
parameters: {
docs: {
description: {
component: "Description set explicitly in the comment above \`defineMeta\`"
component: "Description set explicitly in the comment above \`defineMeta\`.\\n\\nMultiline supported. And also Markdown syntax:\\n\\n* **Bold**,\\n* _Italic_,\\n* \`Code\`."
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion stories/Example.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
import Example from './Example.svelte';
// Description set explicitly in the comment above `defineMeta`
/**
* Description set explicitly in the comment above `defineMeta`.
*
* Multiline supported. And also Markdown syntax:
*
* * **Bold**,
* * _Italic_,
* * `Code`.
*/
const { Story } = defineMeta({
title: 'Example',
component: Example,
Expand Down

0 comments on commit 148c4f7

Please sign in to comment.