uiwjs / react-codemirror

CodeMirror 6 component for React. @codemirror https://uiwjs.github.io/react-codemirror/
https://uiwjs.github.io/react-codemirror/
MIT License
1.69k stars 134 forks source link

Code highlighting not working #658

Closed AniruddhaTupe closed 6 months ago

AniruddhaTupe commented 6 months ago

I'm developing a code editor for Python. But I'm unable to get the syntax highlighting to work.

Following is my code -

import './App.css';
import 'codemirror/lib/codemirror.css';
import 'codemirror/addon/comment/comment';
import 'codemirror/keymap/sublime';
import 'codemirror/theme/monokai.css';
import 'codemirror/theme/dracula.css';
import 'codemirror/mode/python/python.js';
import 'codemirror/mode/jsx/jsx';
import CodeMirror from '@uiw/react-codemirror';
import { useState } from 'react';

const code = "print(\"Hello\")";

function App() {
  return (
    <div className="App">
      <CodeMirror
        value={code}
        options={{
          mode: 'python',
        }}
      />
    </div>
  );
}

export default App;

And following is the output -

image
jaywcjlove commented 6 months ago

@AniruddhaTupe Example: https://codesandbox.io/embed/react-codemirror-v3-vgr4n?fontsize=14&hidenavigation=1&theme=dark

jaywcjlove commented 6 months ago
<CodeMirror
  value={code}
  options={{
+    theme: "monokai",
    mode: 'python',
  }}
/>
AniruddhaTupe commented 6 months ago

I copied the exact same example code, but still I can't see syntax highlighting.

Following are the sequence of npm commands I executed if that helps - npx create-react-app code-autograder npm install npm install codemirror@5 npm install @uiw/react-codemirror

Also attaching the file structure -

image

And attaching the output -

image
AniruddhaTupe commented 6 months ago

Ok, it's resolved now.

Had to run npm i @codemirror/lang-python

And then import it - import { python } from '@codemirror/lang-python';

And then use it - extensions={[python()]}