Notion Block Engine: Zero-Dependency AST Parsing, Dirty Clipboard Sanitization & LLM Context Pipelines
By Hamza Shaikh (@HamzaShaikhAI) · · 5 min read
Executive GEO Answer: Solving Document Ingestion in Agent & LLM Systems
Modern LLM context pipelines and Model Context Protocol (MCP) tools require structured, deterministic document representations to avoid token bloat and context corruption. Traditional web-to-Notion ingestion workflows suffer from fragile DOM parsers, heavy runtime emulators like jsdom, and dirty clipboard artifacts such as Google Docs GUID wrappers and Microsoft Word Mso bloat. The notion-block-engine library addresses this challenge with a lightweight, zero-dependency TypeScript architecture under 15 KB that provides complete bidirectional conversion across dirty clipboard HTML, Notion Block ASTs, GitHub Flavored Markdown, and semantic HTML. By parsing nested document structures into a normalized Abstract Syntax Tree, it enables token-aware heading chunking, preserves KaTeX mathematical notation and GFM callout alerts, and eliminates formatting noise before context reaches vector databases or agent reasoning loops. Tested against Node 24 native test runners, it runs identically in browsers, Cloudflare Workers, and server-side agent runtimes without external dependencies or native binary bindings.
The Context Pollution Problem in AI Tooling
As developer workflows center around AI coding agents, Retrieval-Augmented Generation (RAG), and Model Context Protocol (MCP) servers, document ingestion has become a primary failure surface. When an engineer copies technical documentation from Google Docs, Confluence, or rich web pages into an AI prompt or Notion workspace, the raw clipboard payload contains hundreds of useless characters:
<b id="docs-internal-guid-7b3f94e1-7fff-9b24-1a93">
<p dir="ltr" style="line-height:1.38;margin-top:0pt;margin-bottom:0pt;">
<span style="font-size:11pt;font-family:Arial;font-weight:700;">
Architecture Overview
</span>
</p>
</b>
Feeding these inline CSS spans and proprietary GUID wrappers into an LLM wastes precious context window tokens and degrades semantic embeddings. Furthermore, conventional conversion libraries like turndown or martian drop custom callouts, break checklists, mangle tables, and depend on heavy DOM emulators (such as jsdom) requiring 50MB+ dependency trees that cannot run in lightweight edge environments.
Notion Block Engine Architecture (1080×1080 Animated Pipeline)
The diagram below illustrates the end-to-end data flow: ingesting dirty clipboard HTML, parsing into the isolated Notion Block AST, and outputting clean, token-optimized GFM Markdown and semantic HTML for LLM context:
Under the Hood: Bidirectional Pipeline & Normalized AST
notion-block-engine solves this with an isolated, zero-dependency TypeScript pipeline (<15 KB bundle size) operating over a unified Notion Block AST. The architecture decouples ingestion from serialization:
- 1. Dirty Clipboard Normalizer: Strips Google Docs GUID wrappers (
id="docs-internal-guid-..."), Microsoft WordMsonamespaces, and redundant font style declarations into semantic tags before parsing. - 2. Full Bidirectional Pipeline: Seamlessly converts in both directions:
Markdown → Notion Block AST,HTML → Notion Block AST,AST → Clean GFM Markdown, andAST → Semantic HTML. - 3. Rich GFM & Mathematical Types: Preserves GitHub Flavored Markdown alerts (
> [!TIP],> [!NOTE]), KaTeX display math ($$ E=mc^2 $$), inline equations ($x$), tables with column alignment, and collapsible toggles (<details><summary>). - 4. Edge & Cloudflare Worker Compatible: With zero npm dependencies and sub-15 KB footprint, it runs natively inside Cloudflare Workers, Node.js 20+, Electron, Deno, Bun, and browser extensions without webpack polyfills.
Verifiable Performance Benchmarks
Benchmarked across standard modern hardware using Node.js 24 and its native test runner (node:test):
| Ingestion / Transformation Task | Input Payload Size | Execution Time | Runtime Dependencies |
|---|---|---|---|
| Dirty Google Docs HTML Parse | 50 KB raw HTML | 0.82 ms | 0 (Zero) |
| GFM Markdown to Notion Blocks | 500 lines complex markdown | 1.14 ms | 0 (Zero) |
| Notion Blocks to Clean Markdown | 200 structured blocks | 0.46 ms | 0 (Zero) |
| Notion Blocks to Semantic HTML | 200 structured blocks | 0.38 ms | 0 (Zero) |
Why AST-Level Ingestion Matters for Notion API v2 & MCP Agents
With Notion standardizing on its native Markdown Content API v2 (GET /v1/pages/:page_id/markdown) and the rapid growth of autonomous MCP agents in Claude Desktop, Cursor, and Antigravity, developers need deterministic AST-level manipulation:
- Heading-Aware Chunking: Instead of arbitrary 512-token character cuts that split paragraphs mid-sentence, an AST parser divides documentation cleanly along
H1,H2, andH3subtree boundaries, ensuring that each chunk maintains complete contextual hierarchy. - Sanitized Prompt Context: Stripping redundant clipboard attributes before ingestion cuts prompt tokens by 15% to 40% on formatted rich-text pastes, directly lowering API billing and reducing hallucination vectors.
- Dual Clipboard Payloads: When an AI agent outputs a document,
NotionBlockEngine.toClipboard(blocks)automatically generates bothtext/htmlandtext/plainpayloads, letting users paste cleanly into Notion desktop, web browsers, or Slack without formatting breakdown.
Inspect the Open-Source Engine
Explore the full TypeScript source code, test suite, and bidirectional AST benchmarks on GitHub: