Introduction to CodeMirror - Effective Code and Markdown Editor

Introduction to CodeMirror - Effective Code and Markdown Editor

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » 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...