parse5
repository·master·Indexed 26 days ago
https://github.com/inikulin/parse5A high-performance, WHATWG HTML Living Standard compliant HTML parsing and serialization toolset for Node.js. Designed to mimic browser-level parsing behavior, it includes the core parser and serializer as well as specialized packages: parse5-html-rewriting-stream for streaming transformations, parse5-htmlparser2-tree-adapter for htmlparser2 compatibility, parse5-parser-stream for streaming parsing with scripting support, parse5-plain-text-conversion-stream for converting plain text to HTML, and parse5-sax-parser for event-based parsing.
What's inside parse5
- parse5 is a high-performance, WHATWG HTML Living Standard (HTML5) compliant HTML parsing and serialization toolset for Node.js. It is designed to parse HTML exactly as modern web browsers do and is used by major projects like jsdom, Angular, Lit, Cheerio, and rehype. The toolset includes various specialized packages for streaming, SAX parsing, and tree adaptation.
Overview of parse5-plain-text-conversion-stream
masterTheparse5-plain-text-conversion-streampackage provides a stream implementation that converts plain text into an HTML document as required by the HTML specification.Overview of parse5
masterparse5 is an HTML parser and serializer. It provides tools for parsing HTML documents into a tree structure and serializing them back to HTML strings.Identify the correct parse5 package for your use case
masterThe
parse5ecosystem consists of several specialized packages depending on your requirements for parsing, streaming, or rewriting HTML:- parse5: The core HTML parser and serializer.
- parse5-htmlparser2-tree-adapter: Provides an adapter to use
htmlparser2tree structures with parse5. - parse5-parser-stream: A streaming HTML parser that includes scripting support.
- parse5-plain-text-conversion-stream: A stream designed to convert plain text files into HTML documents.
- parse5-sax-parser: A streaming SAX-style HTML parser.
- parse5-html-rewriting-stream: A streaming HTML rewriter for modifying HTML content on the fly.
Install parse5-sax-parser via npm
masterTo use the streaming SAX-style HTML parser, install the
parse5-sax-parserpackage using npm.npm install --save parse5-sax-parserInstall parse5-parser-stream via npm
masterTo use the streaming HTML parser with scripting support in your project, install the
parse5-parser-streampackage using npm.npm install --save parse5-parser-streamInstall parse5-html-rewriting-stream
masterInstall the
parse5-html-rewriting-streampackage via npm to use the streaming HTML rewriter.npm install --save parse5-html-rewriting-streamInstall parse5-plain-text-conversion-stream
masterInstall the
parse5-plain-text-conversion-streampackage via npm to use a stream that converts plain text into an HTML document according to the HTML specification.npm install --save parse5-plain-text-conversion-streamInstall parse5-htmlparser2-tree-adapter
masterInstall the
parse5-htmlparser2-tree-adapterpackage via npm to use thehtmlparser2tree adapter withparse5.npm install --save parse5-htmlparser2-tree-adapterInstall parse5 via npm
masterTo use the parse5 HTML parser and serializer in your project, install it using npm:
npm install --save parse5Handle Adoption Agency Algorithm behavior
masterThe parser implements the HTML 'Adoption Agency' algorithm. This algorithm is triggered when a formatting element (like
<b>or<i>) is encountered while an existing formatting element of the same type is already in scope.When this occurs, the parser:
- Obtains the formatting element entry.
- Finds the furthest block in the stack.
- Performs an inner loop to move/recreate elements to ensure proper tree construction.
- Replaces the formatting element to maintain spec-compliant DOM structure.
Developers using the parser can rely on this built-in behavior to ensure that malformed or highly nested formatting tags are parsed exactly as a web browser would handle them.
Use RewritingStream for streaming HTML transformations
masterThe
RewritingStreamclass is a Node.jsTransformstream that allows you to perform SAX-style HTML rewriting. It parses HTML and emits events for different token types, allowing you to intercept and modify them before pushing the results to the output stream. Because it is aTransformstream, you can pipe data through it (e.g., from an HTTP response to a file).To rewrite HTML, listen for events like
startTag,endTag, ortext, modify the token if necessary, and then call the correspondingemit*method to write the modified (or original) content to the stream.const RewritingStream = require('parse5-html-rewriting-stream'); const http = require('http'); const fs = require('fs'); const file = fs.createWriteStream('/home/google.com.html'); const rewriter = new RewritingStream(); // Replace divs with spans rewriter.on('startTag', startTag => { if (startTag.tagName === 'span') { startTag.tagName = 'div'; } rewriter.emitStartTag(startTag); }); rewriter.on('endTag', endTag => { if (endTag.tagName === 'span') { endTag.tagName = 'div'; } rewriter.emitEndTag(endTag); }); // Wrap all text nodes with an <i> tag rewriter.on('text', (_, raw) => { // Use the raw representation of text without HTML entities decoding rewriter.emitRaw(`<i>${raw}</i>`); }); http.get('http://google.com', res => { // Assumes response is UTF-8. res.setEncoding('utf8'); // `RewritingStream` is a `Transform` stream, which means you can pipe // through it. res.pipe(rewriter).pipe(file); });