npm install --save parse5-html-rewriting-stream
📖 Documentation 📖
Modify HTML tokens while streaming the result. Tokens without a handler are passed through unchanged; handlers must emit the replacement tokens.
import { Readable } from 'node:stream';
import { pipeline } from 'node:stream/promises';
import { RewritingStream } from 'parse5-html-rewriting-stream';
const rewriter = new RewritingStream();
rewriter.on('text', (token) => rewriter.emitText({ text: token.text.toUpperCase() }));
let output = '';
rewriter.on('data', (chunk) => {
output += chunk;
});
await pipeline(Readable.from(['<p>Hello, world!</p>']), rewriter);
console.log(output); // <p>HELLO, WORLD!</p>