Add anchor ids to markdown headings
This commit is contained in:
parent
38755f3cae
commit
a6a25e061c
1 changed files with 8 additions and 0 deletions
|
@ -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(' ') : ''
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue