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

Syntax highlighting for C# #7847

Closed
cgatian opened this issue Aug 23, 2019 · 10 comments
Closed

Syntax highlighting for C# #7847

cgatian opened this issue Aug 23, 2019 · 10 comments

Comments

@cgatian
Copy link
Contributor

cgatian commented Aug 23, 2019

Describe the bug
Code highlighting should work with C# in MDX stories

To Reproduce

  1. Create a new story
  2. Add code snippet using markdown code block syntax or with SyntaxHighlighter

Expected behavior
Code highlighted

Screenshots
image

Code snippets

<SyntaxHighlighter language="C#" copyable={true}>
{`
 // A Hello World! program in C#.
        using System;
        namespace HelloWorld
        {
          class Hello
          {
            static void Main()
            {
              Console.WriteLine("Hello World!");
              // Keep the console window open in debug mode.
              Console.WriteLine("Press any key to exit.");
              Console.ReadKey();
            }
          }
        }
`}
</SyntaxHighlighter>

System:
Environment Info:

System:
OS: Windows 10
CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
Binaries:
Node: 10.16.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.16.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 42.17134.1.0
npmPackages:
@storybook/addon-a11y: ^5.2.0-beta.38 => 5.2.0-beta.38
@storybook/addon-actions: ^5.2.0-beta.38 => 5.2.0-beta.38
@storybook/addon-docs: ^5.2.0-beta.38 => 5.2.0-beta.38
@storybook/addon-knobs: ^5.2.0-beta.38 => 5.2.0-beta.38
@storybook/addon-links: ^5.2.0-beta.38 => 5.2.0-beta.38
@storybook/addon-notes: ^5.2.0-beta.38 => 5.2.0-beta.38
@storybook/addon-viewport: ^5.2.0-beta.38 => 5.2.0-beta.38
@storybook/addons: ^5.2.0-beta.38 => 5.2.0-beta.38
@storybook/angular: ^5.2.0-beta.38 => 5.2.0-beta.38

@shilman
Copy link
Member

shilman commented Aug 24, 2019

We're using react-syntax-highlighter internally.

Can you try adding the folllowing to .storybook/config.js:

import { registerLanguage } from 'react-syntax-highlighter/prism-light';
import csharp from 'react-syntax-highlighter/languages/prism/csharp';

registerLanguage('C#', csharp);

I don't have a good sense for the tradeoff of including this out of the box vs. making it a user configuration like the above.

@cgatian
Copy link
Contributor Author

cgatian commented Aug 24, 2019

Thank you for the reply. I will try your suggestion.

Sorry if this request was misguided, I assumed when I found a C# story for SyntaxHighlighter in the source this should just work.

@shilman
Copy link
Member

shilman commented Aug 24, 2019

Hahaha, I just checked and the C# story is called ... "unsupported"!!

Right now we include jsx, bash, css in addition to react-syntax-highlighter's defaults (whatever those are?). I'm guessing the reason we don't include more is about tree-shaking (@ndelangen confirm?) and that it's probably enough to document how to add your own.

@ndelangen
Copy link
Member

@cgatian I added that story to showcase what happens if a language is given, but it's not installed.

@epiknut
Copy link

epiknut commented Apr 30, 2021

We're using react-syntax-highlighter internally.

Can you try adding the folllowing to .storybook/config.js:

import { registerLanguage } from 'react-syntax-highlighter/prism-light';
import csharp from 'react-syntax-highlighter/languages/prism/csharp';

registerLanguage('C#', csharp);

Is there a different approach for including csharp in SyntaxHighlighter these days? I'm having trouble getting something like this to work in the current version (6.2.9) of storybook.

@shilman
Copy link
Member

shilman commented Apr 30, 2021

@epiknut We switched over to ESM in 6.2 which enables tree shaking, which removes hundreds of unused languages from the bundle & reduces its size/improves performance dramatically. i think to get custom highlighting back we'll probably need to switch to a different highlighting library

@epiknut
Copy link

epiknut commented May 3, 2021

@shilman I see. Good to know, thanks for the reply :)

@ndelangen
Copy link
Member

@epiknut @shilman this is how users can register extra languages:

ReactSyntaxHighlighter.registerLanguage('jsextra', jsExtras);
ReactSyntaxHighlighter.registerLanguage('jsx', jsx);
ReactSyntaxHighlighter.registerLanguage('json', json);
ReactSyntaxHighlighter.registerLanguage('yml', yml);
ReactSyntaxHighlighter.registerLanguage('md', md);
ReactSyntaxHighlighter.registerLanguage('bash', bash);
ReactSyntaxHighlighter.registerLanguage('css', css);
ReactSyntaxHighlighter.registerLanguage('html', html);
ReactSyntaxHighlighter.registerLanguage('tsx', tsx);
ReactSyntaxHighlighter.registerLanguage('typescript', typescript);
ReactSyntaxHighlighter.registerLanguage('graphql', graphql);

@i-am-the-slime
Copy link

@ndelangen I can't make this work (in v7 alpha), did it break?

@ndelangen
Copy link
Member

Yeah i think this indeed breaks in 7.0.

We'll have to come up with a better solution for this.

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

5 participants