How l1xnan/obsidian-better-export-pdf works
Project Type and Purpose
• This is an Obsidian community plugin (“Better Export PDF”) written in TypeScript and Svelte.
• It hooks into the Electron‐based Obsidian host to provide enhanced PDF export capabilities (outline bookmarks, preview, metadata, batch export, etc.).
Architectural Overview
Break the system into four main layers:
A. Obsidian Host (Electron)
B. Plugin Core
C. UI Layer (Svelte Components)
D. PDF Rendering & Filesystem
Main Components
1. Obsidian API Layer
• The host application that loads the plugin via manifest.json.
• Provides plugin lifecycle methods (onload, onunload), command registration, and webview tag for PDF.
2. Plugin Core (src/*.ts)
• main.ts – Entry point. Registers commands, menu items, context actions.
• modal.ts – Controls the export dialog lifecycle and collects user settings.
• render.ts / pdf.ts – Renders Markdown to HTML, injects CSS, calls webview.printToPDF(options).
• setting.ts – Reads and persists plugin configuration.
• utils.ts, constant.ts, type.d.ts – Shared helpers, constants, and TypeScript types.
• version-bump.mjs / versions.json – Release management.
3. UI Layer (Svelte)
• Progress.svelte – Shows export progress bar.
• i18n folder – Localization strings for English and Chinese.
4. Assets & Styling
• assets/ – Preview screenshots used in README.
• styles.css – Global CSS overrides for print styling.
• manifest.json – Plugin metadata (name, version, author, commands).
• esbuild.config.mjs – Build setup for bundling TS and Svelte into a single plugin JS file.
Data Flows and Interactions
• User triggers export (via context menu or command).
↓
• main.ts invokes modal.ts to open Svelte-based export dialog.
↓
• User selects options (page size, margins, CSS snippets, metadata). Svelte UI writes settings to setting.ts.
↓
• Plugin core reads Markdown content from Obsidian vault.
↓
• render.ts converts Markdown to HTML, applies CSS (styles.css + user snippets).
↓
• pdf.ts spins up a hidden <webview> instance, loads HTML, calls webview.printToPDF(options).
↓
• The resulting PDF bytes are written to disk via the Obsidian filesystem API.
↓
• Progress.svelte displays real-time progress; concurrency limit enforced in pdf.ts.
↓
• On completion, plugin notifies Obsidian and optionally opens the PDF.
Architectural Patterns & Design Principles
• Modular separation: UI (Svelte) vs. core logic (TypeScript).
• Single responsibility: each .ts file handles one concern (settings, rendering, PDF calls).
• Concurrency control during batch export to limit resource usage.
• Plugin manifest declares the extension points (commands, settings).
• Localization via i18n folder.
• Plugin files vs. Obsidian/Electron APIs vs. external resources