building a basic markdown editor: unified, trees and data

building-a-basic-markdown-editor:-unified,-trees-and-data

As you have probably guessed from the previous articles in this series (go read them if you haven’t, btw), the main thing when building a markdown publishing webapp, is the markdown editor itself.

Season 3 Walk GIF by The Simpsons

But not all editors are born equal; there are many ways of building one, with benefits and tradeoffs. In this article I’ll try to explain what led me to my current solution (not yet completed, but still I’d say it’s about 80% done).

Markdown

if you don’t know what markdown is, think of it as kind of like html; it is a type of text with special characters or combination of characters that get interpreted and displayed according to a determined specification; for instance, an html

tag would be translated in markdown with ## and vice versa. It is especially useful if you want to quickly write a README, some documentation or, turns out, articles for certain platforms. You can find a more complete introduction that I can provide here

What I needed to build

I needed to build a markdown editor with a live preview. The first part is not that difficult; put down a textarea on a page and you have your editor! The problem was with the live preview; turns out Markdown is not natively supported by browsers…

Excuse Me Wow GIF by Mashable

But you know what is? HTML! So I just needed to build (or find, in this case) a tool that allowed me to convert Markdown to HTML!

Tyler Perry Problem Solved GIF by Nickelodeon

Unified

To build the Markdown editor (and the preview, mostly), I decided to use unified, an ecosystem of tools allowing the developer to parse a format into an abstract tree and back into another format (for example, markdown to html) and modify said tree (for example, to add specific classes to certain html elements before they are converted to an actual html string. The basics of how to do so can be found in this article, but they mostly consist of:

  • reading the markdown content (from the textarea in this case)
  • using remarkParse to convert the markdown into a syntax tree
  • using remarkRehype to convert the markdown syntax tree to an html syntax tree
  • using rehypeStringify to convert the html syntax tree to an html string

GIF by South Park

The real magic is what happens once you generate the syntax trees; at that point, you can modify them with the existing plugins (or make you own, if you really want to). For instance, I use a plugin to add specific css classes to certain elements so they integrate better with the visual design of the website another to add code highlighting with highlight.js and some others for generating a js object from the frontmatter of a Markdown file and to add support for Github flavored Markdown. I could do a lot more with these, like add support for videos, embeds and more, but for now this is enough for a simple preview.

NB: remark and rehype are also used by astro to render the collection’s markdown content, so any plugins you use there can also be used here

Putting it all together

Now that we have a basic understanding of how unified works, let’s practice! I have already created a stackblitz example you can see just below this paragraph: try writing something in the textarea and it should be parsed and displayed in the preview next to it!

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
do-people-still-read-blogs-in-2023?-we-asked-consumers-[new-data]

Do People Still Read Blogs in 2023? We Asked Consumers [New Data]

Next Post
which-startup-marketing-agency-is-ideal-for-you?

Which Startup Marketing Agency Is Ideal for You?

Related Posts
harmonyos-next中密码类数据保护场景解析

HarmonyOS Next中密码类数据保护场景解析

本文旨在深入探讨华为鸿蒙HarmonyOS Next系统(截止目前 API12)在开发多语言电商平台方面的技术细节,基于实际开发实践进行总结。主要作为技术分享与交流载体,难免错漏,欢迎各位同仁提出宝贵意见和问题,以便共同进步。本文为原创内容,任何形式的转载必须注明出处及原作者。 在当今数字化时代,密码类数据的保护对于应用的安全性和用户体验至关重要。无论是登录账号、进行金融交易还是访问敏感信息,密码都起着关键的作用。HarmonyOS Next作为一款先进的操作系统,其提供的Asset Store Kit为密码类数据的安全存储和管理提供了强大的解决方案。 (一)引言 密码类数据保护的重要性    – 在移动应用领域,密码类数据是用户身份验证的核心凭证。一旦密码泄露,用户的账号安全将受到严重威胁,可能导致个人信息被窃取、财产遭受损失等严重后果。例如,在金融类应用中,如果用户的登录密码被泄露,黑客可能会非法访问用户的账户,进行转账、消费等操作。因此,确保密码类数据的安全性是应用开发者必须首要考虑的问题。 Asset Store Kit的关键作用    – HarmonyOS…
Read More