html2any
Parse HTML/XML, then shape it into anything.
Browser
Parse and render html website.
Usage
Turn parsed HTML into the output shape you need.
import html2any from 'html2any'
const html = `<div>
<h1>Getting Started</h1>
<p>Welcome to html2any documentation!</p>
<h3>Install</h3>
<p>html2any is a dependency free library can be used in any JS runtime.</p>
<pre><code>npm install --save html2any</code></pre>
<h3>Usage</h3>
<p>Map each parsed node to whatever output shape your app needs.</p>
</div>`
function rule(node, children, index) {
if (typeof node === 'string') {
return node
}
const Tag = node.name
const key = typeof node.index === 'number' ? node.index : undefined
if (['h1', 'h2', 'h3'].includes(Tag)) {
return <Tag key={key} className="title">{children}</Tag>
}
if (Tag === 'pre') {
return <pre key={key} className="pre">{children}</pre>
}
return <Tag key={key}>{children}</Tag>
}
const content = html2any(html, rule)