Action

Convert Lists to Headers

Posted by kjaymiller, Last update about 6 years ago

Action finds all list items in a draft. Select the items you wish to convert to a header and select the type of header you wish to convert the items to.

Action created by kjaymiller. Follow on Twitter or contact through https://kjaymiller.github.io.

Steps

  • script

    // Adapted from sample originally posted 
    
    /*
      * To Title Case 2.1 – http://individed.com/code/to-title-case/
      * Copyright © 2008–2013 David Gouch. Licensed under the MIT License.
     */
    
    String.prototype.toTitleCase = function(){
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
    
      return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
        if (index > 0 && index + match.length !== title.length &&
          match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
          (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        }
    
        if (match.substr(1).search(/[A-Z]|\../) > -1) {
          return match;
        }
    
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    };
    
  • script

    // See online documentation for examples
    // http://getdrafts.com/scripting
    
    re = /(-( \[x* *\])*) *.+/g
    menu = Prompt.create();
    menu.title = "Select the items you wish to make a header"
    menu.addButton('# H1', '#')
    menu.addButton('## H2', '##')
    menu.addButton('### H1', '##')
    
    var listItems = []
    while ((match=re.exec(draft.content)) != null) {
     listItems.push(match[0])
    }
    
    menu.addSelect('listItems', 'List Items', listItems, listItems, true)
    
    var m = menu.show();
    
    for (let item of menu.fieldValues['listItems']) {
      var location = editor.getText().indexOf(item)
      var length = item.length
      
      switch (item.substring(0,3)) {
      case '- [':
     editor.setTextInRange(location, length, menu.buttonPressed + ' ' + item.substring(6,).toTitleCase())
      break;
      
      default:
     editor.setTextInRange(location, length, menu.buttonPressed + ' ' + item.substring(2,).toTitleCase())
      break;
    }
    }

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.