![Introducing Enhanced Alert Actions and Triage Functionality](https://cdn.sanity.io/images/cgdhsj6q/production/fe71306d515f85de6139b46745ea7180362324f0-2530x946.png?w=800&fit=max&auto=format)
Product
Introducing Enhanced Alert Actions and Triage Functionality
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
turndown
Advanced tools
Readme
Convert HTML into Markdown with JavaScript.
npm:
npm install turndown
Browser:
<script src="https://unpkg.com/turndown/dist/turndown.js"></script>
// For Node.js
var TurndownService = require('turndown')
var turndownService = new TurndownService()
var markdown = turndownService.turndown('<h1>Hello world!</h1>')
Options can be passed in to the constructor on instantiation.
Option | Valid values | Default |
---|---|---|
headingStyle | setext or atx | setext |
hr | Any Thematic break | * * * |
bulletListMarker | - , + , or * | * |
codeBlockStyle | indented or fenced | indented |
fence | ``` or ~~~ | ``` |
emDelimiter | _ or * | _ |
strongDelimiter | ** or __ | ** |
linkStyle | inlined or referenced | inlined |
linkReferenceStyle | full , collapsed , or shortcut | full |
Option | Valid values | Default |
---|---|---|
blankReplacement | rule replacement function | See Special Rules below |
keep | rule filter | See Special Rules below |
remove | rule filter | See Special Rules below |
defaultReplacement | rule replacement function | See Special Rules below |
keepRule | rule | See Special Rules below |
removeRule | rule | See Special Rules below |
addRule(key, rule)
The key
parameter is a unique name for the rule for easy reference. Example:
turndownService.addRule('strikethrough', {
filter: ['del', 's', 'strike'],
replacement: function (content) {
return '~' + content + '~'
}
})
See Extending with Rules below.
use(plugin|array)
Use a plugin, or an array of plugins. Example:
// Import plugins from turndown-plugin-gfm
var turndownPluginGfm = require('turndown-plugin-gfm')
var gfm = turndownPluginGfm.gfm
var tables = gfm.tables
var strikethrough = gfm.strikethrough
// Use the gfm plugin
turndownService.use(gfm)
// Use the table and strikethrough plugins only
turndownService.use([tables, strikethrough])
See Plugins below.
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:
{
filter: 'p',
replacement: function (content) {
return '\n\n' + content + '\n\n'
}
}
The filter selects <p>
elements, and the replacement function returns the <p>
contents separated by two new lines.
filter
String|Array|FunctionThe 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:
filter: 'p'
will select <p>
elementsfilter: ['em', 'i']
will select <em>
or <i>
elementsAlternatively, 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
:
filter: function (node, options) {
return (
options.linkStyle === 'inlined' &&
node.nodeName === 'A' &&
node.getAttribute('href')
)
}
replacement
FunctionThe 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.
The following rule shows how <em>
elements are converted:
rules.emphasis = {
filter: ['em', 'i'],
replacement: function (content, node, options) {
return options.emDelimiter + content + options.emDelimiter
}
}
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.
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
.
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
.
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.
Turndown iterates over the set of rules, and picks the first one that matches satifies the filter
. The following list describes the order of precedence:
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.
turndown is copyright © 2017+ Dom Christie and released under the MIT license.
FAQs
A library that converts HTML to Markdown
The npm package turndown receives a total of 554,901 weekly downloads. As such, turndown popularity was classified as popular.
We found that turndown demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports four distinct alert actions instead of the previous two, and alert triaging allows users to override the actions taken for all individual alerts.
Security News
Polyfill.io has been serving malware for months via its CDN, after the project's open source maintainer sold the service to a company based in China.
Security News
OpenSSF is warning open source maintainers to stay vigilant against reputation farming on GitHub, where users artificially inflate their status by manipulating interactions on closed issues and PRs.