# jwtor7/mdviewer architecture

> Architecture diagram of the GitHub repository jwtor7/mdviewer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jwtor7/mdviewer
- Repository: https://github.com/jwtor7/mdviewer

## Overview

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: [`src/main.ts`](https://github.com/jwtor7/mdviewer/blob/main/src/main.ts) (Main entry), [`src/preload.ts`](https://github.com/jwtor7/mdviewer/blob/main/src/preload.ts) (Preload bridge), [`src/components/MarkdownPreview.tsx`](https://github.com/jwtor7/mdviewer/blob/main/src/components/MarkdownPreview.tsx) (UI components), [`src/utils/textEditing.ts`](https://github.com/jwtor7/mdviewer/blob/main/src/utils/textEditing.ts) (Utilities)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_main["Main process"]
  node_node_main_entry["Main entry<br/>electron main<br/>[main.ts]"]
  node_node_window_manager["Window manager<br/>native windows<br/>[windowManager.ts]"]
  node_node_file_watcher["File watcher<br/>external change<br/>[fileWatcher.ts]"]
  node_node_markitdown["Conversion<br/>ingest pipeline<br/>[markitdown.ts]"]
  node_node_storage[("Storage<br/>prefs/recent files<br/>[preferences.ts]")]
  node_node_security["Security<br/>ipc/path guard<br/>[ipcValidation.ts]"]
end

subgraph group_group_bridge["Preload bridge"]
  node_node_preload["Preload bridge<br/>IPC bridge<br/>[preload.ts]"]
end

subgraph group_group_renderer["Renderer"]
  node_node_renderer_entry["Renderer entry<br/>react boot<br/>[renderer.tsx]"]
  node_node_app["App shell<br/>react shell<br/>[App.tsx]"]
  node_node_ui_components["UI components<br/>editor views"]
  node_node_hooks["Hooks<br/>state orchestration<br/>[useDocuments.ts]"]
end

subgraph group_group_shared["Shared code"]
  node_node_types["Types & schemas<br/>contracts<br/>[ipc-schemas.ts]"]
  node_node_utils["Utilities<br/>transform helpers<br/>[textEditing.ts]"]
  node_node_default_content["Default content<br/>starter content<br/>[defaultContent.ts]"]
end

node_node_main_entry -->|"creates"| node_node_window_manager
node_node_main_entry -->|"guards"| node_node_security
node_node_main_entry -->|"loads state"| node_node_storage
node_node_main_entry -->|"converts"| node_node_markitdown
node_node_main_entry -->|"watches"| node_node_file_watcher
node_node_main_entry -->|"exposes bridge"| node_node_preload
node_node_preload -->|"uses schemas"| node_node_types
node_node_renderer_entry -->|"boots"| node_node_app
node_node_app -->|"composes"| node_node_ui_components
node_node_app -->|"orchestrates"| node_node_hooks
node_node_hooks -->|"calls IPC"| node_node_preload
node_node_hooks -->|"transforms"| node_node_utils
node_node_ui_components -->|"renders data"| node_node_utils
node_node_ui_components -->|"defaults"| node_node_default_content
node_node_markitdown -->|"normalizes"| node_node_utils
node_node_security -->|"validates against"| node_node_types
node_node_file_watcher -->|"notifies"| node_node_preload
node_node_storage -->|"persists via IPC"| node_node_preload
node_node_default_content -->|"seeds"| node_node_ui_components
node_node_utils -->|"shares contracts"| node_node_types

click node_node_main_entry "https://github.com/jwtor7/mdviewer/blob/main/src/main.ts"
click node_node_window_manager "https://github.com/jwtor7/mdviewer/blob/main/src/main/windowManager.ts"
click node_node_file_watcher "https://github.com/jwtor7/mdviewer/blob/main/src/main/fileWatcher.ts"
click node_node_markitdown "https://github.com/jwtor7/mdviewer/blob/main/src/main/markitdown.ts"
click node_node_storage "https://github.com/jwtor7/mdviewer/blob/main/src/main/storage/preferences.ts"
click node_node_security "https://github.com/jwtor7/mdviewer/blob/main/src/main/security/ipcValidation.ts"
click node_node_preload "https://github.com/jwtor7/mdviewer/blob/main/src/preload.ts"
click node_node_renderer_entry "https://github.com/jwtor7/mdviewer/blob/main/src/renderer.tsx"
click node_node_app "https://github.com/jwtor7/mdviewer/blob/main/src/App.tsx"
click node_node_ui_components "https://github.com/jwtor7/mdviewer/blob/main/src/components/MarkdownPreview.tsx"
click node_node_hooks "https://github.com/jwtor7/mdviewer/blob/main/src/hooks/useDocuments.ts"
click node_node_types "https://github.com/jwtor7/mdviewer/blob/main/src/types/ipc-schemas.ts"
click node_node_utils "https://github.com/jwtor7/mdviewer/blob/main/src/utils/textEditing.ts"
click node_node_default_content "https://github.com/jwtor7/mdviewer/blob/main/src/constants/defaultContent.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_node_main_entry,node_node_window_manager,node_node_file_watcher,node_node_markitdown,node_node_storage,node_node_security toneBlue
class node_node_preload toneAmber
class node_node_renderer_entry,node_node_app,node_node_ui_components,node_node_hooks toneMint
class node_node_types,node_node_utils,node_node_default_content toneRose
```

## Components

### Main process

Electron authority: windowing, file I/O, conversion, persistence, and security checks.

- [Main entry](https://github.com/jwtor7/mdviewer/blob/main/src/main.ts): electron main · Electron startup and app wiring. · `src/main.ts`
- [Window manager](https://github.com/jwtor7/mdviewer/blob/main/src/main/windowManager.ts): native windows · Creates and manages windows and lifecycle. · `src/main/windowManager.ts`
- [File watcher](https://github.com/jwtor7/mdviewer/blob/main/src/main/fileWatcher.ts): external change · Detects file changes outside the app. · `src/main/fileWatcher.ts`
- [Conversion](https://github.com/jwtor7/mdviewer/blob/main/src/main/markitdown.ts): ingest pipeline · Converts non-Markdown documents into normalized text/Markdown. · `src/main/markitdown.ts`
- [Storage](https://github.com/jwtor7/mdviewer/blob/main/src/main/storage/preferences.ts): prefs/recent files · Persists preferences and recent-file state. · `src/main/storage/preferences.ts`
- [Security](https://github.com/jwtor7/mdviewer/blob/main/src/main/security/ipcValidation.ts): ipc/path guard · Validates IPC, paths, and request rate. · `src/main/security/ipcValidation.ts`

### Preload bridge

Restricted IPC surface exposed to the renderer.

- [Preload bridge](https://github.com/jwtor7/mdviewer/blob/main/src/preload.ts): IPC bridge · Sandboxed API surface between renderer and main. · `src/preload.ts`

### Renderer

React UI, editing state, and presentation logic.

- [Renderer entry](https://github.com/jwtor7/mdviewer/blob/main/src/renderer.tsx): react boot · Bootstraps the React renderer. · `src/renderer.tsx`
- [App shell](https://github.com/jwtor7/mdviewer/blob/main/src/App.tsx): react shell · Top-level application composition and mode switching. · `src/App.tsx`
- [UI components](https://github.com/jwtor7/mdviewer/blob/main/src/components/MarkdownPreview.tsx): editor views · Markdown preview, code editor, text preview, find/replace, Mermaid, and notifications. · `src/components/MarkdownPreview.tsx`
- [Hooks](https://github.com/jwtor7/mdviewer/blob/main/src/hooks/useDocuments.ts): state orchestration · 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](https://github.com/jwtor7/mdviewer/blob/main/src/types/ipc-schemas.ts): contracts · IPC schemas and shared Electron/document typings. · `src/types/ipc-schemas.ts`
- [Utilities](https://github.com/jwtor7/mdviewer/blob/main/src/utils/textEditing.ts): transform helpers · Text, clipboard, file validation, PDF, and Mermaid-related helpers. · `src/utils/textEditing.ts`
- [Default content](https://github.com/jwtor7/mdviewer/blob/main/src/constants/defaultContent.ts): starter content · Initial editor content and constants. · `src/constants/defaultContent.ts`

## Connections

- 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.
