Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@whitespace/storybook-addon-html

whitespace-se216.8kAGPL-3.0-or-later7.0.0TypeScript support: included

A Storybook addon that extracts and displays compiled syntax-highlighted HTML

storybook-addon, html, code, popular, storybook-addons

readme

Storybook Addon HTML

This addon for Storybook adds a tab that displays the compiled HTML for each story.

Animated preview

Requirements

Version 6 of this addon requires Storybook 8 and Prettier 3. If you are still using Storybook 7, you can use version 5.

As of version 7 of this addon, the react-syntax-highlighter dependency is no longer required. The addon will use the default syntax highlighter of Storybook, which also formats the code with Prettier.

Getting Started

Install the addon and its dependencies.

With NPM:

npm i --save-dev @whitespace/storybook-addon-html

With Yarn:

yarn add -D @whitespace/storybook-addon-html

With PNPM:

pnpm add -D @whitespace/storybook-addon-html

Register addon

// .storybook/main.js

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

Usage

You can override the wrapper element selector used to grab the component HTML.

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

Some frameworks put comments inside the HTML. If you want to remove these you can use the removeComments parameter. Set it to true to remove all comments or set it to a regular expression that matches the content of the comments you want to remove.

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

You can also use the removeEmptyComments parameter to remove only empty comments like <!----> and <!-- -->.

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

You can override the showLineNumbers and wrapLines settings for the syntax highlighter by using the highlighter parameter:

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

Another way of hiding unwanted code is to define the transform option. It allows you to perform any change to the output code, e.g. removing attributes injected by frameworks.

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

You can disable the HTML panel by setting the disable parameter to true. This will hide and disable the HTML addon in your stories.

html: {
  disable: true, // default: false
}

changelog

6.1.1 (2024-05-11)

Bug Fixes

  • update syntax-highlighter language option and correct type reference (#126) (a653292)

6.1.0 (2024-05-07)

Features

  • add paramKey property to panel object. (#120) (1506cad)

6.0.5 (2024-04-11)

Bug Fixes

  • Remove unused preset file (2a8299d)

6.0.4 (2024-04-02)

6.0.3 (2024-04-02)

Bug Fixes

  • Replace all instances of "my addon" (693c60e)

6.0.2 (2024-03-25)

Bug Fixes

  • Remove reference to preset (f7997e8)

6.0.1 (2024-03-25)

6.0.0 (2024-03-25)

⚠ BREAKING CHANGES

  • Rewrite in Typescript with support for Storybook 8 and Prettier 3 based on latest addon kit

Code Refactoring

  • Rewrite in Typescript with support for Storybook 8 and Prettier 3 based on latest addon kit (4963646)

5.1.4 (2023-03-13)

Bug Fixes

  • Incompatibility with Storybook 7.0 (4751ec7)

5.1.3 (2023-03-10)

5.1.2 (2023-03-10)

Bug Fixes

  • Replace removed hljs theme (d7204aa)

5.1.1 (2022-12-15)

Bug Fixes

5.1.0 (2022-12-09)

Features

  • Add transform option for custom manipulation (e7a35b8)

Bug Fixes

  • Match multiline comments (c23952b)

Reverts

  • Revert "ci: Remove autoit/conventional-commits" (74628b2)

5.0.3 (2022-11-11)

5.0.2 (2022-11-11)

Features

5.0.1 (2022-11-11)

⚠ BREAKING CHANGES

  • Complete rewrite based on the official addon kit structure

Code Refactoring

  • Complete rewrite based on the official addon kit structure (836d4b9)

5.0.0 (2021-03-26)

4.2.0 (2021-01-07)

Features

  • allow empty comments to be removed (5cf46c0)
  • allow overriding showLineNumbers and wrapLines for the highlighter (121ffb6), closes #21

4.1.0 (2021-01-07)

4.0.2 (2020-12-14)

4.0.1 (2020-12-11)

Bug Fixes

  • show error message when trying to import framework-specific module (1ef9e1b), closes #31

4.0.0 (2020-12-10)

⚠ BREAKING CHANGES

  • add preset and support for att frameworks

Features

  • add preset and support for att frameworks (51a33c8)

Bug Fixes

  • assign peer deps (ae0dabc)
  • examples/react: simplify Whitespace logo (4884b04)
  • re-add support for options via parameters (ed19b93)

3.0.0 (2020-12-09)

2.0.1 (2020-09-09)

2.0.0 (2020-08-21)

⚠ BREAKING CHANGES

  • deps: Minimum supported Storybook version is now 6.X.X

Bug Fixes

  • ensure code is shown on first render (0f033b8)
  • pin “Copy” button to bottom of panel (ed0c7d4)

Miscellaneous Chores

  • deps: upgrade to support Storybook 6 (2b049c9)

1.2.2 (2020-05-09)

1.2.0 (2020-01-07)