|
1 | 1 | import { teamsDarkTheme, teamsLightTheme, webDarkTheme, webLightTheme } from '@fluentui/tokens';
|
2 |
| -import webcomponentsTheme from './theme.mjs'; |
| 2 | +import * as prettier from 'prettier'; |
| 3 | +import prettierPluginHTML from 'prettier/parser-html.js'; |
3 | 4 | import { setTheme } from '../src/theme/set-theme.js';
|
| 5 | +import webcomponentsTheme from './theme.mjs'; |
4 | 6 |
|
5 | 7 | import '../src/index-rollup.js';
|
6 | 8 | import './docs-root.css';
|
7 | 9 |
|
| 10 | +const FAST_EXPRESSION_COMMENTS = /<!--((fast-\w+)\{.*\}\2)?-->/g; // Matches comments that contain FAST expressions |
| 11 | + |
8 | 12 | const themes = {
|
9 | 13 | 'web-light': webLightTheme,
|
10 | 14 | 'web-dark': webDarkTheme,
|
@@ -36,6 +40,31 @@ export const parameters = {
|
36 | 40 | },
|
37 | 41 | },
|
38 | 42 | docs: {
|
| 43 | + source: { |
| 44 | + // To get around the inability to change Prettier options in the source addon, this transform function |
| 45 | + // imports the standalone Prettier and uses it to format the source with the desired options. |
| 46 | + transform(/** @type {string} */ src, /** @type {import('@storybook/html').StoryContext} */ storyContext) { |
| 47 | + if (!src) { |
| 48 | + const fragment = storyContext.originalStoryFn(storyContext.allArgs, storyContext); |
| 49 | + if (!(fragment instanceof DocumentFragment) && !(fragment instanceof HTMLElement)) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + const div = document.createElement('div'); |
| 54 | + div.append(fragment); |
| 55 | + src = div.innerHTML; |
| 56 | + } |
| 57 | + |
| 58 | + src = src.replace(FAST_EXPRESSION_COMMENTS, ''); // remove comments |
| 59 | + src = src.replace(/=""/g, ''); // remove values for boolean attributes |
| 60 | + src = prettier.format(src, { |
| 61 | + htmlWhitespaceSensitivity: 'ignore', |
| 62 | + parser: 'html', |
| 63 | + plugins: [prettierPluginHTML], |
| 64 | + }); |
| 65 | + return src; |
| 66 | + }, |
| 67 | + }, |
39 | 68 | theme: webcomponentsTheme, // override the default Storybook theme with a custom fluent theme
|
40 | 69 | },
|
41 | 70 | };
|
0 commit comments