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