9.07.2020

I made a simple markdown editor with vanilla javascript


Making a Markdown Editor with JavaScript

Two days ago, I started to make a custom markdown editor with JavaScript. Today in this blog post, I want to share the making process so that the other people who want to make their own custom markdown editor will get som help in this article.

Experiment : try to make it work with repl

I spent most of my time in repl project file when I was trying to find basic blueprint and logic of this editor that I want to make. I strongly recommend for the other people to do it as well. It doesn't matter if you're using repl or codepen or something else. This kind of services make our project easy to test, especially when your project is a combination of HTML, CSS and JS. Below is the repl code for this project. You can just directly learn from a code, or you can read the summary of the techniques that I needed for this project which I will explain at the bottom.


repl page : https://repl.it/@SooYoungKim/markdown-editor#index.html

keep in mind that repl code is not a final code, final code is in my github code


Vanilla JavaScript techniques that helped me a LOT

These are the techniques and JavaScript methods that I learned this time. I guess if there's someone who want to make a markdown editor, I think he or she will also need these kind of techniques.


  • div tag and contenteditable attribute

  • window.getSelection()

    • anchorNode

    • focusNode

    • removeAllRanges()

    • addRange()

  • document.createRange()

    • setStart()

    • setEnd()

    • collapse()

    • surroundContents

    • insertNode

    • deleteContents

  • event.keyCode

  • String

    • String.match()

    • String.replace(),

    • RegExp

    • String.indexOf()

    • String.lastIndexOf()

  • a tag download attribute, encodeURIComponent()

  • parentNode.insertBefore()

  • childNode.after()

Above techniques are the core techniques that helped me to build this markdown editor. You can google it each concept, or you can come back later when you want to add certain functionality but don't know how to.

Finally, I put it on to React project

Someone told me before that I should not directly use DOM inside of an React project. I understand what they're trying to say. But at the current moment, I have absolutely no idea how to implement this vanilla JS project to React style(using state, hooks etc but not pure DOM). However, I'm planning to use this editor on more big scale personal project and there're good chances that I will use React. So, I turned into React project, but still using DOM directly. I guess I can figure it out how to code them more "Reactfully" later on.

If anyone who want to see the code, or want to help me with the react code, I'll leave a github address, and deployed URL.


github code

you can use this markdown editor in here!

Share:

0 comments:

댓글 쓰기