HTML 預覽

一個 Storybook 擴充套件,用於提取和顯示編譯過的、語法高亮的 HTML

在 Github 上檢視

Storybook 擴充套件 HTML

這個 Storybook 的擴充套件會新增一個分頁,顯示每個 story 編譯後的 HTML。它使用 highlight.js 進行語法高亮。

Animated preview

需求

此擴充套件的第 6 版需要 Storybook 8 和 Prettier 3。如果您仍然使用 Storybook 7,可以使用第 5 版。

開始使用

安裝擴充套件及其依賴項。

使用 NPM

npm i --save-dev @whitespace/storybook-addon-html prettier react-syntax-highlighter

使用 Yarn

yarn add -D @whitespace/storybook-addon-html prettier react-syntax-highlighter

註冊擴充套件

// .storybook/main.js

module.exports = {
  // ...
  addons: [
    "@whitespace/storybook-addon-html",
    // ...
  ],
};

用法

HTML 使用 Prettier 格式化。您可以透過在 html 參數中提供一個遵循 Prettier API 覆寫格式 的物件,來覆寫 Prettier 設定(parserplugins 除外)

// .storybook/preview.js

export const parameters = {
  // ...
  html: {
    prettier: {
      tabWidth: 4,
      useTabs: false,
      htmlWhitespaceSensitivity: "strict",
    },
  },
};

您可以覆寫用於抓取元件 HTML 的包裝元素選取器。

export const parameters = {
  html: {
    root: "#my-custom-wrapper", // default: #root
  },
};

某些框架會將註解放入 HTML 中。如果您想移除這些註解,可以使用 removeComments 參數。將其設定為 true 以移除所有註解,或將其設定為符合您要移除的註解內容的正規表示式。

export const parameters = {
  html: {
    removeComments: /^\s*remove me\s*$/, // default: false
  },
};

您也可以使用 removeEmptyComments 參數來僅移除空註解,例如 <!----><!-- -->

export const parameters = {
  html: {
    removeEmptyComments: true, // default: false
  },
};

您可以透過使用 highlighter 參數來覆寫語法高亮器的 showLineNumberswrapLines 設定

export const parameters = {
  html: {
    highlighter: {
      showLineNumbers: true, // default: false
      wrapLines: false, // default: true
    },
  },
};

隱藏不需要的程式碼的另一種方法是定義 transform 選項。它允許您對輸出程式碼執行任何變更,例如移除框架注入的屬性。

html: {
  transform: (code) => {
    // Remove attributes `_nghost` and `ng-reflect` injected by Angular:
    return code.replace(/(?:_nghost|ng-reflect).*?="[\S\s]*?"/g, "");
  };
}

您可以透過將 disable 參數設定為 true 來停用 HTML 面板。這將隱藏並停用您 stories 中的 HTML 擴充套件。

html: {
  disable: true, // default: false
}
作者
  • olofsandell
    olofsandell
  • ynwuay
    ynwuay
  • tsubarah
    tsubarah
  • jeanfredrik
    jeanfredrik
  • whitespace-se
    whitespace-se
  • pbrotherton
    pbrotherton
適用於
    Angular
    Ember
    HTML
    Preact
    React
    React native
    Svelte
    Vue
    Web Components
標籤