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
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.
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()
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.