From 733147f8a8c9e187c08abb79359048904f376c36 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 26 Sep 2025 08:44:43 +0200 Subject: [PATCH 1/7] Updates GitHub release action to v2 Updates the action used for creating releases to the latest major version. This ensures continued compatibility and access to the newest features and bug fixes provided by the action. --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f2caabe..bfb3b61 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -108,7 +108,7 @@ jobs: - name: Create Release id: create_release - uses: softprops/action-gh-release@v1 + uses: softprops/action-gh-release@v2 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: From bf683b2983f4dcb9c0405fa5d6fe822b9c420bac Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 26 Sep 2025 08:44:48 +0200 Subject: [PATCH 2/7] Updates changelog date for security fix Corrects the date in the changelog to reflect the accurate release date of the security fix. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bc15525..a5f45e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [1.0.2] - 2025-01-21 +## [1.0.2] - 2025-09-21 ### Security - Fix innerHTML usage in settings to prevent XSS vulnerabilities From e90eef0f958b3a64f027f7a717ccafd2e9d2727d Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 26 Sep 2025 08:46:25 +0200 Subject: [PATCH 3/7] Refactors plugin architecture and documentation Improves plugin structure by adopting a modular architecture with separated services, commands, and settings. Updates documentation to reflect the new architecture, settings management, command registration, UI components, and resource cleanup strategies. This change enhances maintainability, testability, and API interaction separation. --- CLAUDE.md | 71 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 8d3a88d..a8ae80b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -48,15 +48,20 @@ Manual testing requires copying build artifacts (`main.js`, `manifest.json`, `st - **Production**: Minified output without sourcemaps ### Plugin Structure -The plugin follows Obsidian's standard plugin architecture: -- **Main Plugin Class**: `MyPlugin` extends `Plugin` from obsidian API +The plugin follows a modular architecture extending Obsidian's standard plugin system: +- **Main Plugin Class**: `FreewritingCleanupPlugin` extends `Plugin` from obsidian API +- **Service Layer**: `CleanupService` handles AI API interactions +- **Command Layer**: `CleanupCommand` implements text processing logic - **Settings System**: Interface-based settings with persistence via `loadData()`/`saveData()` -- **Commands**: Registered via `addCommand()` with unique IDs -- **UI Components**: Modal and settings tab classes -- **Lifecycle Management**: Uses `registerDomEvent()`, `registerInterval()` for cleanup +- **Type Definitions**: Centralized in `types.ts` for consistency +- **Lifecycle Management**: Uses proper initialization and cleanup patterns ### Key Files -- `main.ts`: Plugin entry point containing all core functionality +- `main.ts`: Plugin entry point coordinating services and commands +- `types.ts`: TypeScript interfaces for settings, API responses, and plugin configuration +- `settings.ts`: Settings tab implementation and default configuration +- `services/cleanupService.ts`: AI service integration and API handling +- `commands/cleanupCommand.ts`: Command execution and text processing - `manifest.json`: Plugin metadata (ID, version, compatibility) - `package.json`: npm configuration with build scripts - `esbuild.config.mjs`: Build configuration @@ -66,33 +71,48 @@ The plugin follows Obsidian's standard plugin architecture: ## Plugin Architecture Patterns ### Settings Management -Settings are defined as TypeScript interfaces with default values and persisted automatically: +Settings use the `FreewritingCleanupSettings` interface with modular defaults: ```typescript -interface MyPluginSettings { - mySetting: string; +interface FreewritingCleanupSettings { + apiKey: string; + model: AnthropicModel; + cleanupPrompt: string; + enableCommentary: boolean; + commentaryStyle: CommentaryStyle; + customCommentaryPrompt: string; } -const DEFAULT_SETTINGS: MyPluginSettings = { - mySetting: 'default' +const DEFAULT_SETTINGS: FreewritingCleanupSettings = { + apiKey: '', + model: 'claude-3-5-haiku-latest', + cleanupPrompt: 'I did some free writing. Please correct my jumbled typo mess...', + enableCommentary: false, + commentaryStyle: 'constructive', + customCommentaryPrompt: 'Provide thoughtful feedback...' } ``` ### Command Registration -Commands should have stable IDs and descriptive names: -- Use `addCommand()` for simple commands -- Use `editorCallback` for editor-specific commands -- Use `checkCallback` for conditional commands +Commands use dedicated command classes with service injection: +- Main plugin registers commands via `addCommand()` with stable IDs +- `CleanupCommand` class handles execution logic and editor interaction +- Service dependencies injected during plugin initialization + +### Service Architecture +- `CleanupService` manages Anthropic API integration and connection testing +- Service instances created during plugin load and updated when settings change +- Clean separation between API logic and UI components ### UI Components -- Modals extend `Modal` class with `onOpen()`/`onClose()` lifecycle -- Settings tabs extend `PluginSettingTab` with `display()` method -- Use Obsidian's `Setting` class for form controls +- `FreewritingCleanupSettingTab` extends `PluginSettingTab` with comprehensive configuration +- Custom confirmation modal for destructive operations +- Dynamic UI updates based on setting states (e.g., commentary options) ### Resource Cleanup -Critical for plugin stability: -- Use `registerDomEvent()` for DOM listeners -- Use `registerInterval()` for timers -- Use `registerEvent()` for Obsidian app events +Standard Obsidian plugin lifecycle with service coordination: +- Services initialized in `onload()` with proper dependency injection +- Settings persistence triggers service updates automatically +- Clean separation allows for easy testing and maintenance ## Release Process 1. Update `manifest.json` version (semantic versioning) @@ -102,7 +122,8 @@ Critical for plugin stability: 5. Attach `manifest.json`, `main.js`, and `styles.css` as release assets ## Development Notes -- This is currently the default Obsidian sample plugin template -- All functionality is in `main.ts` - consider splitting into modules for real development -- Plugin demonstrates ribbon icons, status bar, commands, modals, and settings +- Plugin uses modular architecture with separate services, commands, and settings +- Build process bundles all TypeScript modules into single `main.js` artifact +- Service layer enables clean testing and API interaction separation +- Settings system provides comprehensive user configuration with dynamic UI updates - Mobile compatibility controlled by `isDesktopOnly` flag in manifest \ No newline at end of file From fe51d1efd270a5b6487d11c5619f21712ecdf87c Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 26 Sep 2025 08:49:26 +0200 Subject: [PATCH 4/7] Updates license badge to GPL v3 Updates the license badge in the README to reflect the project's licensing under GPL v3. This ensures accurate and up-to-date information regarding the project's licensing terms. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5a4bc74..77aebde 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Freewriting Cleanup [![GitHub release (latest by date)](https://img.shields.io/github/v/release/AlexKucera/freewriting-cleanup?color=blue)](https://github.com/AlexKucera/freewriting-cleanup/releases) -[![License: MIT](https://img.shields.io/github/license/AlexKucera/freewriting-cleanup?color=yellow)](https://opensource.org/licenses/MIT) +[![License: GPL v3](https://img.shields.io/github/license/AlexKucera/freewriting-cleanup?color=yellow)](https://www.gnu.org/licenses/gpl-3.0) [![Downloads](https://img.shields.io/github/downloads/AlexKucera/freewriting-cleanup/total?color=green)](https://github.com/AlexKucera/freewriting-cleanup/releases) [![CodeRabbit Reviews](https://img.shields.io/coderabbit/prs/github/AlexKucera/freewriting-cleanup)](https://coderabbit.ai/) ![](https://img.shields.io/badge/mobile_supported-green?label=obsidian&labelColor=purple) From 565cddf86c242ea949313efd4bfe43cbdb9dab2e Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 26 Sep 2025 08:52:24 +0200 Subject: [PATCH 5/7] Updates Anthropic API to use structured content Adapts the Anthropic client to use the new structured content format for messages and system prompts, ensuring compatibility with the API's expected input structure. This change modifies both the standard request and the test request within the Anthropic client. --- api/anthropicClient.ts | 8 ++++---- types.ts | 10 ++++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/api/anthropicClient.ts b/api/anthropicClient.ts index e256415..cebf5a9 100644 --- a/api/anthropicClient.ts +++ b/api/anthropicClient.ts @@ -69,8 +69,8 @@ You MUST use exactly these markers. Do not deviate from this format.`; const request: AnthropicRequest = { model, max_tokens: ANTHROPIC_LIMITS.DEFAULT_MAX_OUTPUT_TOKENS, - messages: [{ role: 'user', content: userMessage }], - system: systemPrompt + messages: [{ role: 'user', content: [{ type: 'text', text: userMessage }] }], + system: [{ type: 'text', text: systemPrompt }] }; const response = await this.makeRequestWithRetry(request); @@ -100,8 +100,8 @@ You MUST use exactly these markers. Do not deviate from this format.`; const testRequest: AnthropicRequest = { model, max_tokens: 50, - messages: [{ role: 'user', content: 'Hello! Please respond with just "Test successful."' }], - system: 'You are a test assistant. Respond only with "Test successful." and nothing else.' + messages: [{ role: 'user', content: [{ type: 'text', text: 'Hello! Please respond with just "Test successful."' }] }], + system: [{ type: 'text', text: 'You are a test assistant. Respond only with "Test successful." and nothing else.' }] }; try { diff --git a/types.ts b/types.ts index b5a8567..fccce8c 100644 --- a/types.ts +++ b/types.ts @@ -12,14 +12,20 @@ export interface FreewritingCleanupSettings { export interface AnthropicMessage { role: 'user' | 'assistant' | 'system'; - content: string; + content: Array<{ + type: string; + text: string; + }>; } export interface AnthropicRequest { model: string; max_tokens: number; messages: AnthropicMessage[]; - system?: string; + system?: Array<{ + type: string; + text: string; + }>; } export interface AnthropicResponse { From 69a92e02d281fa94f7113c42f49e6fb3c90922c6 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 26 Sep 2025 08:56:33 +0200 Subject: [PATCH 6/7] Validates text selection before processing Refactors text validation to ensure that the cleanup process only proceeds with valid text selections. Removes the insertion position logic as it is no longer needed. --- commands/cleanupCommand.ts | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/commands/cleanupCommand.ts b/commands/cleanupCommand.ts index e8e3c03..85fffc9 100644 --- a/commands/cleanupCommand.ts +++ b/commands/cleanupCommand.ts @@ -24,10 +24,10 @@ export class CleanupCommand { return; } - // Validate text length before proceeding - const limits = CleanupService.isWithinLimits(selectedText); - if (!limits.withinCharacterLimit) { - new Notice(`Selected text is too long (${limits.characterCount} characters). Maximum: 680,000 characters.`); + // Validate text selection before proceeding + const validation = this.validateSelection(selectedText); + if (!validation.isValid) { + new Notice(validation.error || 'Invalid text selection'); return; } @@ -87,19 +87,6 @@ export class CleanupCommand { // MARK: - Helper Methods - private getInsertionPosition(editor: Editor): { line: number; ch: number } { - const selection = editor.listSelections()[0]; - if (selection) { - // Insert after the selection - return { - line: selection.head.line, - ch: editor.getLine(selection.head.line).length - }; - } - - // Fallback to current cursor position - return editor.getCursor(); - } private formatSelectedTextInfo(text: string): string { const characterCount = text.length; From 13b25676724d51ba9e9fdb4bd87481c5486e8af7 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Fri, 26 Sep 2025 08:58:23 +0200 Subject: [PATCH 7/7] Avoids potential null reference error Ensures that the modal element is not null before attempting to append it to the document body or remove it from its parent node. This prevents potential errors if the modal element is not properly initialized. --- settings.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/settings.ts b/settings.ts index e1da8c3..c449d85 100644 --- a/settings.ts +++ b/settings.ts @@ -254,7 +254,7 @@ class ConfirmModal { private title: string; private message: string; private callback: (result: boolean) => void; - private modalEl: HTMLElement; + private modalEl: HTMLElement | null = null; constructor(app: App, title: string, message: string, callback: (result: boolean) => void) { this.app = app; @@ -284,7 +284,9 @@ class ConfirmModal { const confirmButton = buttons.createEl('button', { text: 'Confirm', cls: 'mod-warning' }); confirmButton.onclick = () => this.close(true); - document.body.appendChild(this.modalEl); + if (this.modalEl) { + document.body.appendChild(this.modalEl); + } confirmButton.focus(); } @@ -292,6 +294,7 @@ class ConfirmModal { if (this.modalEl && this.modalEl.parentNode) { this.modalEl.parentNode.removeChild(this.modalEl); } + this.modalEl = null; this.callback(result); } } \ No newline at end of file