uiwjs / react-codemirror

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

Code highlighting not working #658

Closed AniruddhaTupe closed 1 month ago

AniruddhaTupe commented 1 month 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 1 month ago

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

jaywcjlove commented 1 month ago
<CodeMirror
  value={code}
  options={{
+    theme: "monokai",
    mode: 'python',
  }}
/>
AniruddhaTupe commented 1 month 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 1 month 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()]}