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

Support for .md / .mdx files in storybook #29

Open
nate-summercook opened this issue Jun 26, 2024 · 6 comments
Open

Support for .md / .mdx files in storybook #29

nate-summercook opened this issue Jun 26, 2024 · 6 comments

Comments

@nate-summercook
Copy link

I am migrating my project from webpack to rsbuild and am currently in the process of trying to make storybook work with the adapted setup. Using this library, I'd have expected it to support .mdx files out of the box, as it's pretty standard for storybook.
So I started out with a simple configuration without any custom .md(x) configuration. Doesn't complain about .mdx, but about my .md import within an .mdx file. Since in webpack I have been dealing with .md files by adding a rule to treat .md as type asset/source, I thought I could simply do that here too, but to no avail. I've also used this rule successfully with rsbuild when referencing .md from within .ts files.

const storybookConfig = {
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
  addons: [
    '@storybook/addon-links',
    '@storybook/addon-essentials',
    '@storybook/addon-a11y',
    '@storybook/addon-designs',
  ],
  core: {
    disableTelemetry: true,
  },
  docs: {
    autodocs: true,
  },
  framework: 'storybook-react-rsbuild',
  rsbuildFinal: (config) => {
    return mergeRsbuildConfig(config, {
      tools: {
        rspack(config, { addRules }) {
          addRules([
            {
              test: /\.md$/,
              type: 'asset/source',
            },
          ]);
        },
      },
      plugins: [pluginReact()],
    });
  },
}

My suspicion is, that this rule is not applied within for .mdx files and therefore it still doesn't know what to do with that .md import in my .mdx file.
Did I simply miss some additinoal config, or is this not yet supported?
I also did try using the pluginMdx(), but that only made things worse (weird errors on .mdx files that weren't complaining without the plugin.

@fi3ework
Copy link
Member

storybook-rsbuild's configuration is a little complex so for now you had to write the rsbuildFinal in the following style

import { mergeRsbuildConfig } from '@rsbuild/core'
import type { StorybookConfig } from 'storybook-react-rsbuild'
import { join, dirname } from 'path'

/**
 * This function is used to resolve the absolute path of a package.
 * It is needed in projects that use Yarn PnP or are set up within a monorepo.
 */
function getAbsolutePath(value: string): any {
  return dirname(require.resolve(join(value, 'package.json')))
}

const config: StorybookConfig = {
  stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  addons: [
    getAbsolutePath('@storybook/addon-onboarding'),
    getAbsolutePath('@storybook/addon-links'),
    getAbsolutePath('@storybook/addon-essentials'),
    getAbsolutePath('@chromatic-com/storybook'),
    getAbsolutePath('@storybook/addon-interactions'),
  ],
  framework: {
    name: getAbsolutePath('storybook-react-rsbuild'),
    options: {},
  },
  docs: {
    autodocs: 'tag',
  },
  typescript: {
    reactDocgen: 'react-docgen',
    check: true,
  },
  rsbuildFinal: (config) => {
    return mergeRsbuildConfig(config, {
      tools: {
+       rspack(config, { addRules, mergeConfig }) {
+         return mergeConfig(config, {
+             module: {
+              rules: [
+               {
+                test: /\.md$/,
+                type: 'asset/source',
+             },
+              ],
+           },
          })
        },
      },
    })
  },
}

export default config

@fi3ework
Copy link
Member

With above configuration, the .md could be rendered as plain text instead of Markdown style. Is that identical with webpack?
image

image image

@nate-summercook
Copy link
Author

Well, I'm importing it plain text to render it within an .mdx file. So that should work. Can't test it though until #30 is addressed ;) Unless I'd set up a separate project just to test this.

@fi3ework
Copy link
Member

So when importing and using a .md in .mdx, the .md file could be render as Markdown content with webpack & SB8?

@nate-summercook
Copy link
Author

webpack & SB7, yes. But the import was *.md?raw but I think that results in pretty much the same.

@fi3ework
Copy link
Member

fi3ework commented Jun 28, 2024

I'll test does it works on webpack and SB8 then align with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants