Markdown Exit Breaks the Rules With Async Rendering
This article is the continuation of and focuses on a more concrete use case: automatically optimizing images in Markdown files.
It is highly technical, and unless you're dealing with Markdown files, this could be a little abstract. Nevertheless, I highly encourage you to read both the previous and the current article, as it could give you some nice ideas.
Why write a dedicated article about it? Because, until now, this wasn't possible. The main Markdown parser in the JavaScript world is markdown-it, and it is fully synchronous.
This might not be an issue, but it means that all rules must be synchronous as well. You can't have asynchronous rules. You can't perform a fetch within a rule, and you can't parallelize anything.
A little recap is needed to make sure we're all on the same page.
I won't go into details, but essentially, the parser will split the Markdown text into tokens and then apply rules, which are basically functions, to those tokens to generate the HTML.
For example, the following text **bold text** will be tokenized into: ['strong_open', 'text', 'strong_close'] and the corresponding rules will generate the HTML: <strong>bold text</strong>. The token strong_open will be processed by the strong_open rule, which returns <strong>, and so on.
Thanks for reading! My name is Estéban, and I love to write about web development and the human journey around it.
I've been coding for several years, and I'm still learning new things every day. I share what I learn because I would have appreciated clear and complete resources when I started programming.
If you have a question or want to chat, leave a comment below or reach out through my social profiles.
I hope you learned something useful. Share the article, leave a comment, or add a reaction if you did. Support my work on GitHub.
Follow me
Estéban Soubiran
Software engineer, conference speaker, and open source enthusiast.
Discussions
Loading discussions...
Add a comment
Checking your session...