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 設定

無需任何操作 :)

但如果您將 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>

使用它來定義故事的文件

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