Showdown Markdown to HTML Converter
Showdown is an open-source JavaScript library designed for converting Markdown formatted text into HTML. It is a robust, cross-platform solution, capable of operating both client-side in web browsers and server-side within Node.js environments. The current stable version is 2.1.0, with releases occurring periodically to address bugs, update dependencies, and introduce features; for instance, version 2.0.0 primarily focused on maintenance, updating its `yargs` dependency and Node.js support. Key differentiators include its foundational basis on John Gruber's original Markdown specifications, long-standing stability, and an experimental HTML to Markdown converter introduced in version 1.9.0, providing versatile bidirectional conversion capabilities. Unlike some newer, more opinionated Markdown parsers, Showdown offers extensive configuration options to control the conversion process, allowing users to enable or disable various Markdown features.
Common errors
-
ReferenceError: showdown is not defined
cause The Showdown library was not properly loaded in the browser environment, or accessed incorrectly in a module context.fixIn browsers, ensure you have included `<script src="path/to/showdown.min.js"></script>` before attempting to use it. In a module, ensure it's imported via `require` or `import`. -
TypeError: showdown.Converter is not a constructor
cause Attempted to call `showdown.Converter()` as a function instead of instantiating it with `new`.fixAlways use `new showdown.Converter()` to create a new converter instance. -
Error: Unknown option: --strikethrough (or other CLI options)
cause Using the pre-v2.1.0 CLI syntax for extra options after upgrading to Showdown 2.1.0 or newer.fixFor CLI options like `strikethrough` or `emoji`, use the `-c` flag for each option. E.g., `showdown makehtml -i input.md -o output.html -c strikethrough -c emoji`. -
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` in an ECMAScript Module (ESM) context (e.g., a Node.js file with `"type": "module"` in `package.json`).fixRefactor your import statements to use ESM syntax: `import { Converter } from 'showdown';` and then `new Converter();`.
Warnings
- breaking The command-line interface (CLI) for `showdown` changed how 'extra options' are passed. Instead of direct flags like `--strikethrough`, you must now use the `-c` flag for each option.
- breaking Supported Node.js versions were updated to align with the official Node.js release schedule. This dropped support for older, unmaintained Node.js versions.
- breaking The library's license changed from BSD to MIT with the 2.0.0 release. This primarily affects legal compliance and redistribution terms.
- gotcha The `yargs` dependency, used for CLI parsing, was updated to mitigate security vulnerabilities. While primarily an internal change, it could potentially introduce subtle behavioral differences or require adjustments if you relied on specific `yargs` behaviors in custom CLI scripts.
- gotcha While Showdown claims compatibility with very old browsers (e.g., IE6, Firefox 1.5), using it in such environments may not be practical or fully functional with modern Markdown features. Modern development typically targets more recent browser versions.
Install
-
npm install showdown -
yarn add showdown -
pnpm add showdown
Imports
- Converter
import showdown from 'showdown';
import { Converter } from 'showdown'; - showdown (CommonJS object)
const { Converter } = require('showdown');const showdown = require('showdown'); - Converter (Global in browser)
const converter = new Converter();
const converter = new showdown.Converter();
Quickstart
const showdown = require('showdown');
const converter = new showdown.Converter({
emoji: true,
strikethrough: true,
tables: true,
tasklists: true,
openLinksInNewWindow: true
});
const markdownText = `
# Hello, Showdown!
This is a **Markdown** example with some common features.
- List item 1
- List item 2
- Sub-item
~~Strikethrough text~~ and an :emoji:! 🎉
| Header 1 | Header 2 |
|---|---|
| Cell 1 | Cell 2 |
- [x] Task 1 (completed)
- [ ] Task 2 (pending)
[Visit ShowdownJS](https://showdownjs.com/)
`;
const html = converter.makeHtml(markdownText);
console.log(html);
// Expected output: An HTML string representing the converted Markdown.