Storybook HTML插件
这个Storybook插件添加了一个标签页,用于显示每个故事的已编译HTML。它使用highlight.js进行语法高亮。
入门指南
安装插件及其依赖项。
使用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配置(除了 parser
和 plugins
)。
// .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
参数来覆盖语法高亮器的 showLineNumbers
和 wrapLines
设置。
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, "");
};
}