What Are Plugins?
A plugin is a function that takes a TurndownService instance and extends it with custom rules, options, or behavior:use() method:
Using Plugins
Theuse() method accepts either a single plugin function or an array of plugins.
Single Plugin
Multiple Plugins
use() returns the TurndownService instance, allowing you to chain method calls.Official Plugins
turndown-plugin-gfm
Adds support for GitHub Flavored Markdown (GFM) extensions:gfm- All GFM features (combines all plugins below)tables- Table supportstrikethrough- Strikethrough text (~~text~~)taskListItems- Task list checkboxes
Creating Custom Plugins
Create a plugin by writing a function that configures a TurndownService instance.Basic Plugin Structure
Plugin with Multiple Rules
Plugin with Options
Create a plugin factory that accepts configuration:Plugin that Modifies Service Behavior
Plugins can do more than add rules:Packaging Plugins
When creating a plugin package for distribution:Module Structure
Exporting Multiple Plugins
Complete Plugin Example
Here’s a complete plugin that adds support for Bootstrap alert components:Plugin Best Practices
Best practices:
- Keep plugins focused on a single feature or set of related features
- Document all rules and behavior your plugin adds
- Test plugins with various HTML inputs
- Consider edge cases (empty content, nested elements, etc.)
- Follow Markdown and CommonMark specifications
- Provide options for customization when appropriate
Loading Plugins
Plugins can be loaded in different ways depending on your environment:Node.js
ES Modules
Browser (CDN)
Related
- Rules - Learn about the rule system that powers plugins
- API Reference - Detailed use() method documentation
- Examples - More plugin examples