Working with Markdown in Node.js: Overview of Libraries and Tools
Markdown is a popular markup language used for formatting text in documentation, blogs, README files, and educational platforms. In the Node.js ecosystem, there are several powerful libraries that allow you to convert Markdown to HTML, process code, tables, and other elements. In this article, we will look at three of the most popular tools: marked, remarkable, and showdown, and also analyze examples of their use.
1. The marked Library
Marked is one of the fastest and lightest libraries for parsing Markdown. It has no external dependencies and supports CommonMark and GFM (GitHub Flavored Markdown).
Installation
npm install markedBasic Usage
const marked = require('marked');
const markdownString = '# HeadingThis is **bold** text and *italic*.
- Item 1- Item 2';
const html = marked.parse(markdownString);console.log(html);Result:
<h1 id="heading">Heading</h1><p>This is <strong>bold</strong> text and <em>italic</em>.</p><ul><li>Item 1</li><li>Item 2</li></ul>Configuration Options
Marked supports many options for fine-tuning parsing:
const marked = require('marked');
marked.setOptions({ breaks: true, // line breaks to <br> gfm: true, // GFM support headerIds: false // disable ids on headings});
const html = marked.parse('Line 1Line 2');console.log(html);// <p>Line 1<br>Line 2</p>2. The remarkable Library
Remarkable is a flexible library with support for plugins and extensions. It allows you to add syntax for mathematical formulas, diagrams, and other specific elements.
Installation
npm install remarkableUsage Example
const { Remarkable } = require('remarkable');const md = new Remarkable();
const markdown = '## Subheading
`code` and `inline code`';const html = md.render(markdown);console.log(html);Output:
<h2>Subheading</h2><p><code>code</code> and <code>inline code</code></p>Plugins and Extensions
Remarkable supports plugins for processing tables, task lists, and other elements:
const { Remarkable } = require('remarkable');const md = new Remarkable({ html: true, linkify: true});
// Adding a plugin for tablesmd.renderer.rules.table_open = function() { return '<table class="table">';};
const html = md.render('| Column 1 | Column 2 ||-----------|-----------|| Data 1 | Data 2 |');console.log(html);3. The showdown Library
Showdown is a port of the original Showdown for JavaScript. It is easy to use and well-suited for client-server applications.
Installation
npm install showdownCode Example
const showdown = require('showdown');const converter = new showdown.Converter();
const markdown = '**Bold** and *italic*';const html = converter.makeHtml(markdown);console.log(html);// <p><strong>Bold</strong> and <em>italic</em></p>Configuring Extensions
Showdown supports extensions that can be added to enable new features:
const showdown = require('showdown');
// Creating an extension for code highlightingconst myExt = { type: 'output',