-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathtransform-iframe-html.ts
36 lines (32 loc) · 1.67 KB
/
transform-iframe-html.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { normalizeStories } from '@storybook/core-common';
import type { CoreConfig, DocsOptions, Options } from '@storybook/types';
export type PreviewHtml = string | undefined;
export async function transformIframeHtml(html: string, options: Options) {
const { configType, features, presets } = options;
const frameworkOptions = await presets.apply<Record<string, any> | null>('frameworkOptions');
const headHtmlSnippet = await presets.apply<PreviewHtml>('previewHead');
const bodyHtmlSnippet = await presets.apply<PreviewHtml>('previewBody');
const logLevel = await presets.apply('logLevel', undefined);
const docsOptions = await presets.apply<DocsOptions>('docs');
const coreOptions = await presets.apply<CoreConfig>('core');
const stories = normalizeStories(await options.presets.apply('stories', [], options), {
configDir: options.configDir,
workingDir: process.cwd(),
}).map((specifier) => ({
...specifier,
importPathMatcher: specifier.importPathMatcher.source,
}));
return html
.replace('[CONFIG_TYPE HERE]', configType || '')
.replace('[LOGLEVEL HERE]', logLevel || '')
.replace(`'[FRAMEWORK_OPTIONS HERE]'`, JSON.stringify(frameworkOptions))
.replace(
`'[CHANNEL_OPTIONS HERE]'`,
JSON.stringify(coreOptions && coreOptions.channelOptions ? coreOptions.channelOptions : {})
)
.replace(`'[FEATURES HERE]'`, JSON.stringify(features || {}))
.replace(`'[STORIES HERE]'`, JSON.stringify(stories || {}))
.replace(`'[DOCS_OPTIONS HERE]'`, JSON.stringify(docsOptions || {}))
.replace('<!-- [HEAD HTML SNIPPET HERE] -->', headHtmlSnippet || '')
.replace('<!-- [BODY HTML SNIPPET HERE] -->', bodyHtmlSnippet || '');
}