Syntax highlighter not working using codemirror 2 with Next.js

1.2k views Asked by At

I saw many examples it works fine for all but I don't know why it does not highlight my code. I tried many solutions none of them work for me.

Also, I need to inject my own custom HTML file in it for the initial when user click on demo for the code and also user can change the file and run its own custom code that user want.

Here is the screenshot of the output:

enter image description here

Code that I used for the editor

import React from 'react';
import {Controlled as ControlledEditor} from 'react-codemirror2';
import {language} from "@hapi/accept";
import {codecs} from "next/dist/next-server/server/lib/squoosh/codecs";

if (typeof navigator !== 'undefined') {
    require('codemirror/mode/xml/xml');
    require('codemirror/mode/css/css');
    require('codemirror/mode/javascript/javascript');
    require('codemirror/mode/markdown/markdown');
}
    
const Editor = (props) => {
    const {
        displayName,
        value,
        onChange
    } = props
    
    const handleChange = (editor,date,value) => {
        onChange(value);
    }
    
    return (
        <div className="editor-container">
            <div className="editor_title">
                {displayName}
                <button>O/C</button>
            </div>
            <ControlledEditor
                onBeforeChange={handleChange}
                value={value}
                className={'code-mirror-wrapper'}
                options={{
                    lineWrapping:true,
                    lint:true,
                    mode:language,
                    theme:'dracula',
                    lineNumbers:true
                }}
            />
        </div>
    )
}
    
export  default  Editor;

That is the new file where I call my editor:

import React,{useState,useEffect} from 'react';
import Editor from '../../component/Editor/editor';
    
const RunCode = () => {
    const [html,setHtml] = useState();
    const [css,setCss] = useState();
    const [JS,setJS] = useState();
    const [scrDoc,setSrcDoc] = useState('');
    
    useEffect(()=>{
        const timeout = setTimeout(()=>{
            setSrcDoc(
                `<html>
                   <body>${html}</body>
                   <style>${css}</style>
                   <script>${JS}</script>
                </html>`
            )
        },250)
    
        return () => clearTimeout(timeout)
    },[html,JS,css]);
    
    return(
        <>
             <div className={'pane top-pane'}>
                 <Editor
                     language={'xml'}
                     displayName={'HTML'}
                     value={html}
                     onChange={setHtml}
                 />
                 <Editor
                     language={'CSS'}
                     displayName={'CSS'}
                     value={css}
                     onChange={setCss}
                 />
                 <Editor
                     language={'JS'}
                     displayName={'JS'}
                     value={JS}
                     onChange={setJS}
                 />
             </div>
             <div className="output">
                  <iframe
                      srcDoc={scrDoc}
                      title={'Output'}
                      sandbox={'allow-scripts'}
                      height={'100%'}
                      width={'100%'}
                  >
                  </iframe>
             </div>
         </>
    )
}
    
export  default  RunCode;
1

There are 1 answers

1
HARDIK KAUSHIK On BEST ANSWER

The short answer is: You might not be importing the .css file to use the "Dracula" theme. Make sure you are importing these two files from node_modules in the component in which you are using ControlledEditor, to provide the theme to your editor.

  • import "codemirror/lib/codemirror.css";
  • import "codemirror/theme/dracula.css";

PS: My personal favorite theme is: ayu-dark ;)

I have also made a similar thing. Here is a Github Repo for your reference. :)

FunFact: @juliomalves helped me in deploying this project on vercel.