How to read the content of a JS file and insert it into CodeMirror


#1

I’m using react-codemirror and I want to insert the content of a javascript file into the editor but since react-codemirror only receives text I don’t know how to implement this.

Suppose that I have a file

index.js

function findSequence(goal) {
  function find(start, history) {
    if (start == goal)
      return history;
    else if (start > goal)
      return null;
    else
      return find(start + 5, "(" + history + " + 5)") ||
             find(start * 3, "(" + history + " * 3)");
  }
  return find(1, "1");
}

How can I read this file and insert its content in the editor as a string but highlighted just like here?


#2

This is outside of the scope of this library. In the browser you normally don’t have file system access, though there are exceptions and you can make HTTP requests.