A fast and lightweight HTML parser for Bun
npm install @tkeron/html-parserA fast and lightweight HTML parser for Bun that converts HTML strings into DOM Document objects. Built with a custom tokenizer optimized for Bun runtime.
- ⥠Custom Tokenizer: Tokenizer specifically optimized for Bun runtime
- ð Ultra Fast: Leverages Bun's native optimizations
- ðŠķ Lightweight: Zero external dependencies
- ð Standards Compliant: Returns standard DOM Document objects
- ð§ TypeScript Support: Full TypeScript definitions included
- â
Well Tested: Comprehensive test suite (5660+ tests passing)
- ðŊ HTML5 Spec: Implements Adoption Agency Algorithm for proper formatting element handling
- ð§Đ Fragment Parsing: Parse HTML fragments with context element support
``bash`
npm install @tkeron/html-parser
Or with Bun:
`bash`
bun add @tkeron/html-parser
`typescript
import { parseHTML } from "@tkeron/html-parser";
// Parse HTML string into DOM Document
const html =
"
// Use standard DOM methods
const title = document.querySelector("title")?.textContent;
const heading = document.querySelector("h1")?.textContent;
console.log(title); // "Test"
console.log(heading); // "Hello World"
`
`typescript
import { parseHTML } from "@tkeron/html-parser";
const html =
Hello, world!
;const doc = parseHTML(html);
const container = doc.querySelector(".container");
const info = doc.getElementById("info");
console.log(container?.children.length); // 2
console.log(info?.textContent); // "This is a test"
`API
$3
Parses an HTML string and returns a DOM Document object.
Parameters:
-
html (string): The HTML string to parseReturns:
-
Document: A standard DOM Document object with all the usual methods like querySelector, getElementById, etc.$3
Parses an HTML string as a fragment within a context element. Useful for parsing innerHTML-style content.
Parameters:
-
html (string): The HTML string to parse
- contextTagName (string): The tag name of the context element (e.g., "div", "body")Returns:
-
Node[]: An array of parsed nodesExample:
`typescript
import { parseHTMLFragment } from "@tkeron/html-parser";const nodes = parseHTMLFragment("Hello World", "div");
console.log(nodes.length); // 3 (b element, text node, i element)
`Development
This project is built with Bun. To get started:
`bash
Install dependencies
bun installRun tests
bun test`Testing
Run the test suite:
`bash
bun test
`License
MIT
Contributing
1. Fork the repository
2. Create your feature branch (
git checkout -b feature/amazing-feature)
3. Commit your changes (git commit -m 'Add some amazing feature')
4. Push to the branch (git push origin feature/amazing-feature`)If you encounter any issues or have questions, please file an issue on the GitHub repository.