aboutsummaryrefslogtreecommitdiff
path: root/docs/api/html-rewriter.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/api/html-rewriter.md')
-rw-r--r--docs/api/html-rewriter.md31
1 files changed, 31 insertions, 0 deletions
diff --git a/docs/api/html-rewriter.md b/docs/api/html-rewriter.md
new file mode 100644
index 000000000..f67577b13
--- /dev/null
+++ b/docs/api/html-rewriter.md
@@ -0,0 +1,31 @@
+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.
+
+```ts
+const rewriter = new HTMLRewriter();
+
+rewriter.on("*", {
+ element(el) {
+ console.log(el.tagName); // "body" | "div" | ...
+ },
+});
+```
+
+To parse and/or transform the HTML:
+
+```ts#rewriter.ts
+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](https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/).