Skip to main content

Module html_rewriter

Module html_rewriter 

Source
Expand description

Thin wrapper around Cloudflare’s [lol_html] streaming HTML rewriter.

Replaces the previous fragile str::find / str::rfind rewriting that lived in the image, search, CSP, and html-fix plugins (issue #525). Compared to the old approach lol_html:

  • Skips HTML comments. <!-- <img …> --> is byte-identical in the output rather than being half-rewritten.
  • Preserves character entities. alt="Café &amp; bar" round-trips verbatim — no double-encoding, no entity decoding inside attribute values.
  • Streams. Peak RSS stays flat regardless of input size; there is no full-document DOM materialised in memory.

§API

The crate-private functions rewrite_html and extract_text_with_filter cover every call site inside SSG. Plugins pass closure-based [ElementContentHandlers] or write to a &mut String aggregator and let the wrapper deal with feeding chunks through lol_html::HtmlRewriter.

Functions§

collapse_whitespace
Collapses runs of ASCII whitespace into a single space and trims.
decode_html_entities
Minimal HTML entity decoder for text-extraction call sites.
extract_text_with_filter
Extract concatenated text content from every element matching selector, separating successive elements with a single space.
inject_before_body_close
Rewrites every element with its attributes in name order.
inject_before_body_close_or_append
Injects payload before </body>, appending it when there is none.
rewrite_html
Run lol_html over html with the supplied (selector, handlers) pairs and return the rewritten document.
sort_attributes
Returns SsgError::Io if lol_html fails to parse or rewrite.