Intro
To read the content of a Markdown file in Node.js,
we need an interpreter that understands Markdown,
and can convert what he understood to HTML.
So what we need is just like a translator,
who can translate a context from one language to another.
The translator
There are many Markdown to HTML translators out there.
Since we're building a Node.js app, we need a translator that works in this environnement.
We'll be using, the best one in my humble opinion for this task,
Marked, a markdown parser and compiler.
Installation
To install Marked in a Node.js application,
type the following command in the terminal of your code editor:
npm install marked
You now have a powerful Markdown converter at your service 😉
Usage
To start using Marked, all we have to do is to fill its parse()
method with some Markdown.
It's as simple as the following block of code:
import { marked } from "marked"
const html = marked.parse("# An H1 heading converted from Markdown to HTML with Marked!")
The output of the code above would be:
<h1>An H1 heading converted from Markdown to HTML with Marked!</h1>
That's all folks, CU!