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

在 Github 上檢視

Storybook README 擴充套件

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

所有先前的 API 在 ^5.0.0 及以上版本應能正常運作。但 Vue 使用者將需要更改其匯入路徑,因為 Vue 命令已移至其自己的資料夾。


Storybook README addon

此擴充套件與以下項目相容

線上展示

功能

  • 自動產生 props 表格(僅適用於 React)
  • 100% 支援 Markdown
  • 程式碼高亮顯示
  • 可以將文件新增至擴充套件面板或 Story 周圍
  • 接受多個 README(適用於高階元件 - 新增元件和原始元件的 README)
  • 外觀與 Github 的 README 相似
  • 支援 Vue 元件的 <docs/> 標籤 (example-vue/components/MyButton/MyButton.vue)。

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

如果安裝了Storybook Info 擴充套件,將使用 *.addWithInfo* 方法新增 Story。

安裝

npm install --save-dev storybook-readme

yarn add --dev storybook-readme

React Storybook 的 Webpack 設定

無需執行任何操作 :)

但是,如果您將 TypeScript 與 React 一起使用,則需要在 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>

使用它來定義 Story 的文件

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 />);

可以覆寫 Story 的文件

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,
    },
  });

完整的選項列表

將套用至一系列 Story。

.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>
    },
  })

全域設定

將套用至所有 Story。注意:全域設定僅套用至內容文件(Story 周圍的文件)。

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 --> Story 的預留位置
  • <!-- SOURCE --> Story 原始碼的預留位置
  • <!-- STORY_SOURCE --> Story 原始碼的預留位置
  • <!-- PROPS --> props 表格的預留位置。props 剖析存在一些問題。說明 issue#137
  • <!-- STORY HIDE START --><!-- STORY HIDE END --> 標籤括住的內容不會顯示在 Story 中
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

Emoji

在冒號之間使用簡碼將 emoji 插入文件中。例如

這是火箭 :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

在 Story 周圍呈現 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,沒有 Story。

import { doc } from 'storybook-readme';

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