Drafts Action Directory

Slugify Selection

Posted by @mattgemmell, Last updated: 2018-07-04 14:19:42 UTC

Transforms the selected text into a “slug”, i.e. a URL-safe, lowercase version of itself, with spaces converted to hyphens, and various special characters removed. Useful for filenames and web permalinks.

Install

Steps

  • script

    // Slugify selected text.
    var selectedText = editor.getSelectedText();
    
    var slug = selectedText.toLowerCase()
    				.replace(/["'=:!?@£#$%&\*\(\)\[\]_\+\|;~`<>,\.]+/g, '')
    				.replace(/ +/g,'-');
    
    editor.setSelectedText(slug);

Comments

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