mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-09 00:19:13 +08:00
37 lines
1.0 KiB
JavaScript
37 lines
1.0 KiB
JavaScript
import addons, { makeDecorator } from '@storybook/addons';
|
|
import marked from 'marked';
|
|
|
|
function renderMarkdown(text, options) {
|
|
marked.setOptions({ ...marked.defaults, options });
|
|
return marked(text);
|
|
}
|
|
|
|
export const withNotes = makeDecorator({
|
|
name: 'withNotes',
|
|
parameterName: 'notes',
|
|
skipIfNoParametersOrOptions: true,
|
|
allowDeprecatedUsage: true,
|
|
wrapper: (getStory, context, { options, parameters }) => {
|
|
const channel = addons.getChannel();
|
|
|
|
const storyOptions = parameters || options;
|
|
|
|
const { text, markdown, markdownOptions } =
|
|
typeof storyOptions === 'string' ? { text: storyOptions } : storyOptions;
|
|
|
|
if (!text && !markdown) {
|
|
throw new Error('You must set of one of `text` or `markdown` on the `notes` parameter');
|
|
}
|
|
|
|
channel.emit('storybook/notes/add_notes', text || renderMarkdown(markdown, markdownOptions));
|
|
|
|
return getStory(context);
|
|
},
|
|
});
|
|
|
|
export const withMarkdownNotes = (text, options) =>
|
|
withNotes({
|
|
markdown: text,
|
|
markdownOptions: options,
|
|
});
|