{"id":13475215,"url":"https://github.com/mikenikles/html-to-react","last_synced_at":"2025-03-26T23:30:36.661Z","repository":{"id":34003602,"uuid":"37758788","full_name":"mikenikles/html-to-react","owner":"mikenikles","description":"A lightweight library that converts raw HTML to a React DOM structure.","archived":true,"fork":false,"pushed_at":"2024-08-12T06:27:00.000Z","size":91,"stargazers_count":190,"open_issues_count":5,"forks_count":115,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-30T08:51:27.484Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikenikles.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-06-20T05:20:03.000Z","updated_at":"2023-12-22T06:19:16.000Z","dependencies_parsed_at":"2024-10-30T06:41:53.397Z","dependency_job_id":null,"html_url":"https://github.com/mikenikles/html-to-react","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikenikles%2Fhtml-to-react","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikenikles%2Fhtml-to-react/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikenikles%2Fhtml-to-react/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikenikles%2Fhtml-to-react/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikenikles","download_url":"https://codeload.github.com/mikenikles/html-to-react/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245753819,"owners_count":20666818,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T16:01:18.398Z","updated_at":"2025-03-26T23:30:36.367Z","avatar_url":"https://github.com/mikenikles.png","language":"JavaScript","readme":"# html-to-react [![Build Status](https://travis-ci.org/mikenikles/html-to-react.svg?branch=master)](https://travis-ci.org/mikenikles/html-to-react) [![npm version](https://badge.fury.io/js/html-to-react.svg)](http://badge.fury.io/js/html-to-react) [![Dependency Status](https://david-dm.org/mikenikles/html-to-react.svg)](https://david-dm.org/mikenikles/html-to-react) [![Coverage Status](https://coveralls.io/repos/mikenikles/html-to-react/badge.svg?branch=master)](https://coveralls.io/r/mikenikles/html-to-react?branch=master)\nA lightweight library that converts raw HTML to a React DOM structure.\n\n## Project Moved\n\nAs part of [#43](https://github.com/mikenikles/html-to-react/issues/43), this project moved to https://github.com/aknuds1/html-to-react. Please file any issues or PRs at the new location.\n\n## Why?\nI had a scenario where an HTML template was generated by a different team, yet I wanted to leverage React for the parts\nI did have control over. The template basically contains something like:\n\n```\n\u003cdiv class=\"row\"\u003e\n    \u003cdiv class=\"col-sm-6\"\u003e\n        \u003cdiv data-report-id=\"report-1\"\u003e\n          \u003c!-- A React component for report-1 --\u003e\n        \u003c/div\u003e\n    \u003c/div\u003e\n    \u003cdiv class=\"col-sm-6\"\u003e\n        \u003cdiv data-report-id=\"report-2\"\u003e\n          \u003c!-- A React component for report-2 --\u003e\n        \u003c/div\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n```\n\nI had to replace each `\u003cdiv\u003e` that contains a `data-report-id` attribute with an actual report, which was nothing more\nthan a React component.\n\nSimply replacing the `\u003cdiv\u003e` elements with a React component would end up with multiple top-level React components\nthat have no common parent.\n\nThe **html-to-react** module solves this problem by parsing each DOM element and converting it to a React tree with one\nsingle parent.\n\n## Installation\n\n`$ npm install --save html-to-react`\n\n## Examples\n\n### Simple\n\nThe following example parses each node and its attributes and returns a tree of React components.\n\n```javascript\nvar React = require('react');\nvar HtmlToReact = new require('html-to-react');\n\nvar htmlInput = '\u003cdiv\u003e\u003ch1\u003eTitle\u003c/h1\u003e\u003cp\u003eA paragraph\u003c/p\u003e\u003c/div\u003e';\nvar htmlToReactParser = new HtmlToReact.Parser(React);\nvar reactComponent = htmlToReactParser.parse(htmlInput);\nvar reactHtml = React.renderToStaticMarkup(reactComponent);\n\nassert.equal(reactHtml, htmlInput); // true\n```\n\n### With custom processing instructions\n\nIf certain DOM nodes require specific processing, for example if you want to capitalize each `\u003ch1\u003e` tag, the following\nexample demonstrates this:\n\n```javascript\nvar React = require('react');\nvar HtmlToReact = new require('html-to-react');\n\nvar htmlInput = '\u003cdiv\u003e\u003ch1\u003eTitle\u003c/h1\u003e\u003cp\u003eParagraph\u003c/p\u003e\u003ch1\u003eAnother title\u003c/h1\u003e\u003c/div\u003e';\nvar htmlExpected = '\u003cdiv\u003e\u003ch1\u003eTITLE\u003c/h1\u003e\u003cp\u003eParagraph\u003c/p\u003e\u003ch1\u003eANOTHER TITLE\u003c/h1\u003e\u003c/div\u003e';\n\nvar isValidNode = function() {\n    return true;\n};\n\n// Order matters. Instructions are processed in the order they're defined\nvar processNodeDefinitions = new HtmlToReact.ProcessNodeDefinitions(React);\nvar processingInstructions = [\n    {\n        // Custom \u003ch1\u003e processing\n        shouldProcessNode: function(node) {\n            return node.parent \u0026\u0026 node.parent.name \u0026\u0026 node.parent.name === 'h1';\n        },\n        processNode: function(node, children) {\n            return node.data.toUpperCase();\n        }\n    }, {\n        // Anything else\n        shouldProcessNode: function(node) {\n            return true;\n        },\n        processNode: processNodeDefinitions.processDefaultNode\n    }];\nvar htmlToReactParser = new HtmlToReact.Parser(React);\nvar reactComponent = htmlToReactParser.parseWithInstructions(htmlInput, isValidNode, processingInstructions);\nvar reactHtml = React.renderToStaticMarkup(reactComponent);\nassert.equal(reactHtml, htmlExpected);\n```\n\n## Tests \u0026 Coverage\n\n`$ npm run test-locally`\n\n`$ npm run test-html-coverage`\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikenikles%2Fhtml-to-react","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikenikles%2Fhtml-to-react","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikenikles%2Fhtml-to-react/lists"}