Action

JS Beautify

Posted by RoyRogers, Last update almost 5 years ago

Indents blocks of code using two spaces for each level,
by first cleaning all existing tab or space indents.
- Keeps original line spacings to max 1 blank line.
- Keeps ‘} else/else if (…) {‘ on same line.

Steps

  • script

    // JS Beautify
    // RV 2020-03-14 at 22:18
    
    // Indents blocks of code using two spaces for each level,
    // by first cleaning all existing tab or space indents.
    // Keeps original line spacings to max 1 blank line.
    // Keeps '} else/else if (…) {' on same line.
    
    function main() {
      let text = draft.content;
      
      // RegEx pre wash:
      // Remove all indents, both tabs and spaces:
      text = text.replace(/^[ \t]*/gm, '');
      text = text.replace(/[ \t]*$/gm, ''); // trailing spaces
      
      // Multi to double linefeeds:
      text = text.replace(/\n\n+/g, '\n\n');
      // Keep '} else/else if (…) {' on same line:
      text = text.replace(/\n\}\s+else/g, '\n} else');
      text = text.replace(/\n+(function )/g, '\n\n$1');
      
      let cleanCode = cleanLines(text);
      
      if (preview(cleanCode)) {
        draft.saveVersion();
        editor.setText(cleanCode);
      } else {
        context.cancel('Canceled by user');
      }
    }
    
    function cleanLines(text) {
      let lines = text.split('\n')
      let code = new myIndent();
        
      for (let line of lines) {
        if (line.match(/^\/\//))
        {
          code.add(line); // add commented line as it is.
        } else if (line.match(/^}.*?\{$/)) {
          code.outdent();
          code.add(line);
          code.indent();
        } else if (line.match(/\{$/)) {
          code.add(line);
          code.indent();
        } else if (line.match(/^\}/)) {
          code.outdent();
          code.add(line);
        } else {
          code.add(line);
        }
      }
      return code.getCode();
    }
    
    class myIndent {
      constructor() {
        // Indent = '  ' // (two spaces) as recommended for JS.
        this.ind = '  ';
        this.indents = [];
        this.lines = [];
      }
      
      indent() {
        this.indents.push(this.ind);
      }
      
      outdent() {
        this.indents.pop();
      }
      
      add(line) {
        this.lines.push(this.indents.join('') + line);
      }
      
      getCode() {
        return this.lines.join('\n');
      }
    }
    
    function preview(text) {
      // Test: < > " ' &
      text = text.replace(/</g, '&lt;');
      // text = text.replace(/>/g, '&gt;');
      // text = text.replace(/&/g, '&amp;');
      // text = text.replace(/'/g, "&apos;");
      // text = text.replace(/"/g, '&quot;');
      
      if (app.themeMode == 'dark') {
        var css = "body { background: #222; color: #ddd; }";
      } else {
        var css = "body { background: #fff; color: #444; }";
      }
      
      let html = `<html><style>${css}</style><body>
      <h3>Result-code: Press to 'Continue' to update draft.<h3>
      <hr>
      <pre>${text}</pre>
      </body></html>`
      let preview = HTMLPreview.create();
      return preview.show(html);
    }
    
    main();
    
    

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.