What is turndown-plugin-gfm?
The turndown-plugin-gfm package is a plugin for Turndown, a library that converts HTML into Markdown. This plugin specifically adds support for GitHub Flavored Markdown (GFM) to Turndown, enabling the conversion of HTML elements that are unique to GFM.
What are turndown-plugin-gfm's main functionalities?
Strikethrough
This feature converts HTML <del> tags into GFM strikethrough syntax using double tildes (~~).
const TurndownService = require('turndown');
const gfm = require('turndown-plugin-gfm').gfm;
const turndownService = new TurndownService();
turndownService.use(gfm);
const html = '<p><del>Strikethrough text</del></p>';
const markdown = turndownService.turndown(html);
console.log(markdown); // Outputs: '~~Strikethrough text~~'
Tables
This feature converts HTML tables into GFM table syntax, which is a common requirement for documentation and README files.
const TurndownService = require('turndown');
const gfm = require('turndown-plugin-gfm').gfm;
const turndownService = new TurndownService();
turndownService.use(gfm);
const html = '<table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td></tr></tbody></table>';
const markdown = turndownService.turndown(html);
console.log(markdown); // Outputs: '| Header 1 | Header 2 |
| --- | --- |
| Data 1 | Data 2 |'
Task Lists
This feature converts HTML checkboxes within list items into GFM task list syntax, which is useful for project management and to-do lists.
const TurndownService = require('turndown');
const gfm = require('turndown-plugin-gfm').gfm;
const turndownService = new TurndownService();
turndownService.use(gfm);
const html = '<ul><li><input type="checkbox" checked> Task 1</li><li><input type="checkbox"> Task 2</li></ul>';
const markdown = turndownService.turndown(html);
console.log(markdown); // Outputs: '- [x] Task 1
- [ ] Task 2'
Other packages similar to turndown-plugin-gfm
showdown
Showdown is a bidirectional converter that can convert HTML to Markdown and vice versa. It supports a wide range of Markdown flavors, including GFM. Compared to turndown-plugin-gfm, Showdown is more versatile as it can handle both conversions.
markdown-it
Markdown-it is a Markdown parser that can be extended with plugins to support GFM and other Markdown flavors. While it primarily focuses on parsing Markdown to HTML, it can be extended to support HTML to Markdown conversion as well. It is highly customizable and fast.
remark
Remark is a Markdown processor powered by plugins. It can parse, transform, and compile Markdown. With the appropriate plugins, it can support GFM and convert HTML to Markdown. Remark is known for its flexibility and extensive plugin ecosystem.
turndown-plugin-gfm
A Turndown plugin which adds GitHub Flavored Markdown extensions.
Installation
npm:
npm install turndown-plugin-gfm
Browser:
<script src="https://unpkg.com/turndown/dist/turndown.js"></script>
<script src="https://unpkg.com/turndown-plugin-gfm/dist/turndown-plugin-gfm.js"></script>
Usage
var TurndownService = require('turndown')
var turndownPluginGfm = require('turndown-plugin-gfm')
var gfm = turndownPluginGfm.gfm
var turndownService = new TurndownService()
turndownService.use(gfm)
var markdown = turndownService.turndown('<strike>Hello world!</strike>')
turndown-plugin-gfm is a suite of plugins which can be applied individually. The available plugins are as follows:
strikethrough
(for converting <strike>
, <s>
, and <del>
elements)tables
taskListItems
gfm
(which applies all of the above)
So for example, if you only wish to convert tables:
var tables = require('turndown-plugin-gfm').tables
var turndownService = new TurndownService()
turndownService.use(tables)
License
turndown-plugin-gfm is copyright © 2017+ Dom Christie and released under the MIT license.