Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Fix Grammar" option to editor context menu #373

Open
benjaminshafii opened this issue Mar 19, 2025 · 2 comments
Open

Add "Fix Grammar" option to editor context menu #373

benjaminshafii opened this issue Mar 19, 2025 · 2 comments
Labels
enhancement New feature or request feature request

Comments

@benjaminshafii
Copy link
Member

Feature Request: Context Menu Grammar Correction

Description

Add a new context menu option in the Obsidian editor that allows users to fix grammar in selected text while preserving their original tone and style.

User Flow

  1. User selects text within the Obsidian editor
  2. User right-clicks to open the context menu
  3. User clicks on "Fix Grammar" option
  4. Plugin corrects grammar mistakes while maintaining the original tone and writing style

Technical Considerations

  • This would require extending the current editor context menu
  • The grammar correction should be intelligent enough to preserve the user's unique writing style and voice
  • Should be implemented in a way that's performant and non-intrusive

User Value

This feature would help users improve their writing quality without disrupting their creative process or changing their personal writing style.

Priority

Medium - This would be a valuable quality-of-life improvement for users who write extensively in Obsidian.

Related Features

  • This could potentially integrate with existing AI capabilities in the plugin
Copy link

linear bot commented Mar 19, 2025

@benjaminshafii
Copy link
Member Author

Technical Implementation Details

After reviewing the codebase, here are some specific implementation details for the "Fix Grammar" context menu feature:

Integration Points

  1. Editor Context Menu:

    • Register for the Obsidian editor-menu event in the registerEventHandlers function
    • Example implementation:
    plugin.registerEvent(
      plugin.app.workspace.on("editor-menu", (menu, editor, info) => {
        // Only add menu item if text is selected
        if (editor.somethingSelected()) {
          menu.addItem((item) => {
            item.setTitle("Fix Grammar")
                .setIcon("check")
                .onClick(() => {
                  // Handle grammar correction
                  fixGrammarPreservingTone(editor, plugin);
                });
          });
        }
      })
    );
  2. Leveraging Existing API Infrastructure:

    • The plugin already has robust text formatting capabilities:
      • formatContentV2(content, formattingInstruction) for basic formatting
      • formatStream(content, formattingInstruction, serverUrl, apiKey, updateCallback) for streaming responses
  3. Grammar Correction Implementation:

    • Create a new function fix-grammar.ts to handle the grammar correction logic
    • Use existing API utilities for making requests to language models
    • Provide a specialized prompt that emphasizes preserving tone and style

Proposed Implementation Steps

  1. Create a new file packages/plugin/handlers/fix-grammar.ts with the core functionality

  2. Add the editor-menu event handler to eventHandlers.ts

  3. Implement the grammar correction function:

    async function fixGrammarPreservingTone(editor: Editor, plugin: FileOrganizer) {
      // Get selected text
      const selectedText = editor.getSelection();
      if (!selectedText) return;
      
      const formattingInstruction = 
        "Fix grammar errors in the text while preserving the original tone, style, and voice. " +
        "Only correct grammatical issues - do not rewrite or restructure the text.";
      
      try {
        // Show subtle notice 
        new Notice("Fixing grammar...", 1000);
        
        // Use existing formatting infrastructure
        const correctedText = await plugin.formatContentV2(
          selectedText,
          formattingInstruction
        );
        
        // Replace the selected text with the corrected version
        editor.replaceSelection(correctedText);
      } catch (error) {
        logger.error("Error fixing grammar:", error);
        new Notice("Failed to fix grammar. Please try again.", 3000);
      }
    }
  4. Handle edge cases like long text selections by using the streaming API instead of the synchronous one

Backend Requirements

  • The backend API will need to handle the grammar correction prompt effectively
  • The instruction to "preserve tone and style" should be properly interpreted by the language model
  • Consider specialized instruction templates specifically optimized for grammar correction

This implementation approach leverages the existing plugin architecture with minimal changes, making it a relatively straightforward feature to implement while providing significant value to users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request
Projects
None yet
Development

No branches or pull requests

1 participant