Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addon-docs: MDX2 support #17515

Merged
merged 11 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<h1>Migration</h1>

- [From version 6.4.x to 6.5.0](#from-version-64x-to-650)
- [Opt-in MDX2 support](#opt-in-mdx2-support)
- [CSF3 auto-title redundant filename](#csf3-auto-title-redundant-filename)
- [From version 6.3.x to 6.4.0](#from-version-63x-to-640)
- [Automigrate](#automigrate)
Expand Down Expand Up @@ -192,6 +193,24 @@

## From version 6.4.x to 6.5.0

### Opt-in MDX2 support

SB6.5 adds experimental opt-in support for MDXv2. To install:

```sh
yarn add @storybook/mdx2-csf -D
```

Then add the `previewMdx2` feature flag to your `.storybook/main.js` config:

```js
module.exports = {
features: {
previewMdx2: true,
},
};
```

### CSF3 auto-title redundant filename

SB 6.4 introduced experimental "auto-title", in which a story's location in the sidebar (aka `title`) can be automatically inferred from its location on disk. For example, the file `atoms/Button.stories.js` might result in the title `Atoms/Button`.
Expand Down
7 changes: 2 additions & 5 deletions addons/docs/jest-transform-mdx.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
const path = require('path');
const mdx = require('@mdx-js/mdx');
const { ScriptTransformer } = require('@jest/transform');
const { dedent } = require('ts-dedent');

const { createCompiler } = require('@storybook/csf-tools/mdx');

const compilers = [createCompiler({})];
const { compileSync } = require('@storybook/mdx1-csf');

module.exports = {
process(src, filename, config, { instrument }) {
const result = dedent`
/* @jsx mdx */
import React from 'react'
import { mdx } from '@mdx-js/react'
${mdx.sync(src, { compilers, filepath: filename })}
${compileSync(src, { filepath: filename })}
`;

const extension = path.extname(filename);
Expand Down
2 changes: 1 addition & 1 deletion addons/docs/mdx-compiler-plugin.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
module.exports = require('@storybook/csf-tools/mdx').createCompiler;
module.exports = require('@storybook/mdx1-csf').createCompiler;
30 changes: 22 additions & 8 deletions addons/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
"@babel/plugin-transform-react-jsx": "^7.12.12",
"@babel/preset-env": "^7.12.11",
"@jest/transform": "^26.6.2",
"@mdx-js/loader": "^1.6.22",
"@mdx-js/mdx": "^1.6.22",
"@mdx-js/react": "^1.6.22",
"@storybook/addons": "6.5.0-alpha.41",
"@storybook/api": "6.5.0-alpha.41",
Expand All @@ -73,6 +71,7 @@
"@storybook/core-events": "6.5.0-alpha.41",
"@storybook/csf": "0.0.2--canary.87bc651.0",
"@storybook/csf-tools": "6.5.0-alpha.41",
"@storybook/mdx1-csf": "0.0.1-canary.1.0025c94.0",
"@storybook/node-logger": "6.5.0-alpha.41",
"@storybook/postinstall": "6.5.0-alpha.41",
"@storybook/preview-web": "6.5.0-alpha.41",
Expand All @@ -88,7 +87,6 @@
"fast-deep-equal": "^3.1.3",
"global": "^4.4.0",
"html-tags": "^3.1.0",
"js-string-escape": "^1.0.1",
"loader-utils": "^2.0.0",
"lodash": "^4.17.21",
"nanoid": "^3.1.23",
Expand All @@ -109,22 +107,20 @@
"@emotion/styled": "^10.0.27",
"@storybook/angular": "6.5.0-alpha.41",
"@storybook/html": "6.5.0-alpha.41",
"@storybook/mdx2-csf": "0.0.1-canary.1.357011b.0",
"@storybook/react": "6.5.0-alpha.41",
"@storybook/vue": "6.5.0-alpha.41",
"@storybook/web-components": "6.5.0-alpha.41",
"@types/cross-spawn": "^6.0.2",
"@types/doctrine": "^0.0.3",
"@types/enzyme": "^3.10.8",
"@types/estree": "^0.0.44",
"@types/jest": "^26.0.16",
"@types/loader-utils": "^2.0.0",
"@types/prop-types": "^15.7.3",
"@types/tmp": "^0.2.0",
"@types/util-deprecate": "^1.0.0",
"babel-loader": "^8.0.0",
"babel-plugin-react-docgen": "^4.2.1",
"cross-spawn": "^7.0.3",
"fs-extra": "^9.0.1",
"jest": "^26.6.3",
"jest-specific-snapshot": "^4.0.0",
"lit-element": "^3.0.2",
Expand All @@ -134,12 +130,27 @@
"styled-components": "^5.2.1",
"sveltedoc-parser": "4.1.0",
"tmp": "^0.2.1",
"tslib": "^2.1.0",
"vue": "^2.6.10",
"web-component-analyzer": "^1.1.6",
"webpack": "4",
"zone.js": "^0.11.3"
},
"peerDependencies": {
"@storybook/angular": "6.5.0-alpha.41",
"@storybook/html": "6.5.0-alpha.41",
"@storybook/mdx2-csf": "0.0.1-canary.1.357011b.0",
"@storybook/react": "6.5.0-alpha.41",
"@storybook/vue": "6.5.0-alpha.41",
"@storybook/vue3": "6.5.0-alpha.41",
"@storybook/web-components": "6.5.0-alpha.41",
"lit": "^2.0.0",
"lit-html": "^1.4.1 || ^2.0.0",
"react": "^16.8.0 || ^17.0.0",
"react-dom": "^16.8.0 || ^17.0.0",
"svelte": "^3.31.2",
"sveltedoc-parser": "^4.1.0",
"vue": "^2.6.10 || ^3.0.0",
"webpack": "*"
},
"peerDependenciesMeta": {
"@storybook/angular": {
"optional": true
Expand All @@ -159,6 +170,9 @@
"@storybook/manager-webpack5": {
"optional": true
},
"@storybook/mdx2-csf": {
"optional": true
},
"@storybook/react": {
"optional": true
},
Expand Down
27 changes: 16 additions & 11 deletions addons/docs/src/frameworks/common/preset.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import path from 'path';
import remarkSlug from 'remark-slug';
import remarkExternalLinks from 'remark-external-links';
import global from 'global';

// @ts-ignore
import { createCompiler } from '@storybook/csf-tools/mdx';
import type { BuilderConfig, Options } from '@storybook/core-common';

const { log } = console;

// for frameworks that are not working with react, we need to configure
// the jsx to transpile mdx, for now there will be a flag for that
// for more complex solutions we can find alone that we need to add '@babel/plugin-transform-react-jsx'
Expand Down Expand Up @@ -34,9 +35,9 @@ function createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }: Bab
export async function webpack(
webpackConfig: any = {},
options: Options &
BabelParams & { sourceLoaderOptions: any; transcludeMarkdown: boolean } & Parameters<
BabelParams & { sourceLoaderOptions: any; transcludeMarkdown: boolean } /* & Parameters<
typeof createCompiler
>[0]
>[0] */
) {
const { builder = 'webpack4' } = await options.presets.apply<{
builder: BuilderConfig;
Expand All @@ -62,9 +63,17 @@ export async function webpack(
} = options;

const mdxLoaderOptions = {
skipCsf: true,
remarkPlugins: [remarkSlug, remarkExternalLinks],
};

const mdxVersion = global.FEATURES?.previewMdx2 ? 'MDX2' : 'MDX1';
log(`Addon-docs: using ${mdxVersion}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Console log is intentional?


const mdxLoader = global.FEATURES?.previewMdx2
? require.resolve('@storybook/mdx2-csf/loader')
: require.resolve('@storybook/mdx1-csf/loader');

// set `sourceLoaderOptions` to `null` to disable for manual configuration
const sourceLoader = sourceLoaderOptions
? [
Expand All @@ -89,7 +98,7 @@ export async function webpack(
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: require.resolve('@mdx-js/loader'),
loader: mdxLoader,
options: mdxLoaderOptions,
},
],
Expand Down Expand Up @@ -123,11 +132,7 @@ export async function webpack(
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: require.resolve('@mdx-js/loader'),
options: {
compilers: [createCompiler(options)],
...mdxLoaderOptions,
},
loader: mdxLoader,
},
],
},
Expand All @@ -140,7 +145,7 @@ export async function webpack(
options: createBabelOptions({ babelOptions, mdxBabelOptions, configureJSX }),
},
{
loader: require.resolve('@mdx-js/loader'),
loader: mdxLoader,
options: mdxLoaderOptions,
},
],
Expand Down
1 change: 1 addition & 0 deletions examples/react-ts/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const config: StorybookConfig = {
buildStoriesJson: true,
babelModeV7: true,
warnOnLegacyHierarchySeparator: false,
previewMdx2: true,
},
framework: '@storybook/react',
};
Expand Down
4 changes: 2 additions & 2 deletions examples/react-ts/src/__snapshots__/storyshots.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3404,15 +3404,15 @@ exports[`Storyshots Demo/Account Form Verification Success 1`] = `
</section>
`;

exports[`Storyshots Docs/Button Basic 1`] = `
exports[`Storyshots Docs/ButtonMdx Basic 1`] = `
<button
type="button"
>
Click me
</button>
`;

exports[`Storyshots Docs/Button Controls 1`] = `
exports[`Storyshots Docs/ButtonMdx Controls 1`] = `
<button
type="button"
>
Expand Down
2 changes: 1 addition & 1 deletion examples/react-ts/src/addon-docs/docs-only.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import { Meta, Canvas } from '@storybook/addon-docs';

## [Link](http://https://storybook.js.org/) in heading

This file is a documentation-only MDX file, i.e. it doesn't contain any `<Story>` definitions.
This file is a documentation-only MDX {1+1} file, i.e. it doesn't contain any `<Story>` definitions.
4 changes: 2 additions & 2 deletions examples/react-ts/src/addon-docs/docs.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Meta, Story } from '@storybook/addon-docs';
import { Button } from '../button';

<Meta title="Docs/Button" component={Button} />
<Meta title="Docs/ButtonMdx" component={Button} />

# Button
# Button MDX

<Story name="Basic">
<Button label="Click me" />
Expand Down
112 changes: 56 additions & 56 deletions lib/cli/src/versions.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
// auto generated file, do not edit
export default {
"@storybook/addon-a11y": "6.5.0-alpha.41",
"@storybook/addon-actions": "6.5.0-alpha.41",
"@storybook/addon-backgrounds": "6.5.0-alpha.41",
"@storybook/addon-controls": "6.5.0-alpha.41",
"@storybook/addon-docs": "6.5.0-alpha.41",
"@storybook/addon-essentials": "6.5.0-alpha.41",
"@storybook/addon-interactions": "6.5.0-alpha.41",
"@storybook/addon-jest": "6.5.0-alpha.41",
"@storybook/addon-links": "6.5.0-alpha.41",
"@storybook/addon-measure": "6.5.0-alpha.41",
"@storybook/addon-outline": "6.5.0-alpha.41",
"@storybook/addon-storyshots": "6.5.0-alpha.41",
"@storybook/addon-storyshots-puppeteer": "6.5.0-alpha.41",
"@storybook/addon-storysource": "6.5.0-alpha.41",
"@storybook/addon-toolbars": "6.5.0-alpha.41",
"@storybook/addon-viewport": "6.5.0-alpha.41",
"@storybook/addons": "6.5.0-alpha.41",
"@storybook/angular": "6.5.0-alpha.41",
"@storybook/api": "6.5.0-alpha.41",
"@storybook/builder-webpack4": "6.5.0-alpha.41",
"@storybook/builder-webpack5": "6.5.0-alpha.41",
"@storybook/channel-postmessage": "6.5.0-alpha.41",
"@storybook/channel-websocket": "6.5.0-alpha.41",
"@storybook/channels": "6.5.0-alpha.41",
"@storybook/cli": "6.5.0-alpha.41",
"@storybook/client-api": "6.5.0-alpha.41",
"@storybook/client-logger": "6.5.0-alpha.41",
"@storybook/codemod": "6.5.0-alpha.41",
"@storybook/components": "6.5.0-alpha.41",
"@storybook/core": "6.5.0-alpha.41",
"@storybook/core-client": "6.5.0-alpha.41",
"@storybook/core-common": "6.5.0-alpha.41",
"@storybook/core-events": "6.5.0-alpha.41",
"@storybook/core-server": "6.5.0-alpha.41",
"@storybook/csf-tools": "6.5.0-alpha.41",
"@storybook/ember": "6.5.0-alpha.41",
"@storybook/html": "6.5.0-alpha.41",
"@storybook/instrumenter": "6.5.0-alpha.41",
"@storybook/manager-webpack4": "6.5.0-alpha.41",
"@storybook/manager-webpack5": "6.5.0-alpha.41",
"@storybook/node-logger": "6.5.0-alpha.41",
"@storybook/postinstall": "6.5.0-alpha.41",
"@storybook/preact": "6.5.0-alpha.41",
"@storybook/preview-web": "6.5.0-alpha.41",
"@storybook/react": "6.5.0-alpha.41",
"@storybook/router": "6.5.0-alpha.41",
"@storybook/server": "6.5.0-alpha.41",
"@storybook/source-loader": "6.5.0-alpha.41",
"@storybook/store": "6.5.0-alpha.41",
"@storybook/svelte": "6.5.0-alpha.41",
"@storybook/theming": "6.5.0-alpha.41",
"@storybook/ui": "6.5.0-alpha.41",
"@storybook/vue": "6.5.0-alpha.41",
"@storybook/vue3": "6.5.0-alpha.41",
"@storybook/web-components": "6.5.0-alpha.41"
}
'@storybook/addon-a11y': '6.5.0-alpha.41',
'@storybook/addon-actions': '6.5.0-alpha.41',
'@storybook/addon-backgrounds': '6.5.0-alpha.41',
'@storybook/addon-controls': '6.5.0-alpha.41',
'@storybook/addon-docs': '6.5.0-alpha.41',
'@storybook/addon-essentials': '6.5.0-alpha.41',
'@storybook/addon-interactions': '6.5.0-alpha.41',
'@storybook/addon-jest': '6.5.0-alpha.41',
'@storybook/addon-links': '6.5.0-alpha.41',
'@storybook/addon-measure': '6.5.0-alpha.41',
'@storybook/addon-outline': '6.5.0-alpha.41',
'@storybook/addon-storyshots': '6.5.0-alpha.41',
'@storybook/addon-storyshots-puppeteer': '6.5.0-alpha.41',
'@storybook/addon-storysource': '6.5.0-alpha.41',
'@storybook/addon-toolbars': '6.5.0-alpha.41',
'@storybook/addon-viewport': '6.5.0-alpha.41',
'@storybook/addons': '6.5.0-alpha.41',
'@storybook/angular': '6.5.0-alpha.41',
'@storybook/api': '6.5.0-alpha.41',
'@storybook/builder-webpack4': '6.5.0-alpha.41',
'@storybook/builder-webpack5': '6.5.0-alpha.41',
'@storybook/channel-postmessage': '6.5.0-alpha.41',
'@storybook/channel-websocket': '6.5.0-alpha.41',
'@storybook/channels': '6.5.0-alpha.41',
'@storybook/cli': '6.5.0-alpha.41',
'@storybook/client-api': '6.5.0-alpha.41',
'@storybook/client-logger': '6.5.0-alpha.41',
'@storybook/codemod': '6.5.0-alpha.41',
'@storybook/components': '6.5.0-alpha.41',
'@storybook/core': '6.5.0-alpha.41',
'@storybook/core-client': '6.5.0-alpha.41',
'@storybook/core-common': '6.5.0-alpha.41',
'@storybook/core-events': '6.5.0-alpha.41',
'@storybook/core-server': '6.5.0-alpha.41',
'@storybook/csf-tools': '6.5.0-alpha.41',
'@storybook/ember': '6.5.0-alpha.41',
'@storybook/html': '6.5.0-alpha.41',
'@storybook/instrumenter': '6.5.0-alpha.41',
'@storybook/manager-webpack4': '6.5.0-alpha.41',
'@storybook/manager-webpack5': '6.5.0-alpha.41',
'@storybook/node-logger': '6.5.0-alpha.41',
'@storybook/postinstall': '6.5.0-alpha.41',
'@storybook/preact': '6.5.0-alpha.41',
'@storybook/preview-web': '6.5.0-alpha.41',
'@storybook/react': '6.5.0-alpha.41',
'@storybook/router': '6.5.0-alpha.41',
'@storybook/server': '6.5.0-alpha.41',
'@storybook/source-loader': '6.5.0-alpha.41',
'@storybook/store': '6.5.0-alpha.41',
'@storybook/svelte': '6.5.0-alpha.41',
'@storybook/theming': '6.5.0-alpha.41',
'@storybook/ui': '6.5.0-alpha.41',
'@storybook/vue': '6.5.0-alpha.41',
'@storybook/vue3': '6.5.0-alpha.41',
'@storybook/web-components': '6.5.0-alpha.41',
};
5 changes: 5 additions & 0 deletions lib/core-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ export interface StorybookConfig {
* Will be removed in 7.0.
*/
warnOnLegacyHierarchySeparator?: boolean;

/**
* Preview MDX2 support, will become default in 7.0
*/
previewMdx2?: boolean;
};

/**
Expand Down
Loading