HTML to React Parser

1.7.0 · maintenance · verified Sun Apr 19

html-to-react is a lightweight JavaScript library designed to convert raw HTML strings into a React DOM structure. Released last as version 1.7.0 approximately six years ago, it primarily serves applications built with older React versions, evidenced by its peer dependency on `react@^0.13.0 || ^0.14.0 || >=15`. While not actively maintained for recent React releases, it provides a solution for embedding React components within existing HTML templates, specifically addressing scenarios where a page includes HTML generated externally but requires certain sections to be managed by React. This avoids the common issue of creating multiple top-level React roots, instead parsing the entire HTML into a single React element tree. Key features include the ability to define custom processing instructions for specific DOM nodes, allowing for fine-grained control over how HTML elements are transformed into React components.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to parse an HTML string, apply custom processing instructions (specifically capitalizing the content of H1 tags), and then render the resulting React component tree back to a static HTML string. It highlights the use of `Parser` and `ProcessNodeDefinitions` with TypeScript typings.

import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { Parser, ProcessNodeDefinitions } from 'html-to-react';

interface CustomNode extends Node {
  data?: string;
  name?: string;
  parent?: CustomNode; // Add parent for 'shouldProcessNode' logic
}

const htmlInput = '<div><h1>Title</h1><p>Paragraph</p><h1>Another title</h1></div>';
const htmlExpected = '<div><h1>TITLE</h1><p>Paragraph</p><h1>ANOTHER TITLE</h1></div>';

// A function to determine if a node should be processed
const isValidNode = (node: CustomNode) => true;

// Order of instructions matters: most specific to most general
const processNodeDefinitions = new ProcessNodeDefinitions();
const processingInstructions = [
  {
    // Custom processing for <h1> tags: capitalize their content
    shouldProcessNode: (node: CustomNode) => {
      // Check if the node is a child of an <h1> tag and contains text data
      return node.parent && node.parent.name === 'h1' && node.type === 'text';
    },
    processNode: (node: CustomNode) => {
      return node.data?.toUpperCase();
    }
  },
  {
    // Default processing for all other nodes that haven't been handled
    shouldProcessNode: (node: CustomNode) => true,
    processNode: processNodeDefinitions.processDefaultNode
  }
];

const htmlToReactParser = new Parser();
const reactComponent = htmlToReactParser.parseWithInstructions(
  htmlInput,
  isValidNode,
  processingInstructions
);

// Render the resulting React component tree back to static HTML
const reactHtml = ReactDOMServer.renderToStaticMarkup(reactComponent);

console.log('Original HTML Input:', htmlInput);
console.log('Processed React HTML Output:', reactHtml);
console.log('Assertion (output matches expected):', reactHtml === htmlExpected);

view raw JSON →