Action

Scan Text Beautify

Posted by Nicked, Last update almost 2 years ago

UPDATES

almost 2 years ago

Optimize preview

show all updates...

almost 2 years ago

Optimize preview

almost 2 years ago

Fix Preview Issue

When using scan to capture text in iOS, the result text may turn out to be mismatched lines. This JavaScript will do the rematch and beautify the result.

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    let text = editor.getText()
    let textArray = text.split("\n");
    let flNum = textArray[0].length
    let result = ""
    for (let i = 0; i < textArray.length; i++) {
     let ta = textArray[i]
     let diff = flNum-ta.length
     let temp = ta.substr(-1)
     if(diff>3&&(temp.includes(".")||temp.includes("。")||temp.includes("!")||temp.includes("!")||temp.includes("”")||temp.includes("…")))
       ta = ta + "\n\n"
     result = result + ta
    }
    if (preview(result)) {
        draft.saveVersion();
        editor.setText(result);
      } else {
        context.cancel('Canceled by user');
    }
    
    function preview(text) {
      // Test: < > " ' &
      //text = text.replace(/</g, '&lt;');
      text = text.replaceAll("\n","<br>");
      // text = text.replace(/&/g, '&amp;');
      // text = text.replace(/'/g, "&apos;");
      // text = text.replace(/"/g, '&quot;');
      
      if (app.currentThemeMode == 'dark') {
        var css = "body { background: #222; color: #ddd; text-align: justify;}";
      } else {
        var css = "body { background: #fff; color: #444; text-align: justify;}";
      }
      
      let html = `<meta charset="UTF-8" name="viewport" content="width=device-width,initial-scale=1">
      <html><style>${css}</style><body>
      <h3>Result-code: Press 'Continue' to update draft.<h3>
      <hr style="text-align:justify; text-justify:inter-ideograph;">
      <h4>
      <font size="3">
      ${text}
      </font>
      </h4>
      </body></html>`
      let preview = HTMLPreview.create();
      return preview.show(html);
    }
    
  • clipboard

    template
    [[draft]]

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.