DEV Community

loading...
Cover image for Let’s Develop An Online Code Editor/Compiler Like HackerRank

Let’s Develop An Online Code Editor/Compiler Like HackerRank

nilmadhabmondal profile image Nil Madhab Updated on ・2 min read

In this article, I will explain how I made my own online compiler using open source code.

Here I will make one editor that supports 3 languages; C++, Java, and Python.

Adding more languages is also very simple.
Alt Text

Overview

Building an online code editor and compiler can seem too complicated, but we can break it down into two pieces.

  1. API running on the backend server, which will take a piece of code and language as input and output the answer after running the code on the server
  2. Frontend code editor, we can choose the language and edit and modify the code here. Then we make a post request to the backend API and show output on the website

Tutorials in this Series

  1. Deploy our Online Code Executor in Google Cloud
  2. Understanding the API of Judge0
  3. Creating an online code editor front-end (this tutorial)

Demo

This is the webpage we are going to build.

you can find the live demo here

Online code editor demo

FrontEnd

Our frontend is pretty simple. We have one drop-down menu where we can select the language and depend upon the language we will get our respective code editors. Our main code lies in the home-page.html and the code-editor resides in the texteditor.js.

Here we have made one onClickListener for dropdown menu items. So when one item is clicked, the corresponding listener gets triggered and we can call our desired editors. All the languages have different id associated with it . The list can be found using this.

The home-page looks like this.
Alt Text
The home-page html code is given below.

Here we call the codeEditor function of the javascript file , we pass the respective id associated with the language.

We add the style from assets/css/style.css .


Also we need to link the javascript file so that the code can be executed. We get the lang_id from the html and do the network call accordingly. For java code is 62, cpp 53 and python 70. This is passed in language_id . We also set default values to the editor depending on the lang_id.

On execution of code it should show like this.
Alt Text

The entire code can be found in the following repository.

Ace (Ajax.org Cloud9 Editor)

CDNJS npm

Ace is a code editor written in JavaScript.

This repository has only generated files. If you want to work on ace please go to https://github.com/ajaxorg/ace instead.

here you can find pre-built files for convenience of embedding. it contains 4 versions

  • src concatenated but not minified
  • src-min concatenated and minified with uglify.js
  • src-noconflict uses ace.require instead of require
  • src-min-noconflict concatenated, minified with uglify.js, and uses ace.require instead of require

For a simple way of embedding ace into webpage see editor.html or list of other simple examples To see ace in action go to kitchen-sink-demo, scrollable-page-demo or minimal demo,




Discussion (0)

pic
Editor guide