Drafts Action Directory

Markdown Fenced Code Block

Posted by toniwonkanobi, Last update 3 months ago

This Action wraps selected text in Markdown fenced code blocks (“`).

If there is no selected text, it creates the fenced code block and inserts the cursor in the middle of the blocks—ready for preformatted text entry.

Install

Steps

  • script

    // Apply Markdown fenced code block to selection, or insert ``` if no selection
    
    var sel = editor.getSelectedText();
    var selRange = editor.getSelectedRange();
    
    if (!sel || sel.length == 0) {
      // Two ticks because [read next comment]
      editor.setSelectedText("```" + "\n" + "\n" + "```");
      // ... this will position the cursor in betweetn the two tick marks
      editor.setSelectedRange(selRange[0]+4,0);
    }
    else {
      editor.setSelectedText("```" + "\n" + sel + "\n" + "```");
      editor.setSelectedRange(selRange[0] + 4,0);
    }

Options

  • After Success Default
    Notification Error
    Log Level Error

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.