Is there anyway to format the markdown?

I am switching to drafts and I have had most text editors that I previously used for markdown set to autoformat markdown content using prettier or similar. This is most useful for tables where columns can be padded to align. Is there anyway to do this in Drafts?

Thanks

Prettier runs on node, so if you just want to run that on the Mac, you could probably do something straightforward utilising a shell scripting action step to push the Draft content out to Prettier, and then replace the content on return.

If you did want something cross-platform you could try to Browserify Prettier, find an alternative JavaScript library to do the formatting that is independent of node, or write your own. Then you could include that from a standard script action and process the draft content accordingly.

Hope that helps.

Thanks sylumer,

Finally got round to trying this, browerify doesn’t seem to like prettier and the transpile fails so thought I would try the shell script. This is what I have so far:

const fm = FileManager.createLocal();
const tmpFile = "tmp.md";
const tmpFilePath = fm.basePath + tmpFile

formatDraft();

function formatDraft() {
  writeFile();
  formatFile();
  const formatted = readFile();

  if (formatted) {
    draft.content = formatted;
    draft.update();
  }
}

function writeFile() {
  const result = fm.writeString(tmpFile, draft.content);
  if (!result) {
    console.log(`Write failed: ${fm.lastError}`);
    context.fail();
  }
  return result;
}

function readFile() {
  const result = fm.readString(tmpFile);;
  if (!result) {
    console.log(`Read failed: ${fm.lastError}`);
    context.fail();
  }
  return result;
}

function formatFile() {
  const script = `#!/usr/bin/env bash
prettier --write $1
`;

  const runner = ShellScript.create(script);
  if (runner.execute([tmpFilePath])) {
    console.log(`Shell script succeeded: ${runner.standardOutput}`);
  } else {
    console.log(`Shell script failed: ${runner.standardError}`);
    context.fail();
  }
}

The script is failing with:
Shell script failed: /Users/jp/Library/Application Scripts/com.agiletortoise.Drafts-OSX/Temp/shellscript-619220700.585142.sh: line 2: prettier: command not found

  • On the first run I granted access to run scripts.
  • Prettier is installed globally.
  • I can run prettier from /Users/jp/Library/Application Scripts/com.agiletortoise.Drafts-OSX/Temp/

Any help appreciated.

Check the path variable that is in place when running the script and that prettier is located in the path.

If not, update the path, or use the full path to prettier in the command.

Thanks, this pointed me in the right direction. prettier was in the path but it seems that drafts has a limited path due to the sandbox, strangely it still failed when providing the full path.

Stumbled across this post Shell environment. The answer was to amend the path in the script which worked.

Will publish the action when it is a little more polished.

Thanks!

Action published to https://actions.getdrafts.com/a/1bP

1 Like