HAST to MDAST Transformer Utility

10.1.2 · active · verified Sun Apr 19

hast-util-to-mdast is a core utility within the unified.js ecosystem designed to transform a HAST (HTML Abstract Syntax Tree) into an MDAST (Markdown Abstract Syntax Tree). This package is essential for converting HTML content into Markdown programmatically, often serving as the engine behind higher-level plugins like `rehype-remark`. The current stable version is 10.1.2. The package maintains an active release cadence, with frequent patch and minor updates addressing bug fixes and feature enhancements, alongside major versions that typically align with Node.js LTS updates or significant API refinements. Key differentiators include its tight integration with the unified ecosystem, robust handling of various HTML structures, and the provision of a flexible API with custom handlers for fine-grained control over the transformation process, allowing developers to define how specific HTML elements or nodes should be represented in Markdown.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to convert an HTML string to Markdown using `hast-util-from-html`, `hast-util-to-mdast`, and `mdast-util-to-markdown`.

import fs from 'node:fs/promises';
import { fromHtml } from 'hast-util-from-html';
import { toMdast } from 'hast-util-to-mdast';
import { toMarkdown } from 'mdast-util-to-markdown';

async function convertHtmlToMarkdown(htmlString: string): Promise<string> {
  // Parse the HTML string into a HAST (HTML Abstract Syntax Tree) fragment.
  const hast = fromHtml(htmlString, { fragment: true });

  // Transform the HAST tree into an MDAST (Markdown Abstract Syntax Tree).
  const mdast = toMdast(hast);

  // Serialize the MDAST tree back into a Markdown string.
  const markdown = toMarkdown(mdast);

  return markdown;
}

// Example usage:
const exampleHtml = '<h2>Hello <strong>world!</strong></h2><p>This is a paragraph with a <a href="#">link</a> and a <br>line break.</p>';

convertHtmlToMarkdown(exampleHtml)
  .then(markdown => {
    console.log('Original HTML:\n', exampleHtml);
    console.log('\nConverted Markdown:\n', markdown);
  })
  .catch(error => {
    console.error('Conversion failed:', error);
  });

// To show an actual file read (requires 'example.html' file):
// const htmlFilePath = './example.html';
// try {
//   const fileHtml = await fs.readFile(htmlFilePath, 'utf8');
//   const convertedFileMarkdown = await convertHtmlToMarkdown(fileHtml);
//   console.log(`\nMarkdown from ${htmlFilePath}:\n`, convertedFileMarkdown);
// } catch (err) {
//   console.error(`Could not read file ${htmlFilePath}:`, err);
// }

view raw JSON →