Turndown

repository·master·Indexed 11 days ago

https://github.com/mixmark-io/turndown

A JavaScript library that converts HTML to Markdown. Version 7.2.4 features a rule-based system and a plugin API for extensibility, allowing developers to customize output via TurndownService, add custom rules, keep specific elements as HTML, or remove elements entirely.

Tokens
1.9K
Snippets
10
Records
11
Agent score
45%

What's inside Turndown

  1. How rules work in Turndown

    master

    Turndown uses rules to transform HTML elements into Markdown. A rule consists of two parts:

    1. filter: Determines which elements are selected. It can be a string (tag name), an array of strings, or a function that returns a boolean based on the node and current options.
    2. replacement: A function that determines the Markdown output. It receives the node's content, the node itself, and the options object.

    Rule Precedence

    Turndown picks the first matching rule based on this order:

    1. Blank rule
    2. Added rules (via addRule)
    3. Commonmark rules
    4. Keep rules
    5. Remove rules
    6. Default rule
    // Example of a custom rule
    {
      filter: ['em', 'i'],
      replacement: function (content, node, options) {
        return options.emDelimiter + content + options.emDelimiter
      }
    }
  2. Basic Usage of TurndownService

    master

    To convert HTML to Markdown, instantiate TurndownService and call the .turndown() method. You can pass an HTML string or a DOM node (element, document, or document fragment) as input.

    // For Node.js
    var TurndownService = require('turndown')
    
    var turndownService = new TurndownService()
    var markdown = turndownService.turndown('<h1>Hello world!</h1>')
    
    // Using a DOM node
    var markdown = turndownService.turndown(document.getElementById('content'))
  3. Keep elements as HTML with `keep(filter)`

    master

    Use keep(filter) to specify which elements should be rendered as raw HTML in the Markdown output instead of being converted. The filter parameter works like a rule filter. keep returns the TurndownService instance for chaining. Note that keep filters can be overridden by standard CommonMark rules or custom added rules.

    turndownService.keep(['del', 'ins'])
    turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>') // 'Hello <del>world</del><ins>World</ins>'
  4. Add a custom rule with `addRule(key, rule)`

    master

    Use addRule to extend Turndown's behavior. A rule is an object containing a filter (to select elements) and a replacement function (to define the Markdown output). addRule returns the TurndownService instance for chaining.

    turndownService.addRule('strikethrough', {
      filter: ['del', 's', 'strike'],
      replacement: function (content) {
        return '~' + content + '~'
      }
    })
  5. Customizing escaping behavior

    master
    Turndown escapes Markdown characters in HTML input using regular expressions to ensure they aren't misinterpreted when converted back to HTML. If you need to customize this, you can override TurndownService.prototype.escape. The escape method takes the text of an element and should return a version with Markdown characters escaped. Note that text inside code elements is never passed to escape.
  6. Remove elements with `remove(filter)`

    master

    Use remove(filter) to specify elements that should be completely removed (converted to an empty string) from the output. The filter parameter works like a rule filter. remove returns the TurndownService instance for chaining. Note that remove filters can be overridden by keep filters, standard CommonMark rules, or custom added rules.

    turndownService.remove('del')
    turndownService.turndown('<p>Hello <del>world</del><ins>World</ins></p>') // 'Hello World'
  7. Use plugins with `use(plugin|array)`

    master

    A plugin is a function that is called with the TurndownService instance. You can apply one or multiple plugins using the use method. use returns the TurndownService instance for chaining.

    // Import plugins from turndown-plugin-gfm
    var turndownPluginGfm = require('turndown-plugin-gfm')
    var gfm = turndownPluginGfm.gfm
    var tables = turndownPluginGfm.tables
    var strikethrough = turndownPluginGfm.strikethrough
    
    // Use the gfm plugin
    turndownService.use(gfm)
    
    // Use the table and strikethrough plugins only
    turndownService.use([tables, strikethrough])
  8. Reference: Turndown configuration options

    master

    Standard configuration options for TurndownService.

    | Option | Valid values | Default |
    | :--- | :--- | :--- |
    | `headingStyle` | `setext` or `atx` | `setext` |
    | `hr` | Any [Thematic break](http://spec.commonmark.org/0.27/#thematic-breaks) | `* * *` |
    | `bulletListMarker` | `-`, `+`, or `*` | `*` |
    | `codeBlockStyle` | `indented` or `fenced` | `indented` |
    | `fence` | ` ``` ` or `~~~` | ` ``` ` |
    | `emDelimiter` | `_` or `*` | `_` |
    | `strongDelimiter` | `**` or `__` | `**` |
    | `linkStyle` | `inlined` or `referenced` | `inlined` |
    | `linkReferenceStyle` | `full`, `collapsed`, or `shortcut` | `full` |
    | `preformattedCode` | `false` or `true` | `false` |
  9. Reference: Advanced Turndown options

    master

    Advanced configuration options that allow overriding rule replacement logic.

    | Option | Valid values | Default |
    | :--- | :--- | :--- |
    | `blankReplacement` | rule replacement function | See **Special Rules** below |
    | `keepReplacement` | rule replacement function | See **Special Rules** below |
    | `defaultReplacement` | rule replacement function | See **Special Rules** below |