Working with Markdown in Node.js: Libraries for Parsing and Conversion
Markdown is a lightweight markup language widely used for formatting text in documentation, README files, blogs, and content management systems. In the Node.js ecosystem, there are several powerful libraries that allow you to parse Markdown and convert it to HTML, as well as perform the reverse operation. In this article, we will look at three of the most popular tools: marked, showdown, and remarkable, their features, API, and usage examples.
1. The marked Library
marked is a fast and lightweight Markdown parser that supports all basic syntax elements (headings, lists, links, images, code, tables, etc.). It is known for its high performance and ease of configuration.
Installation
npm install markedBasic Usage
To convert a Markdown string to HTML, simply call the marked() function:
const marked = require('marked');
const markdownText = `# Hello, world!
This is **bold text** and *italic*.
- List item 1- List item 2
[Link to PythonLib](https://pythonlib.ru)`;
const html = marked(markdownText);console.log(html);Execution result:
<h1>Hello, world!</h1><p>This is <strong>bold text</strong> and <em>italic</em>.</p><ul><li>List item 1</li><li>List item 2</li></ul><p><a href="https://pythonlib.ru">Link to PythonLib</a></p>Configuration Options
marked supports many options for customizing the output:
const marked = require('marked');
marked.setOptions({ breaks: true, // convert line breaks to <br> gfm: true, // enable GitHub Flavored Markdown (tables, strikethrough, etc.) headerIds: false, // do not add IDs to headings mangle: false, // do not escape email addresses sanitize: false // do not remove HTML tags (dangerous, use with caution)});
const html = marked('Line 1Line 2');console.log(html); // Will output with <br> between linesSynchronous and Asynchronous Usage
By default, marked() works synchronously, but it also supports asynchronous mode via promises:
const marked = require('marked');
async function convertMarkdown(text) { const html = await marked.parse(text); return html;}
convertMarkdown('# Heading').then(console.log);2. The showdown Library
showdown is another popular Markdown parser that can work both in the browser and in Node.js. It provides an extensible API and supports many extensions.
Installation
npm install showdownBasic Usage
Create a converter instance and call the makeHtml() method:
const showdown = require('showdown');const converter = new showdown.Converter();
const markdownText = '## Subheading
Regular text with **emphasis**.';const html = converter.makeHtml(markdownText);console.log(html);Result:
<h2>Subheading</h2><p>Regular text with <strong>emphasis</strong>.</p>Extensions and Customization
showdown allows you to add custom extensions, for example, to support emojis or LaTeX formulas:
const showdown = require('showdown');
// Create an extension to replace :smile: with an emojiconst emojiExtension = function() { return [{ type: 'lang', regex: /:smile:/g, replace: '😊' }];};