How to render Markdown content in Express-Nodejs projects


Markdown is a great tool for documenting projects, composing content without using HTML component. We can use markdown on content based Nodejs projects too.

How do we render markdown content in Express-Nodejs project ?. Here is a special npm package marked which can be used to render the content to pure HTML

How to use

Install and add dependency to project

npm i marked --save

import the package

const marked =require('marked')

Convert the markdown content to HTML using marked

if(this.markDownTxt){
 this.markdHtml=marked(this.mardowntxt)
}

How to render on Model Schema (mongoose)

The conversion can be automatically performed in Model(Mongoose) using pre as follows

postSchema.pre('validate',function(next){
if(this.markdown){
 this.mdHtml=marked(this.markdown)
}
next()
})

Published by

Manoj

A developer,teacher and a blogger obsessed with Python,Dart and open source world from India

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.