Render Markdown with Pandoc

I use Pandoc a lot to convert my Markdown documents to PDF, as it supports the inclusion of Latex snippets.
For me this is especially useful for Math equations.

Thus I created my first Drafts action (macOS only) to convert my Markdown file to a PDF using Pandoc and I wanted to share it here:

As I don’t yet really have a lot of knowledge about how the different parts of a Drafts action work, I copied the sceleton from this post: https://forums.getdrafts.com/t/copy-notes-to-obsidian-folder/8787/5

I then replaced the mv command with pandoc and added an AppleScript at the end to invoke Preview to show the finished pdf (Now removed, see edit).

The only step is a “Script” that has this content:

function exportFile(p_strDestinationPath)
{
	// Write a file to the Drafts area
	let fmLocal = FileManager.createCloud();
	fmLocal.writeString("/convertthisfile.txt", draft.content);

	// Set up a shell script to move the file out of the Drafts area
	let strScript = `#!/bin/zsh
	/usr/local/bin/pandoc "$HOME/Library/Mobile Documents/iCloud~com~agiletortoise~Drafts5/Documents/convertthisfile.txt" --pdf-engine=/Library/TeX/texbin/pdflatex -o "${p_strDestinationPath}"
	open -a Preview "${p_strDestinationPath}"
	`;
	let shScript = ShellScript.create(strScript);

	// Run the script
	if (shScript.execute())
	{
		app.displayInfoMessage("Success");
		return true;
	}
	else
	{
		app.displayErrorMessage("Error: " + shScript.standardError);
		console.log(shScript.standardError);
		return false;
	}
}

exportFile("$HOME/Library/Mobile Documents/com~apple~CloudDocs/Temp/test_output_from_drafts.pdf");

Edit: I had previously used an AppleScript to launch Preview, but @sylumer suggested me using a shell command, which works great and is way faster. Thus I changed this post to reflect that improved version.

1 Like

Does twiddle (~) not work - for the home directory?

Didn’t event think about trying that one, but sadly: No :confused:

1 Like

That’s actually why I asked it as a tentative question. :frowning:

Rather than “fix” the AppleScript, how about just opening the PDF from the command line after you have generated it with pandoc?

open -a Preview "$HOME/Library/Mobile Documents/com~apple~CloudDocs/Temp/test_output_from_drafts.pdf"
3 Likes

Wow, how did I not think of this? Great solution, works like a charm and it’s way faster :)!

2 Likes