@cloudrac3r/turndown 7.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Dom Christie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,222 @@
1
+ # Turndown
2
+
3
+ [![Build Status](https://travis-ci.org/domchristie/turndown.svg?branch=master)](https://travis-ci.org/domchristie/turndown)
4
+
5
+ Convert HTML into Markdown with JavaScript.
6
+
7
+ ## Installation
8
+
9
+ npm:
10
+
11
+ ```
12
+ npm install @cloudrac3r/turndown
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```js
18
+ // For Node.js
19
+ var TurndownService = require('@cloudrac3r/turndown')
20
+
21
+ var turndownService = new TurndownService()
22
+ var markdown = turndownService.turndown('<h1>Hello world!</h1>')
23
+ ```
24
+
25
+ Turndown also accepts DOM nodes as input (either element nodes, document nodes, or document fragment nodes):
26
+
27
+ ```js
28
+ var markdown = turndownService.turndown(document.getElementById('content'))
29
+ ```
30
+
31
+ ## Options
32
+
33
+ Options can be passed in to the constructor on instantiation. For example:
34
+
35
+ ```js
36
+ var turndownService = new TurndownService({ option: 'value' })
37
+ ```
38
+
39
+ | Option | Valid values | Default |
40
+ | :-------------------- | :------------ | :------ |
41
+ | `headingStyle` | `setext` or `atx` | `setext` |
42
+ | `hr` | Any [Thematic break](http://spec.commonmark.org/0.27/#thematic-breaks) | `* * *` |
43
+ | `bulletListMarker` | `-`, `+`, or `*` | `*` |
44
+ | `codeBlockStyle` | `indented` or `fenced` | `indented` |
45
+ | `fence` | ` ``` ` or `~~~` | ` ``` ` |
46
+ | `emDelimiter` | `_` or `*` | `_` |
47
+ | `strongDelimiter` | `**` or `__` | `**` |
48
+ | `linkStyle` | `inlined` or `referenced` | `inlined` |
49
+ | `linkReferenceStyle` | `full`, `collapsed`, or `shortcut` | `full` |
50
+ | `preformattedCode` | `false` or [`true`](https://github.com/lucthev/collapse-whitespace/issues/16) | `false` |
51
+
52
+ ### Advanced Options
53
+
54
+ | Option | Valid values | Default |
55
+ | :-------------------- | :------------ | :------ |
56
+ | `blankReplacement` | rule replacement function | See **Special Rules** below |
57
+ | `keepReplacement` | rule replacement function | See **Special Rules** below |
58
+ | `defaultReplacement` | rule replacement function | See **Special Rules** below |
59
+
60
+ ## Methods
61
+
62
+ ### `addRule(key, rule)`
63
+
64
+ The `key` parameter is a unique name for the rule for easy reference. Example:
65
+
66
+ ```js
67
+ turndownService.addRule('strikethrough', {
68
+ filter: ['del', 's', 'strike'],
69
+ replacement: function (content) {
70
+ return '~' + content + '~'
71
+ }
72
+ })
73
+ ```
74
+
75
+ `addRule` returns the `TurndownService` instance for chaining.
76
+
77
+ See **Extending with Rules** below.
78
+
79
+ ### `keep(filter)`
80
+
81
+ Determines 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:
82
+
83
+ ```js
84
+ turndownService.keep(['del', 'ins'])
85
+ turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>') // 'Hello <del>world</del><ins>World</ins>'
86
+ ```
87
+
88
+ This will render `<del>` and `<ins>` elements as HTML when converted.
89
+
90
+ `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.
91
+
92
+ `keep` returns the `TurndownService` instance for chaining.
93
+
94
+ ### `remove(filter)`
95
+
96
+ Determines 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:
97
+
98
+ ```js
99
+ turndownService.remove('del')
100
+ turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>') // 'Hello World'
101
+ ```
102
+
103
+ This will remove `<del>` elements (and contents).
104
+
105
+ `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.
106
+
107
+ `remove` returns the `TurndownService` instance for chaining.
108
+
109
+ ### `use(plugin|array)`
110
+
111
+ Use a plugin, or an array of plugins. Example:
112
+
113
+ ```js
114
+ // Import plugins from turndown-plugin-gfm
115
+ var turndownPluginGfm = require('turndown-plugin-gfm')
116
+ var gfm = turndownPluginGfm.gfm
117
+ var tables = turndownPluginGfm.tables
118
+ var strikethrough = turndownPluginGfm.strikethrough
119
+
120
+ // Use the gfm plugin
121
+ turndownService.use(gfm)
122
+
123
+ // Use the table and strikethrough plugins only
124
+ turndownService.use([tables, strikethrough])
125
+ ```
126
+
127
+ `use` returns the `TurndownService` instance for chaining.
128
+
129
+ See **Plugins** below.
130
+
131
+ ## Extending with Rules
132
+
133
+ Turndown 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:
134
+
135
+ ```js
136
+ {
137
+ filter: 'p',
138
+ replacement: function (content) {
139
+ return '\n\n' + content + '\n\n'
140
+ }
141
+ }
142
+ ```
143
+
144
+ The filter selects `<p>` elements, and the replacement function returns the `<p>` contents separated by two new lines.
145
+
146
+ ### `filter` String|Array|Function
147
+
148
+ The 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:
149
+
150
+ * `filter: 'p'` will select `<p>` elements
151
+ * `filter: ['em', 'i']` will select `<em>` or `<i>` elements
152
+
153
+ The tag names in the `filter` property are expected in lowercase, regardless of their form in the document.
154
+
155
+ Alternatively, 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`:
156
+
157
+ ```js
158
+ filter: function (node, options) {
159
+ return (
160
+ options.linkStyle === 'inlined' &&
161
+ node.nodeName === 'A' &&
162
+ node.getAttribute('href')
163
+ )
164
+ }
165
+ ```
166
+
167
+ ### `replacement` Function
168
+
169
+ The 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.
170
+
171
+ The following rule shows how `<em>` elements are converted:
172
+
173
+ ```js
174
+ rules.emphasis = {
175
+ filter: ['em', 'i'],
176
+
177
+ replacement: function (content, node, options) {
178
+ return options.emDelimiter + content + options.emDelimiter
179
+ }
180
+ }
181
+ ```
182
+
183
+ ### Special Rules
184
+
185
+ **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.
186
+
187
+ **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.
188
+
189
+ **Remove rules** determine which elements to remove altogether. By default, no elements are removed.
190
+
191
+ **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.
192
+
193
+ ### Rule Precedence
194
+
195
+ Turndown iterates over the set of rules, and picks the first one that matches the `filter`. The following list describes the order of precedence:
196
+
197
+ 1. Blank rule
198
+ 2. Added rules (optional)
199
+ 3. Commonmark rules
200
+ 4. Keep rules
201
+ 5. Remove rules
202
+ 6. Default rule
203
+
204
+ ## Plugins
205
+
206
+ The 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.
207
+
208
+ ## Escaping Markdown Characters
209
+
210
+ Turndown 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.
211
+
212
+ To 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.
213
+
214
+ ### Overriding `TurndownService.prototype.escape`
215
+
216
+ If 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.
217
+
218
+ Note: text in code elements or link elements is never passed to`escape`.
219
+
220
+ ## License
221
+
222
+ turndown is copyright © 2017+ Dom Christie and released under the MIT license.