How gboyd068/obsidian-swiftlatex-render works
Main Components
a. Host Environment
• Obsidian App (plugin host)
• Obsidian Plugin API & Plugin Manager
b. Plugin Frontend
• manifest.json (plugin metadata)
• main.ts → transpiled/bundled to main.js (entry point)
• UI integration (detects latex / latexsvg codeblocks, injects rendered output into DOM)
• styles.css (styling of rendered blocks)
c. Compilation Engines (Web Workers + WASM)
• PdfTeXEngine (swiftlatexpdftex.worker.js + pdftex.wasm)
• DvipdfmxEngine (swiftlatexdvipdfm.worker.js + dvipdfm.wasm)
• XeTeXEngine (swiftlatexxetex.worker.js + xetex.wasm)
• Poppler pdftocairo worker (pdftocairo.js + poppler.wasm)
d. Caching & Storage
• Package cache (.obsidian/swiftlatex-render-cache/package-cache)
• PDF cache (.obsidian/swiftlatex-render-cache/pdf-cache)
e. Remote Services
• TeX Live on-demand package server (https://texlive2.swiftlatex.com/)
Relationships & Data Flow
1. User writes a latex or latexsvg codeblock in Obsidian.
2. Plugin’s main.js (in the main thread) scans markdown and hooks into the rendering pipeline.
3. For each codeblock:
• Select appropriate engine (PdfTeX by default or XeTeX/DVIPDFMX if configured).
• Spawn the corresponding Web Worker.
• Send the raw LaTeX source over postMessage.
4. In the Web Worker:
a. Mount cached packages from package-cache into the WASM FS (Emscripten virtual FS).
b. If the code refers to a missing package:
– Fetch .sty or other files via HTTP from the remote TeX Live server.
– Store newly fetched packages in the package-cache folder (persisted by the plugin).
– Load them into the WASM FS.
c. Invoke the WASM engine to compile .tex → .pdf or .dvi → .pdf.
d. Write output PDF into the WASM FS and postMessage it back to main thread.
5. Back in the main thread:
• If the block type is latexsvg, spawn the pdftocairo Web Worker.
• Send the PDF blob to pdftocairo worker, convert → SVG, return SVG markup.
• Insert the PDF (as an <iframe>/scrollable
) or inline SVG into the Obsidian note.
• Update pdf-cache so repeated renders reuse the compiled PDF.
Architectural Patterns & Design Principles
• Worker-based isolation: each engine runs in its own thread (non-blocking).
• Virtual FS + caching layer: packages are fetched once, persisted, and reused.
• Plugin modularity: clear separation between the plugin frontend, compilation logic, and remote package service.
• On-demand loading: heavy WASM engines and packages are loaded only when needed.
Technologies & Frameworks
• TypeScript for plugin logic (main.ts & engine wrappers)
• Rollup / esbuild for bundling (rollup.config.mjs, esbuild.config.mjs)
• Emscripten-generated WASM modules for TeX engines and Poppler
• Obsidian Plugin API (manifest.json, main.ts hooks)
• HTTP fetch for remote package retrieval
• Local FS via Node/Browser API for caches (.obsidian/...)