The turndown-plugin-gfm extends Turndown with support for GitHub Flavored Markdown (GFM) features that aren’t part of the CommonMark specification. This includes:
Tables - Convert HTML tables to Markdown pipe tables
Strikethrough - Convert <del>, <s>, and <strike> to ~~strikethrough~~
Task lists - Convert checkboxes to - [ ] and - [x] syntax
Autolinks - Improved handling of URLs
The GFM plugin is maintained as a separate package and must be installed alongside Turndown.
The simplest approach is to use the complete gfm plugin, which includes all GFM features:
// Import Turndown and the pluginvar TurndownService = require('turndown')var turndownPluginGfm = require('turndown-plugin-gfm')// Get the gfm pluginvar gfm = turndownPluginGfm.gfm// Create service and apply pluginvar turndownService = new TurndownService()turndownService.use(gfm)// Convert HTML with GFM featuresvar html = ` <table> <tr> <th>Name</th> <th>Role</th> </tr> <tr> <td>Alice</td> <td>Developer</td> </tr> </table> <p>This is <del>deleted</del> text</p>`var markdown = turndownService.turndown(html)console.log(markdown)
Output:
| Name | Role || ----- | --------- || Alice | Developer |This is ~~deleted~~ text
You can also use specific GFM features individually:
var TurndownService = require('turndown')var turndownPluginGfm = require('turndown-plugin-gfm')// Import individual pluginsvar tables = turndownPluginGfm.tablesvar strikethrough = turndownPluginGfm.strikethrough// Use only the plugins you needvar turndownService = new TurndownService()turndownService.use([tables, strikethrough])
Here’s a comprehensive example demonstrating all GFM features:
const TurndownService = require('turndown')const { gfm } = require('turndown-plugin-gfm')const turndownService = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced'})turndownService.use(gfm)const html = ` <h1>Project Tasks</h1> <h2>Progress</h2> <ul> <li><input type="checkbox" checked> Set up repository</li> <li><input type="checkbox" checked> Create initial docs</li> <li><input type="checkbox"> Add tests</li> <li><input type="checkbox"> Deploy to production</li> </ul> <h2>Team Members</h2> <table> <thead> <tr> <th>Name</th> <th>Role</th> <th>Status</th> </tr> </thead> <tbody> <tr> <td>Alice</td> <td>Lead Developer</td> <td>Active</td> </tr> <tr> <td>Bob</td> <td>Designer</td> <td>Active</td> </tr> <tr> <td>Charlie</td> <td>Developer</td> <td><del>On leave</del></td> </tr> </tbody> </table> <h2>Notes</h2> <p>The old approach is <strike>deprecated</strike>. Use the new API instead.</p>`const markdown = turndownService.turndown(html)console.log(markdown)
Output:
# Project Tasks## Progress- [x] Set up repository- [x] Create initial docs- [ ] Add tests- [ ] Deploy to production## Team Members| Name | Role | Status || ------- | -------------- | ------------ || Alice | Lead Developer | Active || Bob | Designer | Active || Charlie | Developer | ~~On leave~~ |## NotesThe old approach is ~~deprecated~~. Use the new API instead.
var turndownService = new TurndownService()// Plugin 1 is applied firstturndownService.use(plugin1)// Plugin 2 can override rules from plugin 1turndownService.use(plugin2)// Custom rules can override plugin rulesturndownService.addRule('customRule', { /* ... */ })
Custom rules added via addRule() take precedence over plugin rules. Add plugins before custom rules if you want to override plugin behavior.
The GFM plugin can be loaded via CDN for browser usage:
<!DOCTYPE html><html><head> <script src="https://unpkg.com/turndown/dist/turndown.js"></script> <script src="https://unpkg.com/turndown-plugin-gfm/dist/turndown-plugin-gfm.js"></script></head><body> <script> var turndownService = new TurndownService() var gfm = turndownPluginGfm.gfm turndownService.use(gfm) // Now you can convert HTML with GFM features var markdown = turndownService.turndown('<p>Text with <del>strikethrough</del></p>') console.log(markdown) // Text with ~~strikethrough~~ </script></body></html>