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

DocsPage code preview indentation is wrong #8078

Closed
danielcolinjames opened this issue Sep 13, 2019 · 44 comments
Closed

DocsPage code preview indentation is wrong #8078

danielcolinjames opened this issue Sep 13, 2019 · 44 comments

Comments

@danielcolinjames
Copy link

danielcolinjames commented Sep 13, 2019

Issuehunt badges

Describe the bug
When creating stories for a DocsPage with an MDX file, the code preview underneath the story seems to have the wrong indentation, even though it's correct in the file.

To Reproduce
Steps to reproduce the behavior:

  1. Create a Component.stories.mdx file
  2. Output a <Story name="default"><Preview><Component /></Preview></Story>
  3. Go to DocsPage for that component
  4. Click "Code" button to bring up code preview
  5. See wrong indentation for code preview

Expected behavior
The indentation (and line breaks) would be identical to how it shows up in the MDX file.

Screenshots
The code:

and how it shows up in the DocsPage <Preview>:

Code snippets
If applicable, add code samples to help explain your problem.

System:
Environment Info:

System:
OS: macOS 10.14.6
CPU: (8) x64 Intel(R) Core(TM) i7-4870HQ CPU @ 2.50GHz
Binaries:
Node: 12.6.0 - /usr/local/bin/node
Yarn: 1.17.3 - /usr/local/bin/yarn
npm: 6.9.0 - /usr/local/bin/npm
Browsers:
Chrome: 76.0.3809.87
Firefox: 68.0.1
Safari: 12.1.2
npmPackages:
@storybook/addon-actions: ^5.2.0-rc.10 => 5.2.0-rc.11
@storybook/addon-docs: ^5.2.0-rc.11 => 5.2.0-rc.11
@storybook/addon-links: ^5.2.0-rc.10 => 5.2.0-rc.11
@storybook/addons: ^5.2.0-rc.10 => 5.2.0-rc.11
@storybook/react: ^5.2.0-rc.10 => 5.2.0-rc.11

Additional context
The same exact indentation abnormality (2nd through 2nd last lines are indented one tab too far left, and last line indented two tabs too far right) seems to happen for all my DocsPage code previews, so I figured it's either a configuration issue somewhere on my end, or a bug with the DocsPage code preview.

Thanks!


IssueHunt Summary

Backers (Total: $20.00)

Become a backer now!

Or submit a pull request to get the deposits!

Tips

@stale
Copy link

stale bot commented Oct 14, 2019

Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks!

@stale stale bot added the inactive label Oct 14, 2019
@stale stale bot removed the inactive label Oct 14, 2019
@shilman shilman added the todo label Oct 14, 2019
@mbbillz
Copy link

mbbillz commented Nov 19, 2019

Having the same issue, looks especially strange with render prop components... 😆
Screenshot 2019-11-19 at 11 46 01

@shilman shilman modified the milestones: 5.2.x, 5.3.x Jan 11, 2020
@shilman
Copy link
Member

shilman commented Jan 16, 2020

@atanasster can you help out here?

@atanasster
Copy link
Member

can someone please paste source code in text form to reproduce the issue instead of pics

@shilman
Copy link
Member

shilman commented Jan 17, 2020

@atanasster
Copy link
Member

Thanks

@shilman
Copy link
Member

shilman commented Jan 20, 2020

Whoopee!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.3.7 containing PR #9513 that references this issue. Upgrade today to try it out!

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Jan 20, 2020
@mrtraser
Copy link

mrtraser commented Jan 22, 2020

Indentation still wrong for *.mdx files in storybook v5.3.8

Source:
Screenshot 2020-01-22 at 17 29 38

Result:
Screenshot 2020-01-22 at 17 29 22

@shilman
Copy link
Member

shilman commented Jan 24, 2020

Boo-yah!! I just released https://github.com/storybookjs/storybook/releases/tag/v5.3.9 containing PR #9609 that references this issue. Upgrade today to try it out!

Closing this issue. Please re-open if you think there's still more to do.

@shilman shilman closed this as completed Jan 24, 2020
@tklives
Copy link

tklives commented Jun 7, 2021

Hey y'all!

I am also still very much interested in any potential fixes or workarounds for this issue. Any additional info to share on this yet? :)

Thanks!
TK

@ndelangen
Copy link
Member

We should now be lazy-loading the syntaxhighlighter.. Maybe we can restart the effort to employ prettier for formatting?

@frassinier
Copy link
Contributor

frassinier commented Jun 22, 2021

Here is a workaround for now, by editing your preview.js

import prettier from 'prettier/standalone';
import prettierBabel from 'prettier/parser-babel';

export const parameters = {
  docs: {
    transformSource: input =>
      prettier.format(input, {
        parser: 'babel',
        plugins: [prettierBabel],
      }),
  },
};

Hope it can help!

@01taylop
Copy link

@frassinier

Whilst this solution did work for me, running Storybook took a lot longer and I see this getting logged for a number of different files:

Note: The code generator has deoptimised the styling of /PATH/node_modules/prettier/standalone.js as it exceeds the max of 500KB.

It seems the issue occurs as soon as I add the prettier/standalone import.

@mpalpha
Copy link

mpalpha commented Oct 7, 2021

@01taylop
My workaround may work for you.

@wisteria-hill-technologies
Copy link

wisteria-hill-technologies commented Nov 14, 2021

Hi, I tried the workaround by @frassinier.
But, it added semicolon at the end of the code block and also it did not parse a component with a prop which passes a component, and doc breaks.

Instead, I did below using pretty library:

import pretty from 'pretty';
...
export const parameters = {
  docs: {
     transformSource: input => pretty(input)
  }
  ...
}

For your information. This worked for me.

@ndelangen
Copy link
Member

This is fixed in 7.0 beta

@MrErHu
Copy link

MrErHu commented Sep 13, 2023

Ridiculous, this problem still has not been solved in 2023!

@ndelangen
Copy link
Member

Would you be so kind to explain to us how you concluded this is in fact not yet fixed in the current latest version?

We can re-open this if you supply a reproduction repo.

@PauliCZ44
Copy link

Here is a workaround for now, by editing your preview.js

import prettier from 'prettier/standalone';
import prettierBabel from 'prettier/parser-babel';

export const parameters = {
  docs: {
    transformSource: input =>
      prettier.format(input, {
        parser: 'babel',
        plugins: [prettierBabel],
      }),
  },
};

Hope it can help!

This work like a charm in SB 7.5

@lwkchan
Copy link

lwkchan commented Nov 10, 2023

It seems like the solution which uses prettier/standalone and prettier/parser-babel above do not work if you use prettier v3 or later. This is because from v3, prettier's format function is asychrnous returning a promise (see release note). However, Storybook's transformSource is expected to be a function which returns a string.

Using 'prettier-synchronized' didn't work for me either, since it looks like it needs to be used in a node environment

@JamesIves
Copy link

It seems like the solution which uses prettier/standalone and prettier/parser-babel above do not work if you use prettier v3 or later. This is because from v3, prettier's format function is asychrnous returning a promise (see release note). However, Storybook's transformSource is expected to be a function which returns a string.

Using 'prettier-synchronized' didn't work for me either, since it looks like it needs to be used in a node environment

Also running into this. Curious if there's any workarounds.

@karlschwaier
Copy link

I had the same issue and found a workaround by installing Prettier v2 with npm aliasing.

"prettier": "3.3.2",
"prettier-v2": "npm:[email protected]",

preview.ts:

import prettier from 'prettier-v2';
import HTMLParser from 'prettier-v2/parser-html';

@caseywatts
Copy link

caseywatts commented Sep 2, 2024

I got this working! Prettier parsing of the code preview.
Thanks to a lot of clues in this thread, plus this thread

package.json

  "dependencies": {
    "prettier": "^3.3.3",
    "prettier-v2": "npm:[email protected]"
  },

preview.ts

import type { Preview } from "@storybook/web-components";
import prettier from "prettier-v2";
import HTMLParser from "prettier-v2/parser-html";

const preview: Preview = {
    docs: {
      source: {
        transform: (input) =>
          prettier.format(input, {
            parser: "html",
            plugins: [HTMLParser],
          }),
      },
    },
  },
};

export default preview;

('prettier-synchronized' didn't work for me either)

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

No branches or pull requests