Skip to main content
Mastra v1 is coming in January 2026. Get ahead by starting new projects with the beta or upgrade your existing project today.

Semantically Chunking HTML

When working with HTML content, you often need to break it down into smaller, manageable pieces while preserving the document structure. The chunk method splits HTML content intelligently, maintaining the integrity of HTML tags and elements. This example shows how to chunk HTML documents for search or retrieval purposes.

import { MDocument } from "@mastra/rag";

const html = `
<div>
<h1>h1 content...</h1>
<p>p content...</p>
</div>
`;

const doc = MDocument.fromHTML(html);

const chunks = await doc.chunk({
headers: [
["h1", "Header 1"],
["p", "Paragraph"],
],
});

console.log(chunks);





View source on GitHub