UNPKG

9.4 kBMarkdownView Raw
1# Turndown
2
3Convert HTML into Markdown with JavaScript.
4
5## Project Updates
6* `to-markdown` has been renamed to Turndown. See the [migration guide](https://github.com/domchristie/to-markdown/wiki/Migrating-from-to-markdown-to-Turndown) for details.
7* Turndown repository has changed its URL to https://github.com/mixmark-io/turndown.
8
9## Installation
10
11npm:
12
13```
14npm install turndown
15```
16
17Browser:
18
19```html
20<script src="https://unpkg.com/turndown/dist/turndown.js"></script>
21```
22
23For usage with RequireJS, UMD versions are located in `lib/turndown.umd.js` (for Node.js) and `lib/turndown.browser.umd.js` for browser usage. These files are generated when the npm package is published. To generate them manually, clone this repo and run `npm run build`.
24
25## Usage
26
27```js
28// For Node.js
29var TurndownService = require('turndown')
30
31var turndownService = new TurndownService()
32var markdown = turndownService.turndown('<h1>Hello world!</h1>')
33```
34
35Turndown also accepts DOM nodes as input (either element nodes, document nodes, or document fragment nodes):
36
37```js
38var markdown = turndownService.turndown(document.getElementById('content'))
39```
40
41## Options
42
43Options can be passed in to the constructor on instantiation. For example:
44
45```js
46var turndownService = new TurndownService({ option: 'value' })
47```
48
49| Option | Valid values | Default |
50| :-------------------- | :------------ | :------ |
51| `headingStyle` | `setext` or `atx` | `setext` |
52| `hr` | Any [Thematic break](http://spec.commonmark.org/0.27/#thematic-breaks) | `* * *` |
53| `bulletListMarker` | `-`, `+`, or `*` | `*` |
54| `codeBlockStyle` | `indented` or `fenced` | `indented` |
55| `fence` | ` ``` ` or `~~~` | ` ``` ` |
56| `emDelimiter` | `_` or `*` | `_` |
57| `strongDelimiter` | `**` or `__` | `**` |
58| `linkStyle` | `inlined` or `referenced` | `inlined` |
59| `linkReferenceStyle` | `full`, `collapsed`, or `shortcut` | `full` |
60| `preformattedCode` | `false` or [`true`](https://github.com/lucthev/collapse-whitespace/issues/16) | `false` |
61
62### Advanced Options
63
64| Option | Valid values | Default |
65| :-------------------- | :------------ | :------ |
66| `blankReplacement` | rule replacement function | See **Special Rules** below |
67| `keepReplacement` | rule replacement function | See **Special Rules** below |
68| `defaultReplacement` | rule replacement function | See **Special Rules** below |
69
70## Methods
71
72### `addRule(key, rule)`
73
74The `key` parameter is a unique name for the rule for easy reference. Example:
75
76```js
77turndownService.addRule('strikethrough', {
78 filter: ['del', 's', 'strike'],
79 replacement: function (content) {
80 return '~' + content + '~'
81 }
82})
83```
84
85`addRule` returns the `TurndownService` instance for chaining.
86
87See **Extending with Rules** below.
88
89### `keep(filter)`
90
91Determines which elements are to be kept and rendered as HTML. By default, Turndown does not keep any elements. The filter parameter works like a rule filter (see section on filters belows). Example:
92
93```js
94turndownService.keep(['del', 'ins'])
95turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>') // 'Hello <del>world</del><ins>World</ins>'
96```
97
98This will render `<del>` and `<ins>` elements as HTML when converted.
99
100`keep` can be called multiple times, with the newly added keep filters taking precedence over older ones. Keep filters will be overridden by the standard CommonMark rules and any added rules. To keep elements that are normally handled by those rules, add a rule with the desired behaviour.
101
102`keep` returns the `TurndownService` instance for chaining.
103
104### `remove(filter)`
105
106Determines which elements are to be removed altogether i.e. converted to an empty string. By default, Turndown does not remove any elements. The filter parameter works like a rule filter (see section on filters belows). Example:
107
108```js
109turndownService.remove('del')
110turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>') // 'Hello World'
111```
112
113This will remove `<del>` elements (and contents).
114
115`remove` can be called multiple times, with the newly added remove filters taking precedence over older ones. Remove filters will be overridden by the keep filters, standard CommonMark rules, and any added rules. To remove elements that are normally handled by those rules, add a rule with the desired behaviour.
116
117`remove` returns the `TurndownService` instance for chaining.
118
119### `use(plugin|array)`
120
121Use a plugin, or an array of plugins. Example:
122
123```js
124// Import plugins from turndown-plugin-gfm
125var turndownPluginGfm = require('turndown-plugin-gfm')
126var gfm = turndownPluginGfm.gfm
127var tables = turndownPluginGfm.tables
128var strikethrough = turndownPluginGfm.strikethrough
129
130// Use the gfm plugin
131turndownService.use(gfm)
132
133// Use the table and strikethrough plugins only
134turndownService.use([tables, strikethrough])
135```
136
137`use` returns the `TurndownService` instance for chaining.
138
139See **Plugins** below.
140
141## Extending with Rules
142
143Turndown can be extended by adding **rules**. A rule is a plain JavaScript object with `filter` and `replacement` properties. For example, the rule for converting `<p>` elements is as follows:
144
145```js
146{
147 filter: 'p',
148 replacement: function (content) {
149 return '\n\n' + content + '\n\n'
150 }
151}
152```
153
154The filter selects `<p>` elements, and the replacement function returns the `<p>` contents separated by two new lines.
155
156### `filter` String|Array|Function
157
158The filter property determines whether or not an element should be replaced with the rule's `replacement`. DOM nodes can be selected simply using a tag name or an array of tag names:
159
160 * `filter: 'p'` will select `<p>` elements
161 * `filter: ['em', 'i']` will select `<em>` or `<i>` elements
162
163The tag names in the `filter` property are expected in lowercase, regardless of their form in the document.
164
165Alternatively, the filter can be a function that returns a boolean depending on whether a given node should be replaced. The function is passed a DOM node as well as the `TurndownService` options. For example, the following rule selects `<a>` elements (with an `href`) when the `linkStyle` option is `inlined`:
166
167```js
168filter: function (node, options) {
169 return (
170 options.linkStyle === 'inlined' &&
171 node.nodeName === 'A' &&
172 node.getAttribute('href')
173 )
174}
175```
176
177### `replacement` Function
178
179The replacement function determines how an element should be converted. It should return the Markdown string for a given node. The function is passed the node's content, the node itself, and the `TurndownService` options.
180
181The following rule shows how `<em>` elements are converted:
182
183```js
184rules.emphasis = {
185 filter: ['em', 'i'],
186
187 replacement: function (content, node, options) {
188 return options.emDelimiter + content + options.emDelimiter
189 }
190}
191```
192
193### Special Rules
194
195**Blank rule** determines how to handle blank elements. It overrides every rule (even those added via `addRule`). A node is blank if it only contains whitespace, and it's not an `<a>`, `<td>`,`<th>` or a void element. Its behaviour can be customised using the `blankReplacement` option.
196
197**Keep rules** determine how to handle the elements that should not be converted, i.e. rendered as HTML in the Markdown output. By default, no elements are kept. Block-level elements will be separated from surrounding content by blank lines. Its behaviour can be customised using the `keepReplacement` option.
198
199**Remove rules** determine which elements to remove altogether. By default, no elements are removed.
200
201**Default rule** handles nodes which are not recognised by any other rule. By default, it outputs the node's text content (separated by blank lines if it is a block-level element). Its behaviour can be customised with the `defaultReplacement` option.
202
203### Rule Precedence
204
205Turndown iterates over the set of rules, and picks the first one that matches the `filter`. The following list describes the order of precedence:
206
2071. Blank rule
2082. Added rules (optional)
2093. Commonmark rules
2104. Keep rules
2115. Remove rules
2126. Default rule
213
214## Plugins
215
216The plugin API provides a convenient way for developers to apply multiple extensions. A plugin is just a function that is called with the `TurndownService` instance.
217
218## Escaping Markdown Characters
219
220Turndown uses backslashes (`\`) to escape Markdown characters in the HTML input. This ensures that these characters are not interpreted as Markdown when the output is compiled back to HTML. For example, the contents of `<h1>1. Hello world</h1>` needs to be escaped to `1\. Hello world`, otherwise it will be interpreted as a list item rather than a heading.
221
222To avoid the complexity and the performance implications of parsing the content of every HTML element as Markdown, Turndown uses a group of regular expressions to escape potential Markdown syntax. As a result, the escaping rules can be quite aggressive.
223
224### Overriding `TurndownService.prototype.escape`
225
226If you are confident in doing so, you may want to customise the escaping behaviour to suit your needs. This can be done by overriding `TurndownService.prototype.escape`. `escape` takes the text of each HTML element and should return a version with the Markdown characters escaped.
227
228Note: text in code elements is never passed to`escape`.
229
230## License
231
232turndown is copyright © 2017+ Dom Christie and released under the MIT license.