diff options
author | Achilleas Pipinellis <axilleas@axilleas.me> | 2016-11-23 14:35:17 +0300 |
---|---|---|
committer | Achilleas Pipinellis <axilleas@axilleas.me> | 2016-11-23 16:02:57 +0300 |
commit | a91ad32346b4da89196e8f7395c6cb3f480f6085 (patch) | |
tree | cec6ddcfd726cb05d7c9d6b8d0fdef8c8c1b049e /lib | |
parent | 3d43e006883dd53fac61214bde6096aa96947a20 (diff) |
Generate anchor links the way Banzai does
Diffstat (limited to 'lib')
-rw-r--r-- | lib/helpers_.rb | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/helpers_.rb b/lib/helpers_.rb index 41139639..3de84452 100644 --- a/lib/helpers_.rb +++ b/lib/helpers_.rb @@ -11,6 +11,18 @@ require 'rouge/plugins/redcarpet' class HTML < Redcarpet::Render::HTML include Rouge::Plugins::Redcarpet + PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u + + def header(text, header_level) + # https://gitlab.com/gitlab-org/gitlab-ce/blob/0676c5c7140ccf5b809eddab79b6fb78b7db0a66/lib/banzai/filter/table_of_contents_filter.rb#L29-32 + anchor = text.downcase + anchor.gsub!(PUNCTUATION_REGEXP, '') # remove punctuation + anchor.tr!(' ', '-') # replace spaces with dash + anchor.squeeze!('-') # replace multiple dashes with one + + "<h#{header_level} id='#{anchor}'>#{text} <a class='anchor' href='##{anchor}' title='Permalink'>¶</a></h#{header_level}>" + end + def image(link, title, alt_text) %(<a target="_blank" href="#{link}"><img src="#{link}" title="#{title}" alt="#{alt_text}"/></a>) end |