Storybook 擴充功能,用於顯示元件的 README(適用於 React 和 Vue)

在 Github 上檢視

Storybook README 擴充功能

注意:此 README 僅適用於 ^5.0.0 版本。對於較舊版本,請參閱README_V4.md

所有先前的 API 在 ^5.0.0 及以上版本中都應該能正常運作。但是 Vue 使用者需要更改他們的導入路徑,因為 Vue 命令已被移動到它們自己的資料夾。


Storybook README addon

此擴充功能與以下項目相容:

線上示範

功能

  • 自動產生屬性表格(僅限 React)
  • 100% 支援 Markdown
  • 程式碼高亮
  • 可以將文件新增至擴充功能面板或故事周圍
  • 接受多個 README(適用於 高階元件 - 新增元件和原始元件的 README)
  • 看起來像 Github 的 README
  • 支援 Vue 元件的 <docs/> 標籤(example-vue/components/MyButton/MyButton.vue)。

此外,它非常有用,因為大多數專案和元件都已具有 README.md 檔案。現在可以輕鬆地將它們新增到您的 Storybook 中。

如果已安裝 Storybook Info 擴充功能,則故事將以 .addWithInfo 方法新增。

安裝

npm install --save-dev storybook-readme

yarn add --dev storybook-readme

React Storybook 的 Webpack 設定

無需執行任何操作 :)

但是,如果您正在使用帶有 React 的 Typescript,您需要在您的 webpack 設定中新增 markdown-loader

{
  test: /\.md$/,
  use: [
    {
      loader: 'markdown-loader',
    }
  ]
}

Vue Storybook 的 Webpack 設定

僅適用於使用 <docs> 標籤進行文件化的 單一檔案元件

module.exports = storybookBaseConfig => {
  storybookBaseConfig.module.rules.push({
    resourceQuery: /blockType=docs/,
    use: ['storybook-readme/vue/docs-loader', 'html-loader', 'markdown-loader'],
  });
};

在 vue 模組內定義 <docs> 標籤

<docs>
Docs inside vue module 
</docs>

<template>
  <button class="button">
    <slot></slot>
  </button>
</template>

使用它在故事中定義文件

import MyButton from '../components/MyButton/MyButton.vue';

storiesOf('Vue <docs>', module).addParameters({
  readme: {
    content: MyButton.__docs,
  },
});

設定

.storybook/addons.js 中註冊擴充功能

import 'storybook-readme/register';

注意:可以設定擴充功能面板的標題

import registerWithPanelTitle from 'storybook-readme/registerWithPanelTitle';

registerWithPanelTitle('Docs');

.storybook/config.js 中新增裝飾器

import { addDecorator, configure } from '@storybook/react';
import { addReadme } from 'storybook-readme';

// for Vue storybook
import { addReadme } from 'storybook-readme/vue'; // <---- vue subpackage

// for HTML storybook
import { addReadme } from 'storybook-readme/html'; // <---- html subpackage

addDecorator(addReadme);

function loadStories() {
  require('../stories/index.js');
}

configure(loadStories, module);

注意:對於 HTML Storybook,僅提供側邊欄文件。

用法

希望它非常簡單。

import React from 'react';
import { storiesOf } from '@storybook/react';

import Button from '../components/Button';
import ButtonReadme from '../components/Button/README.md';

storiesOf('Buttons', module)
  .addDecorator(withKnobs)
  .addParameters({
    readme: {
      // Show readme before story
      content: ButtonReadme,
      // Show readme at the addons panel
      sidebar: ButtonReadme,
    },
  })
  .add('Button', () => <Button />);

可以覆寫故事的文件

import React from 'react';
import { storiesOf } from '@storybook/react';

import Button from '../components/Button';
import ButtonReadme from '../components/Button/README.md';

storiesOf('Buttons', module)
  .addDecorator(withKnobs)
  .addParameters({
    readme: {
      content: ButtonReadme,
      sidebar: ButtonReadme,
    },
  })
  .add('Button', () => <Button />)
  .add('Button', () => <Button />)
  .add('Button', () => <Button />, {
    readme: {
      // override docs
      content: CustomButtonReadme,
      sidebar: CustomButtonReadme,
    },
  });

完整選項清單

將套用於一系列故事。

.addParameters({
    readme: {
      /**
       * Accepts string (markdown) or array of strings
       * string | Array<string>
       */
      content: Readme,

      /**
       * Accepts string (markdown) or array of strings
       * string | Array<string>
       */
      sidebar: Readme,

      /**
       * Enable / disable code highlighting for sidebar. true by default
       */
      highlightSidebar: true,

      /**
       * Enable / disable code highlighting for content. true by default
       */
      highlightContent: true,

      /**
       * Override theme values
       *
       * All theme values https://github.com/tuchk4/storybook-readme/blob/master/packages/storybook-readme/src/styles/githubMarkdownCss.js#L436

       */
      theme: {},

      /**
       * Prism code theme
       * Full list of theme https://github.com/PrismJS/prism-themes
       * To be used with storybook-readme, naming of the code theme should be used in one of these styles. https://github.com/tuchk4/storybook-readme/tree/master/packages/storybook-readme/src/styles/prismCodeThemes
       */
      codeTheme: 'github',

      /**
       * You can include prop tables locally here. See `propTables` example
       * for more information
       */
      includePropTables: [YourImportedReactComponent],

      /**
       * Wrapper for story. Usually used to set some styles
       * NOTE: will be applied only for content docs (docs around the story)
       *
       * React: React.ReactNode
       * Vue: Vue component
       */
      StoryPreview: ({ children}) => <div>{children}</div>

      /**
       * Wrapper for header docs. Usually used to set some styles
       * NOTE: will be applied only for content docs (docs around the story)
       *
       * React: React.ReactNode
       * Vue: Vue component
       */
      HeaderPreview: ({ children}) => <div>{children}</div>

      /**
       * Wrapper for footer docs. Usually used to set some styles
       * NOTE: will be applied only for content docs (docs around the story)
       *
       * React: React.ReactNode
       * Vue: Vue component
       */
      FooterPreview: ({ children}) => <div>{children}</div>

      /**
       * Wrapper for content and sidebar docs. Usually used to set some styles
       * NOTE: will be applied only for content docs (docs around the story)
       *
       * React: React.ReactNode
       * Vue: Vue component
       */
      DocPreview: ({ children}) => <div>{children}</div>
    },
  })

全域設定

將套用於所有故事。注意:全域設定僅適用於內容文件(故事周圍的文件)。

import { addParameters } from '@storybook/react'; // or @storybook/vue for vuejs
import { configureReadme } from 'storybook-readme';

configureReadme({
  /**
   * Wrapper for story. Usually used to set some styles
   * React: React.ReactNode
   * Vue: Vue component
   */
  StoryPreview: ({ children }) => <div>{children}</div>,

  /**
   * Wrapper for content and sidebar docs. Usually used to set some styles
   * React: React.ReactNode
   * Vue: Vue component
   */
  DocPreview: ({ children }) => (
    <div style={{ background: '#000' }}> {children}</div>
  ),

  /**
   * Wrapper for header docs. Usually used to set some styles
   * React: React.ReactNode
   * Vue: Vue component
   */
  HeaderPreview: ({ children }) => (
    <div style={{ background: 'red' }}>{children}</div>
  ),

  /**
   * Wrapper for footer docs. Usually used to set some styles
   * React: React.ReactNode
   * Vue: Vue component
   */
  FooterPreview: ({ children }) => <div>{children}</div>,

  /**
   * Header docs in markdown format
   */
  header: '',

  /**
   * Footer docs in markdown format
   */
  footer: '',
});

addParameters({
  readme: {
    // You can set a code theme globally.
    codeTheme: 'github',

    // You can exclude prop tables globally here. See `propTables` example
    // for more information
    excludePropTables: [YourImportedReactComponent],
  },
});

Readme 佔位符

  • 故事的 <!-- STORY --> 佔位符
  • 故事來源的 <!-- SOURCE --> 佔位符
  • 故事來源的 <!-- STORY_SOURCE --> 佔位符
  • 屬性表格的 <!-- PROPS --> 佔位符。屬性解析存在一些問題。請參閱 issue#137 了解詳情
  • <!-- STORY HIDE START --><!-- STORY HIDE END --> 標籤括住的內容將不會顯示在故事中
Button variants could be imported separately.

\`\`\`js import { OutlinedButton, ContainedButton, TextButton } from 'Button'; \`\`\`

<!-- PROPS -->

Example:

<!-- STORY -->
<!-- SOURCE -->

<!-- STORY HIDE START -->

content here won't be shown in stories

<!-- STORY HIDE END -->

Some docs after story

表情符號

使用冒號之間的短代碼將表情符號插入文件中。例如

這是火箭:rocket

這是火箭:rocket

所有短代碼的清單可在 EmojipediaGist/rxaviers 中找到

  • :rocket
  • :grinning
  • :monkey

歡迎提出新功能建議或報告錯誤 :)

來自 v4 的 API。

先前 API 的完整文件請參閱 README_V4.md

TL;DR

接受 Markdown 格式的 README 或 README 陣列。當您開發高階元件並希望與原始元件文件一起新增多個 README 時,多個 README 非常有用。

withReadme

在擴充功能面板中渲染 README。

import { withReadme } from 'storybook-readme';
import withReadme from 'storybook-readme/with-readme';

// as HigherOrder Component
storiesOf('Button', module).add(
  'Default',
  withReadme(ButtonReadme, () => <Button />),
);
// as Decorator
storiesOf('Button', module)
  .addDecorator(withReadme(ButtonReadme))
  .add('Default', () => <Button />);

withDocs

在故事周圍渲染 README。

import { withDocs } from 'storybook-readme';
import withDocs from 'storybook-readme/with-docs';

// as HigherOrder Component
storiesOf('Button', module).add(
  'Default',
  withDocs(ButtonReadme, () => <Button />),
);
// as Decorator
storiesOf('Button', module)
  .addDecorator(withDocs(ButtonReadme))
  .add('Default', () => <Button />);

doc

在主框架中渲染 README,而不顯示故事。

import { doc } from 'storybook-readme';

storiesOf('Button', module).add('Default', () => doc(ButtonReadme));