{"library":"parse5","title":"parse5 HTML Parser and Serializer","description":"parse5 is a spec-compliant HTML parsing and serialization library for JavaScript, faithfully implementing the HTML5 specification (WHATWG). It offers robust parsing capabilities, creating a syntax tree that can then be manipulated or serialized back into HTML. The current stable version is 8.0.1, with releases occurring periodically to address dependencies, bug fixes, and minor enhancements. Major version bumps (like v7.0.0 and v8.0.0) typically introduce significant internal refactors or breaking changes. Key differentiators include its strict adherence to the HTML5 parsing algorithm, extensive TypeScript type definitions, and its use as the underlying HTML parser in popular projects such as Cheerio, rehype, and Lit. It provides both a pull-stream based parser and a non-streaming API for parsing strings, along with customizable tree adapters.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install parse5"],"cli":null},"imports":["import { parse } from 'parse5';","import { parseFragment } from 'parse5';","import { serialize } from 'parse5';","import { ParserStream } from 'parse5';","import { DefaultTreeAdapter } from 'parse5/lib/tree-adapters/default';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parse, serialize, parseFragment } from 'parse5';\nimport { createReadStream } from 'fs';\nimport { ParserStream } from 'parse5';\n\n// Example 1: Parsing a full HTML document\nconst htmlDoc = '<!DOCTYPE html><html><head><title>Test</title></head><body><h1>Hello</h1><p>World</p></body></html>';\nconst document = parse(htmlDoc);\nconsole.log('Parsed Document Type:', document.nodeName); // #document\n\n// Example 2: Parsing an HTML fragment\nconst htmlFragment = '<div><span>Fragment</span></div>';\nconst fragment = parseFragment(htmlFragment);\nconsole.log('Parsed Fragment Node Name:', fragment.nodeName); // #document-fragment\n\n// Example 3: Serializing a document back to HTML\nconst serializedHtml = serialize(document);\nconsole.log('Serialized HTML:', serializedHtml);\n\n// Example 4: Using ParserStream for large files (simplified example)\nconst largeHtmlFilePath = 'some-large-file.html'; // Replace with a real path if testing locally\nconst parserStream = new ParserStream();\n\n// In a real scenario, you'd pipe a readable stream to parserStream\n// For demonstration, we'll simulate a stream.\n// For actual usage, createReadStream(largeHtmlFilePath).pipe(parserStream);\nparserStream.write('<!DOCTYPE html><html><head></head><body>');\nparserStream.write('<h1>Streaming</h1><p>Example</p>');\nparserStream.end('</body></html>');\n\nparserStream.on('finish', () => {\n  console.log('Stream Parsed Document Type:', parserStream.document.nodeName);\n});\n","lang":"typescript","description":"Demonstrates basic full document parsing, HTML fragment parsing, serialization, and the usage of the streaming parser.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}