How jwtor7/mdviewer works
1. Overall shape
This is an Electron desktop app with three clear runtime boundaries: the Electron main process, a sandboxed preload bridge, and a React renderer. The renderer owns all UI and editing behavior; the main process owns filesystem access, window lifecycle, native integrations, and document conversion.
2. Primary user flow
A file enters the app through drag-and-drop, the open-file dialog, macOS file association, or a recent-file selection. From there, the main process validates the path, loads or converts the file, and sends normalized Markdown/text content into the renderer for display and editing.
3. Renderer / UI subsystem
src/renderer.tsx and src/App.tsx boot the React UI. The main UI is composed from src/components/ such as Markdown preview, code editor, text preview, find/replace, Mermaid rendering, and error notifications. The renderer is where view mode switching, editing, formatting actions, and visual presentation happen.
4. Document state and editor behavior
The custom hooks in src/hooks/ are the main state-management layer. They coordinate document loading/saving, file drops, keyboard shortcuts, theme selection, split-pane behavior, word wrap, clipboard actions, and error handling. This is where user interactions become document state changes.
5. Main-process responsibilities
src/main.ts is the Electron entry point. The src/main/ folder contains the supporting services: windowManager.ts for window/tab lifecycle, fileWatcher.ts for reacting to external file changes, storage/ for preferences and recent files, and markitdown.ts for conversion of non-Markdown inputs.
6. Conversion pipeline
Native Markdown opens directly. Other supported document types are converted through the markitdown path, which depends on a Python environment created by the repo’s install scripts. This is the key content-ingest boundary: the app normalizes heterogeneous formats into Markdown before the renderer consumes them.
7. Audio/video transcription
Audio and video files are a special ingestion path. The README indicates transcription uses Google’s Web Speech API, making it the one intentional outbound network dependency. This path exists alongside local conversion but is architecturally distinct because it crosses the privacy boundary and depends on external speech services plus local media tooling like ffmpeg.
8. Security boundary
The app is designed around a hardened IPC boundary. src/preload.ts exposes a limited bridge into the renderer, while src/main/security/ enforces IPC schema validation, path validation, and rate limiting. The README also calls out sandboxed rendering, context isolation, and a strict CSP, so the diagram should show the renderer as untrusted and the main process as the authority.
9. Persistence and OS integration
Preferences and recent files are stored in src/main/storage/, which supports app state across launches. The install scripts and forge.config.js indicate packaging for macOS, including file associations and app installation into /Applications. These are important native boundaries because they determine how files enter the app and how the packaged app is distributed.
10. Shared utilities and transformation helpers
src/utils/ contains reusable content-processing logic: text calculations, text editing helpers, clipboard sanitization, file validation, PDF rendering, text conversion, and Mermaid-related section wrapping. These are not standalone subsystems, but they are the shared transformation layer used by the renderer and hooks.
11. Types, schemas, and test structure
src/types/ holds the cross-process contracts, including IPC schemas and Electron typings. Tests are colocated with implementation and cover both UI and utility behavior with Vitest/React Testing Library. For architecture purposes, this suggests tight coupling of code and tests around each subsystem rather than a separate test harness layer.
12. What to draw as the main data path
The most important diagram path is: file input / OS event → main process validation and conversion → IPC bridge → renderer document state → view/editor components. A secondary path is renderer action → preload IPC → main process save/storage/window services. A third, conditional path is audio/video input → transcription service → normalized Markdown output.
Main entry points: main.ts Main entry · preload.ts Preload bridge · MarkdownPreview.tsx UI components · textEditing.ts Utilities
Components 14
Main process
Electron authority: windowing, file I/O, conversion, persistence, and security checks.
- Main entry · Electron startup and app wiring.
src/main.ts - Window manager · Creates and manages windows and lifecycle.
src/main/windowManager.ts - File watcher · Detects file changes outside the app.
src/main/fileWatcher.ts - Conversion · Converts non-Markdown documents into normalized text/Markdown.
src/main/markitdown.ts - Storage · Persists preferences and recent-file state.
src/main/storage/preferences.ts - Security · Validates IPC, paths, and request rate.
src/main/security/ipcValidation.ts
Preload bridge
Restricted IPC surface exposed to the renderer.
- Preload bridge · Sandboxed API surface between renderer and main.
src/preload.ts
Renderer
React UI, editing state, and presentation logic.
- Renderer entry · Bootstraps the React renderer.
src/renderer.tsx - App shell · Top-level application composition and mode switching.
src/App.tsx - UI components · Markdown preview, code editor, text preview, find/replace, Mermaid, and notifications.
src/components/MarkdownPreview.tsx - Hooks · Document loading, shortcuts, drag-and-drop, theme, wrap, split-pane, clipboard, and error handling.
src/hooks/useDocuments.ts
Shared code
Cross-cutting types and transformation utilities used by multiple runtimes.
- Types & schemas · IPC schemas and shared Electron/document typings.
src/types/ipc-schemas.ts - Utilities · Text, clipboard, file validation, PDF, and Mermaid-related helpers.
src/utils/textEditing.ts - Default content · Initial editor content and constants.
src/constants/defaultContent.ts
Connections 20
- Main entry → Window manager: creates · Main startup delegates window lifecycle.
- Main entry → Security: guards · Startup and IPC are constrained by validation.
- Main entry → Storage: loads state · Reads preferences and recent files.
- Main entry → Conversion: converts · Normalizes supported non-Markdown inputs.
- Main entry → File watcher: watches · Tracks external file changes.
- Main entry → Preload bridge: exposes bridge · Connects authority layer to sandboxed renderer access.
- Preload bridge → Types & schemas: uses schemas · IPC surface is defined by shared contracts.
- Renderer entry → App shell: boots · Renderer entry mounts the app shell.
- App shell → UI components: composes · Main UI is assembled from editor and preview components.
- App shell → Hooks: orchestrates · App behavior is driven by custom hooks.
- Hooks → Preload bridge: calls IPC · Hooks invoke the limited preload API for file and app actions.
- Hooks → Utilities: transforms · Hooks reuse shared editing and validation helpers.
- UI components → Utilities: renders data · UI consumes text and markup helpers indirectly.
- UI components → Default content: defaults · Initial editor content seeds the UI.
- Conversion → Utilities: normalizes · Conversion and text handling share helper logic.
- Security → Types & schemas: validates against · Schemas define the allowed IPC and document boundaries.
- File watcher → Preload bridge: notifies · External file updates flow back toward the renderer via the bridge.
- Storage → Preload bridge: persists via IPC · Preferences and recent files are updated through the authority boundary.
- Default content → UI components: seeds · Provides initial empty-state content for rendering.
- Utilities → Types & schemas: shares contracts · Helpers and transformations rely on shared document types.