Skip to content

Instantly share code, notes, and snippets.

@BBonifield
Created September 29, 2015 17:37
Show Gist options
  • Save BBonifield/32d6b1f1c51c1a35fa74 to your computer and use it in GitHub Desktop.
Save BBonifield/32d6b1f1c51c1a35fa74 to your computer and use it in GitHub Desktop.
Github flavored markdown template for compiled kramdown markdown
<!DOCTYPE html>
<html>
<head>
<title>Rendered Markdown</title>
<link rel="stylesheet" href="http://yandex.st/highlightjs/6.1/styles/default.min.css"/>
<link rel="stylesheet" href="http://sindresorhus.com/github-markdown-css/github-markdown.css"/>
</head>
<body>
<div class="markdown-body">
<%= body %>
</div>
<script type="text/javascript" src="http://yandex.st/highlightjs/6.1/highlight.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
var hilight_hljs = function(){
$('pre code').each(function(i, e){hljs.highlightBlock(e, ' ');});
};
var hilight_copy = function(){
$('.insert').each(function(index, element){
var e = $(element);
e.html($(e.html() + ' pre').parent().html());
});
};
$(function(){
hilight_hljs();
hilight_copy();
});
</script>
</body>
</html>
@BBonifield
Copy link
Author

Place this file at: ~/.vim-support/kramdown.html.erb

Add this to your .vimrc:

" render markdown preview
map <silent> <Leader>md :w !ruby -e 'require "rubygems"; require "kramdown"; require "erb"; body = Kramdown::Document.new(STDIN.read.chomp, input: "GFM", hard_wrap: false).to_html; print ERB.new(File.read(Dir.home + "/.vim-support/kramdown.html.erb")).result(binding)' > /tmp/mdresult.html; open /tmp/mdresult.html<CR>

@jtrim
Copy link

jtrim commented Sep 29, 2015

YUSSSSS

@jtrim
Copy link

jtrim commented Sep 29, 2015

Works like a charm.

@jtrim
Copy link

jtrim commented Sep 29, 2015

Here's a version of that vim command broken out into a function:

function! g:RenderMarkdown(template_path)
  let rubyscript = '
        \ require "rubygems";
        \ require "kramdown";
        \ require "erb";
        \ body = Kramdown::Document.new(STDIN.read.chomp, input: "GFM", hard_wrap: false).to_html;
        \ print ERB.new(File.read("' . a:template_path . '/.vim/support/kramdown.html.erb")).result(binding)'

  execute "w !ruby -e '" . rubyscript . "' > /tmp/mdresult.html; open /tmp/mdresult.html"
endfunction

map <silent> <Leader>md :call g:RenderMarkdown($HOME)<CR>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment