Simple Markdown Preview Script

Hi,

one of the really cool feaures of the last DT update was that you can import Markdown files as text files. While the Markdown is really nice to be read in plain text, sometimes one would like see, how it looks like as HTML file.

Here is a Preview Markdown in Browser script I found useful for myself. It basically opens a selected Markdown file in DTP as HTML preview in the default browser.


tell application "DEVONthink Pro"
	-- set location of temp dir and of markdown
	set markdownloc to "/usr/local/bin/markdown"
	set tempdir to "$HOME/Library/tmp"
	
	-- set mytext to current entry
	set mytext to plain text of content record
	
	
	try
		-- delete temporary file for conversion
		do shell script "rm " & tempdir & "/test.mdwn.html"
		
		-- insert html header
		do shell script "cat " & tempdir & "/header.html >" & tempdir & "/test.mdwn.html"
	end try
	
	-- convert current entry to html
	set newhtml to do shell script "echo " & quoted form of mytext & " | " & markdownloc & ">>" & tempdir & "/test.mdwn.html"
	
	try
		-- insert html footer
		do shell script "cat " & tempdir & "/footer.html >>" & tempdir & "/test.mdwn.html"
	end try
	
	-- preview in favourite browser
	do shell script "open " & tempdir & "/test.mdwn.html"
end tell

One has to set the path to a valid temporary directory and to markdown to make use of it.

EDIT: I forgot to mention the script attaches header and footer files to the html, if they are present in tempdir.