Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Kramdown TOC->HTML wrapped in Jekyll

View jekyll-kramtoc.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
require 'kramdown'
 
module Jekyll
module Converters
class Markdown
class KramdownParser
attr_accessor :kramdown
 
DISPATCHER = Hash.new {|h,k| h[k] = "convert_#{k}"}
 
include ::Kramdown::Utils::Html
include ::Kramdown::Parser::Html::Constants
 
# stolen and twiddled from Kramdown::Converter::Html
def inner(e, ind)
result = ''
ind += 2
e.children.each do |inner_e|
result << send(DISPATCHER[inner_e.type], inner_e, ind)
end
result
end
 
# stolen from Kramdown::Converter::Html
def convert_text(e, ind)
escape_html(e.value, :text)
end
 
# much of this is gutted from the innards of Kramdown
def convert_toc(e, ind)
r = ""
r += (" " * ind) + "<li><a href=\"##{e.attr[:id]}\">#{e.value.children.first.value}</a>"
 
if (e.children.length > 1)
r += "<ul>\n"
r += "#{inner(e, ind)}"
r += (" " * ind) + "</ul>"
end
 
r += "</li>\n"
 
r
end
# stolen from Jekyll::Converters::Markdown::KramdownParser
def convert(content)
# Check for use of coderay
if @config['kramdown']['use_coderay']
%w[wrap line_numbers line_numbers_start tab_width bold_every css default_lang].each do |opt|
key = "coderay_#{opt}"
@config['kramdown'][key] = @config['kramdown']['coderay'][key] unless @config['kramdown'].has_key?(key)
end
end
 
@kramdown = Kramdown::Document.new(content, @config["kramdown"].symbolize_keys)
puts inner(@kramdown.to_toc, 0)
@kramdown.to_html
end
 
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.