Output table of contents on top

This commit is contained in:
Olivier Lacan 2017-04-05 23:04:16 +02:00
parent 032451db76
commit 5ac2bc826d
2 changed files with 18 additions and 3 deletions

View File

@ -116,8 +116,25 @@ set :markdown_engine, :redcarpet
## Override default Redcarpet renderer in order to define a class
class CustomMarkdownRenderer < Redcarpet::Render::HTML
def doc_header
%Q[<nav role="navigation">#{@header}</nav>]
end
def preprocess(full_document)
version_regex = /Version \*\*\d.\d.\d\*\*/
top = full_document.split("<pre").first.split(version_regex).last
bottom = full_document.split("</pre>").last
@header = Redcarpet::Markdown.new(
Redcarpet::Render::HTML_TOC.new(nesting_level: 4),
{ with_toc_data: true }
).render(top + bottom).html_safe
full_document
end
def header(text, header_level)
slug = text.gsub(" ", "-").downcase
slug = text.parameterize
tag_name = "h#{header_level}"
anchor_link = "<a id='#{slug}' class='anchor' href='##{slug}' aria-hidden='true'></a>"
header_tag_open = "<#{tag_name} id='#{slug}'>"

View File

@ -53,8 +53,6 @@
= link_to "help translate ", "https://github.com/olivierlacan/keep-a-changelog/issues"
it.
- content = Haml::Filters::Code.render(yield)
= Redcarpet::Markdown.new(Redcarpet::Render::HTML_TOC.new(nesting_level: 5), { with_toc_data: true }).render(content).html_safe
= yield
%footer.clearfix{role: "banner"}