Socket
Socket
Sign inDemoInstall

turndown

Package Overview
Dependencies
95
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.0.0-rc.2 to 4.0.0-rc.3

99

dist/turndown.js

@@ -276,2 +276,4 @@ var TurndownService = (function () {

this.options = options;
this._keep = [];
this._remove = [];

@@ -282,2 +284,4 @@ this.blankRule = {

this.keepReplacement = options.keepReplacement;
this.defaultRule = {

@@ -287,17 +291,3 @@ replacement: options.defaultReplacement

var keepRule = options.keepRule || {
filter: options.keep,
replacement: function (content, node) {
return node.isBlock ? '\n\n' + content + '\n\n' : content
}
};
var removeRule = options.removeRule || {
filter: options.remove,
replacement: function () {
return ''
}
};
this.array = [keepRule, removeRule];
this.array = [];
for (var key in options.rules) this.array.push(options.rules[key]);

@@ -311,9 +301,25 @@ }

keep: function (filter) {
this._keep.unshift({
filter: filter,
replacement: this.keepReplacement
});
},
remove: function (filter) {
this._remove.unshift({
filter: filter,
replacement: function () {
return ''
}
});
},
forNode: function (node) {
if (node.isBlank) return this.blankRule
var rule;
for (var i = 0; i < this.array.length; i++) {
var rule = this.array[i];
if (filterValue(rule, node, this.options)) return rule
}
if ((rule = findRule(this.array, node, this.options))) return rule
if ((rule = findRule(this._keep, node, this.options))) return rule
if ((rule = findRule(this._remove, node, this.options))) return rule

@@ -328,2 +334,10 @@ return this.defaultRule

function findRule (rules, node, options) {
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (filterValue(rule, node, options)) return rule
}
return void 0
}
function filterValue (rule, node, options) {

@@ -638,3 +652,10 @@ var filter = rule.filter;

if (typeof input === 'string') {
root = htmlParser().parseFromString(input, 'text/html').body;
var doc = htmlParser().parseFromString(
// DOM parsers arrange elements in the <head> and <body>.
// Wrapping in a custom element ensures elements are reliably arranged in
// a single element.
'<x-turndown id="turndown-root">' + input + '</x-turndown>',
'text/html'
);
root = doc.getElementById('turndown-root');
} else {

@@ -742,13 +763,5 @@ root = input.cloneNode(true);

},
keep: function (node) {
switch (node.nodeName) {
case 'TABLE':
return true
case 'PRE':
return node.firstChild && node.firstChild.nodeName !== 'CODE'
default:
return false
}
keepReplacement: function (content, node) {
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
},
remove: ['head', 'link', 'meta', 'script', 'style'],
defaultReplacement: function (content, node) {

@@ -818,2 +831,28 @@ return node.isBlock ? '\n\n' + content + '\n\n' : content

/**
* Keep a node (as HTML) that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
keep: function (filter) {
this.rules.keep(filter);
return this
},
/**
* Remove a node that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
remove: function (filter) {
this.rules.remove(filter);
return this
},
/**
* Escapes Markdown syntax

@@ -820,0 +859,0 @@ * @public

@@ -275,2 +275,4 @@ 'use strict';

this.options = options;
this._keep = [];
this._remove = [];

@@ -281,2 +283,4 @@ this.blankRule = {

this.keepReplacement = options.keepReplacement;
this.defaultRule = {

@@ -286,17 +290,3 @@ replacement: options.defaultReplacement

var keepRule = options.keepRule || {
filter: options.keep,
replacement: function (content, node) {
return node.isBlock ? '\n\n' + content + '\n\n' : content
}
};
var removeRule = options.removeRule || {
filter: options.remove,
replacement: function () {
return ''
}
};
this.array = [keepRule, removeRule];
this.array = [];
for (var key in options.rules) this.array.push(options.rules[key]);

@@ -310,9 +300,25 @@ }

keep: function (filter) {
this._keep.unshift({
filter: filter,
replacement: this.keepReplacement
});
},
remove: function (filter) {
this._remove.unshift({
filter: filter,
replacement: function () {
return ''
}
});
},
forNode: function (node) {
if (node.isBlank) return this.blankRule
var rule;
for (var i = 0; i < this.array.length; i++) {
var rule = this.array[i];
if (filterValue(rule, node, this.options)) return rule
}
if ((rule = findRule(this.array, node, this.options))) return rule
if ((rule = findRule(this._keep, node, this.options))) return rule
if ((rule = findRule(this._remove, node, this.options))) return rule

@@ -327,2 +333,10 @@ return this.defaultRule

function findRule (rules, node, options) {
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (filterValue(rule, node, options)) return rule
}
return void 0
}
function filterValue (rule, node, options) {

@@ -637,3 +651,10 @@ var filter = rule.filter;

if (typeof input === 'string') {
root = htmlParser().parseFromString(input, 'text/html').body;
var doc = htmlParser().parseFromString(
// DOM parsers arrange elements in the <head> and <body>.
// Wrapping in a custom element ensures elements are reliably arranged in
// a single element.
'<x-turndown id="turndown-root">' + input + '</x-turndown>',
'text/html'
);
root = doc.getElementById('turndown-root');
} else {

@@ -741,13 +762,5 @@ root = input.cloneNode(true);

},
keep: function (node) {
switch (node.nodeName) {
case 'TABLE':
return true
case 'PRE':
return node.firstChild && node.firstChild.nodeName !== 'CODE'
default:
return false
}
keepReplacement: function (content, node) {
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
},
remove: ['head', 'link', 'meta', 'script', 'style'],
defaultReplacement: function (content, node) {

@@ -817,2 +830,28 @@ return node.isBlock ? '\n\n' + content + '\n\n' : content

/**
* Keep a node (as HTML) that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
keep: function (filter) {
this.rules.keep(filter);
return this
},
/**
* Remove a node that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
remove: function (filter) {
this.rules.remove(filter);
return this
},
/**
* Escapes Markdown syntax

@@ -819,0 +858,0 @@ * @public

@@ -273,2 +273,4 @@ function extend (destination) {

this.options = options;
this._keep = [];
this._remove = [];

@@ -279,2 +281,4 @@ this.blankRule = {

this.keepReplacement = options.keepReplacement;
this.defaultRule = {

@@ -284,17 +288,3 @@ replacement: options.defaultReplacement

var keepRule = options.keepRule || {
filter: options.keep,
replacement: function (content, node) {
return node.isBlock ? '\n\n' + content + '\n\n' : content
}
};
var removeRule = options.removeRule || {
filter: options.remove,
replacement: function () {
return ''
}
};
this.array = [keepRule, removeRule];
this.array = [];
for (var key in options.rules) this.array.push(options.rules[key]);

@@ -308,9 +298,25 @@ }

keep: function (filter) {
this._keep.unshift({
filter: filter,
replacement: this.keepReplacement
});
},
remove: function (filter) {
this._remove.unshift({
filter: filter,
replacement: function () {
return ''
}
});
},
forNode: function (node) {
if (node.isBlank) return this.blankRule
var rule;
for (var i = 0; i < this.array.length; i++) {
var rule = this.array[i];
if (filterValue(rule, node, this.options)) return rule
}
if ((rule = findRule(this.array, node, this.options))) return rule
if ((rule = findRule(this._keep, node, this.options))) return rule
if ((rule = findRule(this._remove, node, this.options))) return rule

@@ -325,2 +331,10 @@ return this.defaultRule

function findRule (rules, node, options) {
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (filterValue(rule, node, options)) return rule
}
return void 0
}
function filterValue (rule, node, options) {

@@ -635,3 +649,10 @@ var filter = rule.filter;

if (typeof input === 'string') {
root = htmlParser().parseFromString(input, 'text/html').body;
var doc = htmlParser().parseFromString(
// DOM parsers arrange elements in the <head> and <body>.
// Wrapping in a custom element ensures elements are reliably arranged in
// a single element.
'<x-turndown id="turndown-root">' + input + '</x-turndown>',
'text/html'
);
root = doc.getElementById('turndown-root');
} else {

@@ -739,13 +760,5 @@ root = input.cloneNode(true);

},
keep: function (node) {
switch (node.nodeName) {
case 'TABLE':
return true
case 'PRE':
return node.firstChild && node.firstChild.nodeName !== 'CODE'
default:
return false
}
keepReplacement: function (content, node) {
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
},
remove: ['head', 'link', 'meta', 'script', 'style'],
defaultReplacement: function (content, node) {

@@ -815,2 +828,28 @@ return node.isBlock ? '\n\n' + content + '\n\n' : content

/**
* Keep a node (as HTML) that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
keep: function (filter) {
this.rules.keep(filter);
return this
},
/**
* Remove a node that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
remove: function (filter) {
this.rules.remove(filter);
return this
},
/**
* Escapes Markdown syntax

@@ -817,0 +856,0 @@ * @public

@@ -275,2 +275,4 @@ 'use strict';

this.options = options;
this._keep = [];
this._remove = [];

@@ -281,2 +283,4 @@ this.blankRule = {

this.keepReplacement = options.keepReplacement;
this.defaultRule = {

@@ -286,17 +290,3 @@ replacement: options.defaultReplacement

var keepRule = options.keepRule || {
filter: options.keep,
replacement: function (content, node) {
return node.isBlock ? '\n\n' + content + '\n\n' : content
}
};
var removeRule = options.removeRule || {
filter: options.remove,
replacement: function () {
return ''
}
};
this.array = [keepRule, removeRule];
this.array = [];
for (var key in options.rules) this.array.push(options.rules[key]);

@@ -310,9 +300,25 @@ }

keep: function (filter) {
this._keep.unshift({
filter: filter,
replacement: this.keepReplacement
});
},
remove: function (filter) {
this._remove.unshift({
filter: filter,
replacement: function () {
return ''
}
});
},
forNode: function (node) {
if (node.isBlank) return this.blankRule
var rule;
for (var i = 0; i < this.array.length; i++) {
var rule = this.array[i];
if (filterValue(rule, node, this.options)) return rule
}
if ((rule = findRule(this.array, node, this.options))) return rule
if ((rule = findRule(this._keep, node, this.options))) return rule
if ((rule = findRule(this._remove, node, this.options))) return rule

@@ -327,2 +333,10 @@ return this.defaultRule

function findRule (rules, node, options) {
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (filterValue(rule, node, options)) return rule
}
return void 0
}
function filterValue (rule, node, options) {

@@ -637,3 +651,10 @@ var filter = rule.filter;

if (typeof input === 'string') {
root = htmlParser().parseFromString(input, 'text/html').body;
var doc = htmlParser().parseFromString(
// DOM parsers arrange elements in the <head> and <body>.
// Wrapping in a custom element ensures elements are reliably arranged in
// a single element.
'<x-turndown id="turndown-root">' + input + '</x-turndown>',
'text/html'
);
root = doc.getElementById('turndown-root');
} else {

@@ -741,13 +762,5 @@ root = input.cloneNode(true);

},
keep: function (node) {
switch (node.nodeName) {
case 'TABLE':
return true
case 'PRE':
return node.firstChild && node.firstChild.nodeName !== 'CODE'
default:
return false
}
keepReplacement: function (content, node) {
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
},
remove: ['head', 'link', 'meta', 'script', 'style'],
defaultReplacement: function (content, node) {

@@ -817,2 +830,28 @@ return node.isBlock ? '\n\n' + content + '\n\n' : content

/**
* Keep a node (as HTML) that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
keep: function (filter) {
this.rules.keep(filter);
return this
},
/**
* Remove a node that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
remove: function (filter) {
this.rules.remove(filter);
return this
},
/**
* Escapes Markdown syntax

@@ -819,0 +858,0 @@ * @public

@@ -273,2 +273,4 @@ function extend (destination) {

this.options = options;
this._keep = [];
this._remove = [];

@@ -279,2 +281,4 @@ this.blankRule = {

this.keepReplacement = options.keepReplacement;
this.defaultRule = {

@@ -284,17 +288,3 @@ replacement: options.defaultReplacement

var keepRule = options.keepRule || {
filter: options.keep,
replacement: function (content, node) {
return node.isBlock ? '\n\n' + content + '\n\n' : content
}
};
var removeRule = options.removeRule || {
filter: options.remove,
replacement: function () {
return ''
}
};
this.array = [keepRule, removeRule];
this.array = [];
for (var key in options.rules) this.array.push(options.rules[key]);

@@ -308,9 +298,25 @@ }

keep: function (filter) {
this._keep.unshift({
filter: filter,
replacement: this.keepReplacement
});
},
remove: function (filter) {
this._remove.unshift({
filter: filter,
replacement: function () {
return ''
}
});
},
forNode: function (node) {
if (node.isBlank) return this.blankRule
var rule;
for (var i = 0; i < this.array.length; i++) {
var rule = this.array[i];
if (filterValue(rule, node, this.options)) return rule
}
if ((rule = findRule(this.array, node, this.options))) return rule
if ((rule = findRule(this._keep, node, this.options))) return rule
if ((rule = findRule(this._remove, node, this.options))) return rule

@@ -325,2 +331,10 @@ return this.defaultRule

function findRule (rules, node, options) {
for (var i = 0; i < rules.length; i++) {
var rule = rules[i];
if (filterValue(rule, node, options)) return rule
}
return void 0
}
function filterValue (rule, node, options) {

@@ -635,3 +649,10 @@ var filter = rule.filter;

if (typeof input === 'string') {
root = htmlParser().parseFromString(input, 'text/html').body;
var doc = htmlParser().parseFromString(
// DOM parsers arrange elements in the <head> and <body>.
// Wrapping in a custom element ensures elements are reliably arranged in
// a single element.
'<x-turndown id="turndown-root">' + input + '</x-turndown>',
'text/html'
);
root = doc.getElementById('turndown-root');
} else {

@@ -739,13 +760,5 @@ root = input.cloneNode(true);

},
keep: function (node) {
switch (node.nodeName) {
case 'TABLE':
return true
case 'PRE':
return node.firstChild && node.firstChild.nodeName !== 'CODE'
default:
return false
}
keepReplacement: function (content, node) {
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
},
remove: ['head', 'link', 'meta', 'script', 'style'],
defaultReplacement: function (content, node) {

@@ -815,2 +828,28 @@ return node.isBlock ? '\n\n' + content + '\n\n' : content

/**
* Keep a node (as HTML) that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
keep: function (filter) {
this.rules.keep(filter);
return this
},
/**
* Remove a node that matches the filter
* @public
* @param {String|Array|Function} filter The unique key of the rule
* @returns The Turndown instance for chaining
* @type Object
*/
remove: function (filter) {
this.rules.remove(filter);
return this
},
/**
* Escapes Markdown syntax

@@ -817,0 +856,0 @@ * @public

{
"name": "turndown",
"description": "A library that converts HTML to Markdown",
"version": "4.0.0-rc.2",
"version": "4.0.0-rc.3",
"author": "Dom Christie",

@@ -6,0 +6,0 @@ "browser": {

@@ -54,7 +54,4 @@ # Turndown

| `blankReplacement` | rule replacement function | See **Special Rules** below |
| `keep` | rule filter | See **Special Rules** below |
| `remove` | rule filter | See **Special Rules** below |
| `keepReplacement` | rule replacement function | See **Special Rules** below |
| `defaultReplacement` | rule replacement function | See **Special Rules** below |
| `keepRule` | rule | See **Special Rules** below |
| `removeRule` | rule | See **Special Rules** below |

@@ -76,4 +73,36 @@ ## Methods

`addRule` returns the `TurndownService` instance for chaining.
See **Extending with Rules** below.
### `keep(filter)`
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:
```js
turndownService.keep(['del', 'ins'])
turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>') // 'Hello <del>world</del><ins>World</ins>'
```
This will render `<del>` and `<ins>` elements as HTML when converted.
`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.
`keep` returns the `TurndownService` instance for chaining.
### `remove(filter)`
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:
```js
turndownService.remove('del')
turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>') // 'Hello World'
```
This will remove `<del>` elements (and contents).
`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.
`remove` returns the `TurndownService` instance for chaining.
### `use(plugin|array)`

@@ -97,2 +126,4 @@

`use` returns the `TurndownService` instance for chaining.
See **Plugins** below.

@@ -154,5 +185,5 @@

**Keep rule** determines how to handle the elements that should not be converted, i.e. rendered as HTML in the Markdown output. By default, it will keep `<table>` and plain `<pre>` elements. Block-level elements will be separated from surrounding content by blank lines. The keep rule `filter` can be customised using the `keep` option. To replace the rule, use the `keepRule` option. The keep rule may be overridden by rules added via `addRule`.
**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.
**Remove rule** determines which elements to remove altogether. By default, it removes `<head>`, `<link>`, `<meta>`, `<script>`, and `<style>` elements, and replaces them with an empty string. Like the keep rule, its `filter` can be customised using the `remove` option. To replace the rule, use the `removeRule` option. The remove rule may be overridden by rules added via `addRule`.
**Remove rules** determine which elements to remove altogether. By default, no elements are removed.

@@ -167,5 +198,5 @@ **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.

2. Added rules (optional)
3. Keep rule
4. Remove rule
5. Commonmark rules
3. Commonmark rules
4. Keep rules
5. Remove rules
6. Default rule

@@ -172,0 +203,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc