Mayank Chaudhari
Back to Blog

Bridging GenAI & Legacy Enterprise: The MDAST AST Transformation Pipeline

Bridging GenAI & Legacy Enterprise: The MDAST AST Transformation Pipeline
Engineering
#Architecture#GenAI#AST#Enterprise

The "Last Mile" Problem in Enterprise GenAI

In the current wave of Generative AI, millions of dollars are spent on LLM infrastructure (RAG, Vector DBs, Fine-Tuning). We have mastered the art of generating high-quality tokens. However, in regulated industries like Banking, Legal, and Healthcare, Markdown is not a deliverable.

You cannot send a Markdown string to a regulator. You cannot email a response.md to a C-Suite executive. They demand DOCX. They demand styled, branded, and legally identifiable documents.

This creates a friction point: How do we transmute volatile AI-generated Markdown into rigid, compliance-ready OOXML (Office Open XML)?

The naive approach is Regex. The architectural approach is AST Transformation.

The Architecture: AST-to-AST Projection

To solve this at the platform level (handling thousands of reports per hour), we moved beyond string manipulation. We treated the document conversion as a compiler problem.

We utilize the Unified.js ecosystem to parse Markdown into MDAST (Markdown Abstract Syntax Tree). We then project this tree directly into the OOXML structure used by docx.js (the underlying rendering engine).

Isomorphic by Design

Unlike legacy tools that rely on server-side binaries (Pandoc, LibreOffice), this entire pipeline is JavaScript-native and Isomorphic. It runs identical logic on:

  • The Server (Node.js/Edge): For high-volume batch processing or "Download as DOCX" API endpoints.
  • The Client (Browser): For immediate, offline-capable generation without server costs.

The Pipeline

graph LR LLM[LLM Output] -->|Markdown String| Parser Parser[remark-parse] -->|MDAST JSON Tree| Transformer Transformer[mdast2docx] -->|AST Traversal and Projection| Buffer Buffer[DOCX Blob] -->|Download or Stream| ClientOrServer ClientOrServer[Client / Server]
  1. Lexical Analysis: The AI output is tokenized.
  2. Semantic Parsing: We construct the MDAST. At this stage, we can perform Static Analysis on the content (e.g., detecting PII, stripping dangerous HTML injections) before the document is ever generated.
  3. Projection: We map MDAST nodes (Root, Paragraph, Table) to their OOXML counterparts (doc.addSection, new Paragraph, new Table).

Technical Deep Dive: mdast2docx

I open-sourced the core engine of this pipeline as mdast2docx (and the modular @m2d/* ecosystem).

Modular by Design

Enterprise constraints often require minimal bundle sizes. The library is architected as a set of decoupled plugins. The Core transformer handles basic blocks (Text, Bold, Italic), while complex nodes are injected via the plugins prop.

[!NOTE] Environment Constraints: While the core is isomorphic, specific plugins may have environment dependencies. For example, @m2d/mermaid (charting) relies on Browser Canvas APIs and is currently Client-Side only, whereas @m2d/table works universally.

import { toDocx } from "@m2d/core";
import { imagePlugin } from "@m2d/image";
import { tablePlugin } from "@m2d/table";
import { mathPlugin } from "@m2d/math";

// Transforming an AI-generated AST into a Blob
const docxBlob = await toDocx(
  mdastTree, 
  {
    // Global Document Styles (Company Branding)
    styles: enterpriseStyleConfig
  }, 
  {
    // Feature Plugins
    plugins: [
      imagePlugin(), // Canvas-based image rendering
      tablePlugin(), // Recursive table projection
      mathPlugin()   // LaTeX to OMML (Office Math ML) conversion
    ],
  }
);

Extending for Custom Entities

The power of ASTs lies in custom directives. If your internal AI generates a generic "Warning" block, you can parse it as a custom MDAST node and write a mdast2docx plugin to render it as a standardized "Risk Badge" in the Word document.

// Custom Plugin Strategy
const riskPlugin = () => {
  return (node) => {
    if (node.type === 'risk-directive') {
      return new Paragraph({
        text: `⚠️ RISK: ${node.value}`,
        shading: { fill: "FF0000" } 
      });
    }
  }
}

Why This Matters

This architecture shifts the "Document Generation" responsibility from the Backend (Python/Jinja templates) to the Edge.

By decoupling the Content (Markdown) from the Presentation (DOCX Styles), we allow the AI agents to focus purely on semantic correctness. The AST pipeline ensures that every generated document—whether it's a 50-page Audit Report or a 1-page Summary—is syntactically perfect and visually consistent with enterprise branding.

This is not just a converter; it is the rendering engine for the AI-native enterprise.

Did you enjoy this post?

Give it a like to let me know!