diff --git a/apps/website/static/llms-full.txt b/apps/website/static/llms-full.txt new file mode 100644 index 00000000..49d40925 --- /dev/null +++ b/apps/website/static/llms-full.txt @@ -0,0 +1,7410 @@ +# Overview and Getting Started + +## What it is + +`@native-html/render` is an iOS/Android pure-JavaScript React Native component that translates HTML markup into 100% native React Native views (no WebView). It balances HTML/CSS standard compliance with lightweightness and is designed to be highly customizable: you can define custom renderers for specific tags (with control over children rendering), provide styles targeting tags, classes and ids, and tamper with the DOM. The best use cases are rendering predictable content where you know the tags and classes in advance — for example CMS output, API-delivered articles, or sections of a web page. It is not a web engine: it does not execute JavaScript, and it is not the right tool for arbitrary, unpredictable web content. If you need full web semantics, use `react-native-webview` instead (at the cost of performance, since it uses the system Web View). + +The package was previously published as `react-native-render-html`. The v1 release of `@native-html/native` follows exactly the same API, so migrating is plug-and-play. + +## Should I use it? + +Yes, if: + +- You want HTML translated into React Native views (see the Architecture page). +- You want a design that balances HTML/CSS compliance with lightweightness. +- You want extensive customization: custom renderers per tag, styling by tag/class/id, DOM tampering. + +No, if: + +- Your use case is very simple — you may prefer rolling a tiny renderer of your own (see "Reinvent the Wheel" below). +- You need a full web engine with JavaScript execution and full Web standards support — use `react-native-webview` instead. + +Best use cases: + +- Predictable content where you know the tags and classes in advance (CMS, API endpoints). +- Rendering article-like sections of a web page (see Root Selection under DOM tampering for selecting a sub-tree). +- Not recommended for arbitrary, unpredictable content. + +## Install + +```bash +npm install @native-html/render +``` + +```bash +yarn add @native-html/render +``` + +The Foundry release is v6: + +```bash +npm install --save-prod @native-html/render +``` + +## Minimal usage + +```jsx +import React from 'react'; +import { useWindowDimensions } from 'react-native'; +import RenderHtml from '@native-html/render'; + +const source = { + html: ` +

+ Hello World! +

` +}; + +export default function App() { + const { width } = useWindowDimensions(); + return ( + + ); +} +``` + +The `source` prop specifies the HTML content to load. It also supports a `uri` field for remote loading and a `dom` field for asynchronous DOM pre-processing (see Prerendering under DOM tampering). + +> Important: The `contentWidth` prop allows proper image scaling. See the Images page for details. + +> Tip: You can inspect the pre-render tree representation (Transient Render Tree) for any input — useful when authoring custom renderers or debugging layout. + +The pre-render tree for the example above looks like: + +``` + + + + + + + + + +``` + +## Core libraries (monorepo packages) + +- `@native-html/css-processor` — CSS processing pipeline. +- `@native-html/transient-render-engine` — engine that builds the Transient Render Tree (TRT) from parsed HTML. + +## Reinvent the Wheel: a 40-line HTML renderer + +To understand how `@native-html/render` works, here is a toy HTML renderer in about 40 lines. It has many limitations the real library overcomes, but it shows the core idea: parse HTML to a DOM-like tree, then recursively map nodes to React Native components. + +It uses `htmlparser2` to produce a proxy DOM representation. + +```jsx title="RenderHtml.jsx" +import {Text, View} from 'react-native'; +import {parseDocument, ElementType} from 'htmlparser2'; +import React, {PureComponent} from 'react'; + +export default class RenderHtml extends PureComponent { + ignoredTags = ['head']; + textTags = ['span', 'strong', 'em']; + + renderTextNode(textNode, index) { + return {textNode.data}; + } + + renderElement(element, index) { + if (this.ignoredTags.indexOf(element.name) > -1) { + return null; + } + const Wrapper = this.textTags.indexOf(element.name) > -1 ? Text : View; + return ( + + {element.children.map((c, i) => this.renderNode(c, i))} + + ); + } + + renderNode(node, index) { + switch (node.type) { + case ElementType.Text: + return this.renderTextNode(node, index); + case ElementType.Tag: + return this.renderElement(node, index); + } + return null; + } + + render() { + const document = parseDocument(this.props.html); + return document.children.map((c, i) => this.renderNode(c, i)); + } +} +``` + +What `render` does: + +1. `parseDocument` returns the root DOM node of the document. +2. Map the root's children through `renderNode`. +3. `renderNode` dispatches: text nodes go to `renderTextNode`, element nodes to `renderElement`, everything else (comments, scripts, stylesheets) returns `null`. + +`renderTextNode` is trivial. `renderElement` chooses between a React Native `Text` and `View` wrapper depending on whether the tag is in `textTags`. This is to avoid rendering glitches when embedding `View` inside `Text` — addressed properly via hoisting in the full library. + +> Note: We refer to "the DOM" and "DOM nodes" even though `htmlparser2` only exposes a subset of the DOM API for lightweightness. + +### When the toy is enough — and when it isn't + +You can extend the toy with relatively easy additions: + +1. Custom renderers for specific tags such as ``, `