I hacked a Markdown Build System that does a pretty barebones Markdown-to-HTML preview. It depends on RVM and Ruby and the rdiscount gem.
Since we donât apparently have access to real environment environment variables in config files, I had to hardcode my paths. Hereâs the code if anyone wants to play around:
Markdown.build-system
{
"cmd": "/Users/alex/.rvm/bin/rvm-exec ~/bin/mdpreview $file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.markdown",
"shell": true
}
~/bin/mdpreview:
#!/usr/bin/env ruby
begin
require 'rdiscount'
rescue LoadError
`gem install rdiscount`
require 'rdiscount'
end
rdiscount="/Users/alex/.rvm/gems/ruby-1.9.2-p290/bin/rdiscount"
input = ARGV[0]
input_dir = File.dirname input
require 'rdiscount'
markdown = RDiscount.new(File.read(input))
html = markdown.to_html
page = <<HTML
<head>
<style>
body {
font-family: georgia, arial, helvetica;
padding: 1em;
}
p {
max-width: 40em;
}
h1 {
background-color: #f1f1fd;
border-bottom: 1px solid blue;
padding: .25em 1em;
margin: 1em -1em .25em;
}
</style>
</head>
<body>
#{html}
HTML
html_file = File.basename(input).gsub(/\.md$/, '.html')
output = "#{input_dir}/#{html_file}"
File.open(output, "w") do |f|
f.write(page)
end
cmd = "open #{output}"
puts `#{cmd}`
Usage:
- select Tools>Build System>Markdown
- hit cmd-B
Problems:
- Uses Ruby, not Python which would be more ST2ish
- Uses RVM; doesnât work with system ruby if youâd need sudo to install gems
- May only work with Ruby 1.9.2
- Hardcoded paths galore
- I wasnât sure where to put the executable file, so I put it in ~/bin
- HTML preview opens in a new browser tab every time
- only works on MacOS, though someone on Linux could probably find an equivalent to âopenâ
- donât know how to get it to automatically recognize markdown files (âselector: source.markdownâ doesnât do it)
- (over)writes foo.html in same dir as foo.md (bug or feature?)