A lightweight, zero-dependency, and Trusted-Types compliant patch tool that brings rich mathematical typesetting (LaTeX / KaTeX) directly into the Antigravity IDE chat conversation panel.
While Antigravity IDE natively renders LaTeX formulas in the built-in Markdown: Preview to the Side tab, its primary AI Agent conversation panel lacks native math parsing:
- Mathematical expressions written in standard syntax (
$...$and$$...$$) remain raw, unformatted text. - Markdown parsing can break math formulas by turning subscripts like
_{xx}into italic<em>tags. - Strict Chromium Trusted Types CSP (
require-trusted-types-for 'script') prevents naive HTML injection methods (innerHTML).
Antigravity LaTeX Patch resolves all these problems by injecting a non-destructive, Trusted-Types safe observer into the Antigravity Workbench window.
- ⚡ Real-Time AI Streaming: Dynamically typesets formulas token-by-token as the AI generates mathematical responses.
- 🛡️ 100% Trusted-Types & CSP Compliant: Built strictly using W3C DOM Range & Node Manipulation APIs—zero
innerHTMLusage. - 🔤 Math-Italic Un-mangler: Automatically prevents Markdown from corrupting LaTeX subscripts (
_{xx},_{xy}). - 📐 Strict Block Scoping: Isolates equation parsing to individual paragraphs and list items, preventing formulas from swallowing headings, dividers, or text.
- 🌐 100% Offline: Leverages the KaTeX engine and web fonts already bundled with Antigravity IDE. No internet or external CDN required.
- 🔄 Safe & Reversible: Atomic 1-click install and 1-click rollback with automated clean backups.
- Node.js
>= 16.0.0 - Antigravity IDE installed on Windows, macOS, or Linux.
Clone this repository and run the install command:
git clone https://github.com/dznpev/antigravity-latex-patch.git
cd antigravity-latex-patch
node bin/cli.js installPress Ctrl + Shift + P (or Cmd + Shift + P on macOS) -> select Developer: Reload Window (or restart the IDE).
Open the developer console (Help -> Toggle Developer Tools), and you will see:
[Antigravity LaTeX Patch v1.0.0] Production AI Math Engine active.
To completely revert Antigravity IDE to its original state:
node bin/cli.js uninstallThen reload the window.
Antigravity IDE (workbench.html)
├── Loads ag-latex-style.css (KaTeX font styles)
└── Loads ag-latex-script.js
├── KaTeX Engine (decapsulated factory)
├── Un-mangler (recovers LaTeX underscores from markdown <em>)
├── Strict Block Scoper (processes <p>, <li>, <blockquote> independently)
├── W3C DOM Range API (replaces math tokens without innerHTML)
└── MutationObserver (debounced real-time wake on streaming tokens)
- Workbench Injection: Injects standalone scripts directly into
workbench.html, targeting the core Workbench process where the AI chat UI runs. - AST Protection: Before parsing, the un-mangler detects italic tags generated by Markdown between math subscripts and restores original LaTeX underscores.
- W3C Range Replacement: Uses
document.createRange()andrange.insertNode(span)to insert KaTeX elements directly, ensuring zero violation of Chromium'srequire-trusted-types-for 'script'.
- Idempotent: Running
installmultiple times will never duplicate code or corrupt files. - Original Backup Preserved: The first clean backup (
.bak) is permanently preserved and never overwritten by subsequent runs. - Clean Console: Internal KaTeX font metric warnings for non-Latin characters (e.g. Vietnamese glyphs) and non-strict unicode warnings are filtered.
Khi trò chuyện với AI Agent trên Antigravity IDE, các công thức toán học ($...$ và $$...$$) thường chỉ hiển thị dưới dạng văn bản thô khó nhìn. Trong khi đó, tính năng Preview to the Side lại hiển thị rất đẹp.
Antigravity LaTeX Patch mang trọn vẹn khả năng hiển thị toán học KaTeX chất lượng cao vào ngay trong khung chat, hoạt động mượt mà với cả quá trình AI đang sinh câu trả lời (streaming).
- ⚡ Hỗ trợ Streaming thời gian thực: Render mượt mà từng ký hiệu toán học khi Agent đang gõ câu trả lời theo thời gian thực.
- 🛡️ Bảo mật tuyệt đối (Trusted Types Compliant): 100% sử dụng W3C DOM Range & Node Manipulation API, không bao giờ vi phạm chính sách bảo mật CSP
TrustedHTMLcủa Chromium. - 🔤 Bộ giải cứu chỉ số dưới (Un-mangler): Tự động ngăn chặn Markdown hiểu nhầm các ký tự gạch dưới (
_{xx},_{xy}) thành thẻ in nghiêng<em>. - 📐 Khóa ranh giới khối (Strict Block Scoping): Cô lập từng khối văn bản (
<p>,<li>), ngăn chặn triệt để hiện tượng công thức nuốt chửng tiêu đề, thẻ kẻ ngang<hr>hoặc nhân bản văn bản. - 🌐 Hoàn toàn Offline: Sử dụng trực tiếp bộ thư viện KaTeX và font toán học tích hợp sẵn bên trong Antigravity IDE, không cần kết nối mạng hay tải từ CDN.
- 🔄 Cài đặt & Gỡ bỏ 1 chạm: Tự động sao lưu file gốc và khôi phục nguyên trạng 100% chỉ bằng một dòng lệnh.
- Đã cài đặt Node.js
>= 16.0.0 - Antigravity IDE trên Windows, macOS hoặc Linux.
Mở terminal và thực thi:
git clone https://github.com/dznpev/antigravity-latex-patch.git
cd antigravity-latex-patch
node bin/cli.js installNhấn tổ hợp phím Ctrl + Shift + P -> gõ và chọn Developer: Reload Window (hoặc khởi động lại IDE).
node bin/cli.js statusKhi muốn gỡ bỏ hoàn toàn và trả IDE về trạng thái xuất xưởng:
node bin/cli.js uninstallSau đó Reload Window để hoàn tất.
- Vị trí can thiệp: Script được nạp trực tiếp qua
workbench.htmlcủa Antigravity Workbench, nơi giao diện chat chính thức được render. - Thao tác DOM tuân thủ Trusted Types:
Thay vì dùng
innerHTML(vốn bị Chromium chặn đứng), công cụ sử dụngdocument.createRange()vàwindow.katex.render()để tạo DOM node toán học trực tiếp. - Khử cảnh báo Console: Tự động tắt cảnh báo strict mode, khử các ký tự unicode lạ từ AI (như dấu phẩy trên
′, khoảng trắng vô hình\u200B) và lọc cảnh báo font metrics cho tiếng Việt.
antigravity-latex-patch/
├── package.json # Package manifest & npm scripts
├── LICENSE # MIT License
├── README.md # Song ngữ EN / VI documentation
├── .gitignore # Git exclusion rules
├── bin/
│ └── cli.js # Giao diện dòng lệnh CLI (install, uninstall, status)
└── src/
├── index.js # Main programmatic API (AntigravityLatexPatcher)
├── detector.js # Auto-discovery for Antigravity IDE paths
├── backup.js # Safe atomic file backup & restore manager
└── injector.js # Client script builder & HTML injector
This project is licensed under the MIT License - feel free to use, modify, and contribute.
