Introduction to CodeMirror - Effective Code and Markdown Editor

Introduction to CodeMirror - Effective Code and Markdown Editor

Daily short news for you
  • Today I have tried to walk a full 8k steps in one session to show you all. As expected, the time spent walking reached over 1 hour and the distance was around 6km 🤓

    Oh, in a few days it will be the end of the month, which means it will also mark one month since I started the habit of walking every day with the goal of 8k steps. At the beginning of next month, I will summarize and see how it goes.

    » Read more
  • It's been a long time since I've read such a heartfelt article The many, many, many JavaScript runtimes of the last decade. In the article, the author recounts the journey of the development of the JavaScript runtime environment, noting that today we have and are seeing JavaScript present in many areas. Additionally, the author also touches on a lot of peripheral knowledge; just a little reading opens up so many new discoveries 🤓

    » Read more
  • Android has seen the emergence of a Tapjacking attack. Essentially, malicious applications exploit your permission to access sensitive information on the device without your knowledge. You can learn more details here TapTrap.

    This reminds me of the DoubleClickjacking attack mentioned earlier in this Threads which is quite alarming.

    » Read more

The Problem

input and textarea are two HTML tags that are used to collect user input. input is suitable for short input, while textarea is used for longer, multiline input. Later on, I learned about another way to collect user input using contenteditable. Unlike input and textarea, contenteditable allows direct editing of HTML code. This means that the content in contenteditable can be very diverse.

However, there is a limitation. The two tags mentioned above only allow users to input plain text. If you want to do something more with the content, such as formatting text, syntax highlighting, creating shortcuts, etc., it becomes a challenging problem. Therefore, using these tags alone makes it difficult to create a text editor with rich formatting capabilities. contenteditable allows more flexibility, but it adds a lot of HTML code, making it difficult to extract the user's input. Moreover, each browser implements contenteditable differently, making it sometimes hard to understand.

Because of these limitations, many Text Editor libraries were developed to provide powerful text editing and formatting features. Some notable names include TinyMCE, CKEditor, WYSIWYG, etc. These tools have beautiful and modern interfaces with toolbar support for text formatting, just like a real word processor. They are used in scenarios where the input requires more than just plain text, including formatting, layout, images, etc. Most of them output HTML code, which can be saved and rendered by the browser to display a fully formatted page.

While these tools are powerful, they didn't meet my simple need for a Markdown editor with a live preview feature. If you look at the mentioned libraries, you might find a few that also support Markdown. I have tried them, but they were unable to fulfill the requirements or only supported a subset of Markdown syntax.

Sample editor

I had a simple requirement: something lightweight, fast, with syntax highlighting and easy customization, meaning I could add additional features smoothly. During my research, I came across a strong contender called SimpleMDE.

SimpleMDE lives up to its name, providing a super "simple" Markdown editor with all the essential features I needed. However, while exploring SimpleMDE's API, I discovered a new name: CodeMirror. At that point, I realized that SimpleMDE might be using CodeMirror as its foundation. Further investigation confirmed that CodeMirror is a powerful Markdown editor in itself.

CodeMirror

CodeMirror is an open-source JavaScript library used to create source code editors in web applications. It provides an interactive user interface for writing and editing source code in various programming languages. CodeMirror supports crucial features like syntax highlighting, code autocompletion, error checking, flexible layout, and many others.

CodeMirror is commonly used in web-based projects such as source code editors for open-source websites, integrated development environments (IDEs), blog post editors with code support, and other web applications that require code editing capabilities.

There are several advantages of using CodeMirror that convinced me and many others to use it:

  • Syntax Highlighting, Code Autocompletion, and Error Checking
  • Easy customizability, including adding keybindings and customizing the interface
  • A rich plugin ecosystem
  • Cross-platform compatibility, which is crucial in avoiding browser compatibility issues
  • Clear documentation, extensive API, and a large community

I am currently using CodeMirror in two places: a post editor in the Admin Control Panel and the comment section of my blog. Although they are not yet fully optimized, I have the ability to add or modify features as needed.

Implementation

Since CodeMirror is a JavaScript library, it can be easily integrated into any web page. If you are using a library like Vue, React, etc., there are packages available that turn CodeMirror into components for these libraries.

Here is an example of using CodeMirror for a simple Markdown editor:

import { basicSetup, EditorView } from "codemirror";
import { markdown } from "@codemirror/lang-markdown";
import { languages } from "@codemirror/language-data";

let view = new EditorView({
  doc: "Hello\n\n```javascript\nlet x = 'y'\n```",  
  extensions: [
    basicSetup,  
    markdown({ codeLanguages: languages }),  
  ],  
  parent: document.body,  
});

You can explore more examples at Try CodeMirror. Additionally, CodeMirror provides specific examples for various use cases at Examples.

Premium
Hello

Me & the desire to "play with words"

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

Have you tried writing? And then failed or not satisfied? At 2coffee.dev we have had a hard time with writing. Don't be discouraged, because now we have a way to help you. Click to become a member now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (0)

Leave a comment...