Here’s a trick for Marked 2 that allows you to keep a custom export format updated every time you save your file and update the Marked preview. I designed it specifically for updating a Lyx file, but you can use it with any secondary processor that doesn’t necessarily output HTML.

Marked’s Custom Preprocessor functionality allows you to do some work with the Markdown file after Marked has compiled any includes and custom syntax, but before it runs the Markdown processor (or other Custom Processor). All the preprocessor has to do is return plain text ready for conversion, so anything else that happens between is fair game.

If a script returns “NOCUSTOM” on STDOUT (as the one below does), Marked ignores the output entirely, so you don’t even have to echo the original back out. This is especially handy with custom processors as it allows them to check for certain conditions before processing a file, and cancel the operation if it’s not needed.

This code will take the open file and determine its directory path and filename, creating a compiled Markdown version and Lyx companion file every time the source file or any included files update. Save it as a script, make it executable, and then put the path to it in the Custom Preprocessor field in Marked’s Behavior settings.

You’ll need the latest MultiMarkdown binary installed, so grab it if you don’t, and make sure it ends up in /usr/local/bin/multimarkdown.

Here’s the script:

marked_lyx.shraw
"
#!/bin/bash

# A custom preprocessor for Marked 2 that will export two files after Marked
# does initial inclusion and processing, then cancel the processor and have
# Marked resume processing.

# Install the latest version of MultiMarkdown:
# http://fletcherpenney.net/multimarkdown/download/

function main()
{
	# MARKED_PATH contains the full path to the document opened in Marked
	local filepath=$MARKED_PATH

	local dir=`dirname $filepath`
	local filename=`basename $filepath .$MARKED_EXT`

	# create a compiled version of the Markdown in the same folder as the
	# original document
	cat > $dir/$filename-compiled.md

	# create a lyx version in the same folder as the original document
	/usr/local/bin/multimarkdown -t lyx $dir/$filename-compiled.md > $dir/$filename.lyx

	# Returning "NOCUSTOM" tells Marked to skip the processor and resume rendering
	echo "NOCUSTOM"
}

cat | main $@

If you don’t have Marked 2 yet, it has a lot to offer. I’ll be posting more tips and tricks soon!