Skip to main content

Overview

The replacement property is a function that defines how a matched HTML element should be converted to Markdown. It receives information about the element and returns the Markdown string.

Function Signature

Returns: string - The Markdown representation of the element

Simple Replacements

Paragraph

Wraps content with blank lines:
Input: <p>Hello world</p> Output:

Blockquote

Prefixes each line with >:
Input: <blockquote>Hello\nWorld</blockquote> Output:

Horizontal Rule

Outputs the configured horizontal rule:
Uses the hr option (default: '* * *').

Replacements Using Options

Line Break

Uses the configured line break style:
The br option determines the line break format (default: ' ' - two spaces).

Emphasis

Wraps content with the configured emphasis delimiter:
Uses the emDelimiter option (default: '_'). Returns empty string if content is blank. Input: <em>italic</em> Output: _italic_

Strong

Wraps content with the configured strong delimiter:
Uses the strongDelimiter option (default: '**'). Input: <strong>bold</strong> Output: **bold**

Replacements Using Node Properties

Heading

Extracts heading level from the node name:
ATX style (h2):
Setext style (h1):

List Item

Checks parent element and position:
This replacement:
  1. Determines if the list is ordered or unordered
  2. Calculates the appropriate prefix (bullet or number)
  3. Indents multiline content
  4. Adds newlines between items

Image

Extracts attributes from the node:
Input: <img src="logo.png" alt="Logo" title="Company Logo"> Output: ![Logo](logo.png "Company Logo") Extracts and escapes link attributes:
Input: <a href="https://example.com" title="Example">Link</a> Output: [Link](https://example.com "Example")

Complex Replacements

List

Contextual formatting based on parent:
Nested lists within list items are formatted differently than top-level lists.

Inline Code

Dynamically determines delimiter:
This replacement:
  1. Replaces newlines with spaces
  2. Determines if extra spaces are needed
  3. Finds a delimiter that doesn’t conflict with backticks in the content
  4. Wraps the content appropriately
Input: <code>`code`</code> Output: `` `code` ``

Fenced Code Block

Extracts language and adapts fence size:
This replacement:
  1. Extracts the language from the class name
  2. Gets the raw code text
  3. Determines the minimum fence size to avoid conflicts
  4. Builds the fenced code block
Input:
Output:
Collects references for later output:
This replacement uses the rule’s references array to collect link definitions, which are output at the end via the append function. Input: <a href="https://example.com">Link</a> Output (in content): [Link][1] Output (appended):

Indented Code Block

Directly accesses text content:
Note: Uses node.firstChild.textContent instead of content to get the raw code without markdown conversion.

Best Practices

Return Empty String for Empty Content

Use Consistent Spacing

Block-level elements typically use \n\n before and after:

Escape Special Characters

When outputting user content, escape characters that have special meaning:

Access Raw Text When Needed

For code blocks, use textContent instead of the converted content:
This preserves HTML entities and special characters without markdown conversion.