-
-
Notifications
You must be signed in to change notification settings - Fork 272
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2701 from modernweb-dev/feat/storybook-builder-mdx
feat(storybook-builder): support MDX and autodocs
- Loading branch information
Showing
16 changed files
with
265 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@web/storybook-builder': patch | ||
--- | ||
|
||
support MDX and autodocs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { compile } from '@storybook/mdx2-csf'; | ||
import type { Options } from '@storybook/types'; | ||
import { readFile } from 'fs-extra'; | ||
import remarkExternalLinks from 'remark-external-links'; | ||
import remarkSlug from 'remark-slug'; | ||
import type { Plugin } from 'rollup'; | ||
|
||
export function rollupPluginMdx(storybookOptions: Options): Plugin { | ||
let mdxPluginOptions: Record<string, any>; | ||
let jsxOptions: Record<string, any>; | ||
|
||
return { | ||
name: 'rollup-plugin-mdx', | ||
|
||
async buildStart() { | ||
({ mdxPluginOptions, jsxOptions } = await storybookOptions.presets.apply<Record<string, any>>( | ||
'options', | ||
{}, | ||
)); | ||
}, | ||
|
||
async resolveId(id) { | ||
if (id.endsWith('.mdx.js')) { | ||
return id; | ||
} | ||
}, | ||
|
||
async load(id) { | ||
if (!id.endsWith('.mdx.js')) return; | ||
|
||
const mdxPath = id.replace(/\.js$/, ''); | ||
const mdxCode = await readFile(mdxPath, 'utf8'); | ||
|
||
const mdxLoaderOptions = await storybookOptions.presets.apply('mdxLoaderOptions', { | ||
...mdxPluginOptions, | ||
mdxCompileOptions: { | ||
providerImportSource: '@mdx-js/react', | ||
...mdxPluginOptions?.mdxCompileOptions, | ||
remarkPlugins: [remarkSlug, remarkExternalLinks].concat( | ||
mdxPluginOptions?.mdxCompileOptions?.remarkPlugins ?? [], | ||
), | ||
}, | ||
jsxOptions, | ||
}); | ||
|
||
const jsCode = compile(mdxCode, { | ||
skipCsf: true, | ||
...mdxLoaderOptions, | ||
}); | ||
|
||
return jsCode; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.