ExamplesRAGChunk HTML

Chunk 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 Example on GitHub

MIT 2025 © Nextra.