Action

Paragraphs

Posted by sentar / M. Apgar, Last update 2 months ago

Reformat selected text into logical paragraphs without changing any of the sentences or words.

Steps

  • script

    // Function to chunk text if it's too long
    function chunkText(text, maxLength) {
        const chunks = [];
        let currentChunk = "";
        const sentences = text.match(/[^.!?]+[.!?]+/g) || [text];
        
        for (let sentence of sentences) {
            if ((currentChunk + sentence).length <= maxLength) {
                currentChunk += sentence;
            } else {
                if (currentChunk) chunks.push(currentChunk.trim());
                currentChunk = sentence;
            }
        }
        if (currentChunk) chunks.push(currentChunk.trim());
        return chunks;
    }
    
    // Main function
    const f = () => {
        // Get the selected text and its range
        const selection = editor.getSelectedText();
        const [st, len] = editor.getSelectedRange();
    
        // Setup model name to use
        const models = AnthropicAI.knownModels();
    
        // Create the prompt
        let p = new Prompt();
        p.title = "Claude: Reformat Selection";
        p.addSelect("model", "Model", models, [models[0]], false);
        p.addSelect("result", "Result", ["Replace Selection", "Append to Selection", "Place in Clipboard"], ["Replace Selection"], false);
        p.addButton("Reformat");
    
        if (!p.show()) {
            return false;
        }
    
        // Set the instruction
        const instruction = "Reformat the following text into logical paragraphs without changing any sentences or words";
    
        // Initialize AI
        let ai = new AnthropicAI();
        const model = p.fieldValues["model"][0];
    
        // Process text in chunks if necessary
        const maxChunkSize = 10000; // Adjust based on model's context window
        const chunks = chunkText(selection, maxChunkSize);
        let reformattedText = "";
    
        for (let chunk of chunks) {
            let chatPrompt = `${instruction}: ${chunk}`;
            let answer = ai.quickPrompt(chatPrompt, {"model": model});
            
            if (!answer || answer.length === 0) {
                context.fail();
                return false;
            }
            
            reformattedText += answer + "\n\n";
        }
    
        reformattedText = reformattedText.trim();
    
        // Handle the result based on user selection
        switch(String(p.fieldValues["result"])) {
            case "Replace Selection":
                editor.setSelectedText(reformattedText);
                editor.setSelectedRange(st, reformattedText.length);
                break;
            case "Append to Selection":
                editor.setSelectedText(`${selection}\n\n${reformattedText}`);
                editor.setSelectedRange(st + selection.length + 2, reformattedText.length);
                break;
            default: // clipboard
                app.setClipboard(reformattedText);
                break;
        }
        return true;
    };
    
    if (!f()) {
        context.fail();
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.