Add anchor ids to markdown headings

This commit is contained in:
Jonathan Rudenberg 2012-10-27 15:56:14 -04:00
parent 38755f3cae
commit a6a25e061c

View file

@ -11,8 +11,16 @@ class MarkdownHTML < Redcarpet::Render::HTML
el('table', el('thead', header) + el('tbody', body), class: 'table table-striped table-bordered') el('table', el('thead', header) + el('tbody', body), class: 'table table-striped table-bordered')
end end
def header(text, level)
el("h#{level}", text, id: anchor(text))
end
private private
def anchor(text)
text.downcase.strip.gsub(/[^a-z0-9 ]/, '').gsub(/\s+/, '-')
end
def el(el, content, attributes = {}) def el(el, content, attributes = {})
if content if content
attrs = attributes ? ' ' + attributes.map { |k,v| "#{k}=\"#{v}\"" }.join(' ') : '' attrs = attributes ? ' ' + attributes.map { |k,v| "#{k}=\"#{v}\"" }.join(' ') : ''