HTML to Markdown Converter

2.0.0 · active · verified Sun Apr 19

node-html-markdown is a high-performance, cross-platform library designed for converting HTML content into Markdown, compatible with both Node.js and browser environments. The current stable version is 2.0.0. The library's primary focus is on speed, with benchmarks indicating significantly faster conversion rates compared to popular alternatives like Turndown. Additionally, it emphasizes generating human-readable Markdown output by producing clean, concise results with consistent spacing, aiming to avoid common formatting issues such as excessive line breaks. While it does not adhere to a rigid release schedule, the project is actively maintained, with regular updates that have historically included features like table support and fixes for browser compatibility, ensuring its reliability for converting large volumes of HTML data.

Warnings

Install

Imports

Quickstart

Demonstrates initializing a reusable instance of `NodeHtmlMarkdown` with custom options and using it to convert both single and multiple HTML strings to Markdown efficiently.

import { NodeHtmlMarkdown } from 'node-html-markdown';

// Initialize with options for a reusable instance
// Custom options can improve output or handle specific HTML structures.
const nhm = new NodeHtmlMarkdown(
  {
    codeFence: '```', // Customize code block fence (default: ```)
    bulletMarker: '-', // Use hyphens for list bullets (default: *)
    codeBlockStyle: 'fenced', // Prefer fenced code blocks (default: fenced)
    preferNativeParser: typeof window !== 'undefined' // Use native DOMParser in browsers if available
  },
  /* customTranslators (optional) */ undefined,
  /* customCodeBlockTranslators (optional) */ undefined
);

// Example HTML input to be converted
const htmlContent = `
  <h1>Welcome to my document</h1>
  <p>This is a <b>paragraph</b> with <i>some emphasis</i> and a <s>strikethrough</s> word.</p>
  <ul>
    <li>First item in a list.</li>
    <li>Second item.</li>
  </ul>
  <pre><code>function greet() {
  console.log('Hello, world!');
}
greet();</code></pre>
  <p>Check out our <a href="https://example.com/docs">documentation</a>.</p>
  <table><thead><tr><th>Header 1</th><th>Header 2</th></tr></thead><tbody><tr><td>Data 1</td><td>Data 2</td></tr></tbody></table>
`;

// Convert a single HTML string to Markdown
const markdownOutput = nhm.translate(htmlContent);
console.log('Converted Markdown Output:\n');
console.log(markdownOutput);

// The instance can be reused for multiple conversions efficiently
const anotherHtml = '<div><p>Another piece of HTML.</p></div>';
const anotherMarkdown = nhm.translate(anotherHtml);
console.log('\nAnother Converted Markdown:\n');
console.log(anotherMarkdown);

view raw JSON →