Drafts Action Directory

Markdown Fenced Code Block

Posted by toniwonkanobi, Last updated: 2018-07-03 17:08:48 UTC

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.



  • 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
    else {
      editor.setSelectedText("```" + "\n" + sel + "\n" + "```");
      editor.setSelectedRange(selRange[0] + 4,0);


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