Files
bun.sh/docs/api/html-rewriter.md
Colin McDonnell f54300578b Add documentation (#2148)
* Add documentation

* Tweaks

* Fixes

* Rearrange

* Update
2023-02-23 17:13:30 -08:00

736 B

Bun provides a fast native implementation of the HTMLRewriter pattern developed by Cloudflare. It provides a convenient, EventListener-like API for traversing and transforming HTML documents.

const rewriter = new HTMLRewriter();

rewriter.on("*", {
  element(el) {
    console.log(el.tagName); // "body" | "div" | ...
  },
});

To parse and/or transform the HTML:

rewriter.transform(
  new Response(`
<!DOCTYPE html>
<html>
<!-- comment -->
<head>
  <title>My First HTML Page</title>
</head>
<body>
  <h1>My First Heading</h1>
  <p>My first paragraph.</p>
</body>
`));

View the full documentation on the Cloudflare website.