Introduction to Writer
Relevant source files
The following files were used as context for generating this wiki page:
Writer is an open-source, distraction-free text editor specifically designed for macOS using the SwiftUI framework. It provides users with a streamlined environment for Markdown editing, featuring live previews and various export capabilities including HTML, PDF, and RTF formats.
The application aims to serve as a high-quality alternative to proprietary writing applications by combining a clean user interface with powerful underlying tools like Apple's Markdown framework for parsing and a custom syntax-highlighted editor.
Sources: README.md:1-2, CLAUDE.md:5-7, Writer/WriterApp.swift:93-124
Core Architecture and State Management
Writer follows a modern SwiftUI architecture, utilizing the Observation framework for reactive state management. The application logic is centralized in several "Store" classes that are injected into the view hierarchy via the SwiftUI environment.
Primary Components
| Component | Description |
|---|---|
| EditorStore | Manages the file tree, document state, file selection, and persistence (autosave). |
| ThemeStore | Handles preview typography and color palettes. |
| LayoutStore | Manages UI state, such as the visibility of the preview pane. |
| MarkdownRenderer | A utility for converting Markdown AST into styled HTML. |
| MarkdownTextView | A bridge to AppKit's NSTextView for advanced text editing and syntax highlighting. |
Sources: CLAUDE.md:13-33, Writer/WriterApp.swift:11-15
Data Flow Diagram
The following diagram illustrates how the EditorStore acts as the central hub for document data, interacting with the file system and the UI components.
Document Management and File System Integration
Writer utilizes Security-Scoped Bookmarks to maintain access to user-selected folders across application launches. This allows the app to stay within the macOS sandbox while providing a persistent workspace.
File Tree and Navigation
The EditorStore builds a recursive FileNode structure to represent the directory hierarchy. It supports standard file operations such as creation, renaming, and deletion.
- Supported File Types:
.md,.markdown,.txt,.text. - Navigation: Uses
NavigationSplitViewwith a sidebar for folder navigation and a detail view for editing. - Persistence: Implements an autosave mechanism that triggers 350ms after the user stops typing.
Sources: CLAUDE.md:46-52, Writer/EditorStore.swift:34-45, Writer/EditorStore.swift:252-259, Writer/EditorStore.swift:328-338
Markdown Processing and Export Pipeline
The application features a robust Markdown pipeline that handles both real-time editing and multi-format exporting.
Rendering Logic
Writer uses a custom HTMLVisitor (implementing the MarkupVisitor protocol) to traverse the Markdown Abstract Syntax Tree (AST). This visitor converts Markdown elements into standard HTML tags.
Exporting Capabilities
The application provides specialized classes for different output formats, all derived from the rendered HTML.
- HTMLExporter: Generates a standalone HTML document with inline CSS based on the active
PreviewTheme. - PDFExporter: Uses a hidden
WKWebViewto render the HTML content and then generates a PDF usingWKWebView.pdf(). - RTFExporter: Converts the HTML string into an
NSAttributedStringand exports it as Rich Text Format.
Sources: Writer/HTMLExporter.swift:12-16, Writer/PDFExporter.swift:24-40, Writer/RTFExporter.swift:16-30
User Interface and Editor Features
The editing experience is powered by MarkdownTextView, which wraps an NSTextView to provide performance and native features not available in standard SwiftUI TextField.
Syntax Highlighting
A custom internal text view (MarkdownTextViewInternal) uses Regular Expressions to apply styling to the NSTextStorage in real-time.
Sources: Writer/MarkdownTextView.swift:94-142
Table Formatting
A dedicated TableFormatter utility detects Markdown tables and aligns columns for better readability. It accounts for visual widths, including CJK characters and emojis.
- Function:
formatEntireDocument(_:) - Logic: Detects table ranges, calculates maximum column widths, and pads cells with spaces to create perfectly aligned ASCII tables.
Sources: Writer/TableFormatter.swift:11-30, Writer/TableFormatter.swift:154-170
Summary
Writer is structured to provide a high-performance Markdown editing experience on macOS. By leveraging the Observation framework for state, Security-Scoped Bookmarks for file access, and a custom Visitor-based rendering pipeline, it ensures that the writing process is both distraction-free and technically robust. Its modular design allows for clean separation between UI state, document persistence, and export logic.
Sources: CLAUDE.md:10-14, Writer/EditorStore.swift:139-158, Writer/MarkdownRenderer.swift:150-161