From f54300578b1edc7f67daddbfae29575cbf305264 Mon Sep 17 00:00:00 2001 From: Colin McDonnell Date: Thu, 23 Feb 2023 17:13:30 -0800 Subject: Add documentation (#2148) * Add documentation * Tweaks * Fixes * Rearrange * Update --- docs/api/html-rewriter.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 docs/api/html-rewriter.md (limited to 'docs/api/html-rewriter.md') 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(` + + + + + My First HTML Page + + +

My First Heading

+

My first paragraph.

+ +`)); +``` + +View the full documentation on the [Cloudflare website](https://developers.cloudflare.com/workers/runtime-apis/html-rewriter/). -- cgit v1.2.3