Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAchilleas Pipinellis <axilleas@axilleas.me>2016-11-23 14:35:17 +0300
committerAchilleas Pipinellis <axilleas@axilleas.me>2016-11-23 16:02:57 +0300
commita91ad32346b4da89196e8f7395c6cb3f480f6085 (patch)
treecec6ddcfd726cb05d7c9d6b8d0fdef8c8c1b049e /lib/helpers_.rb
parent3d43e006883dd53fac61214bde6096aa96947a20 (diff)
Generate anchor links the way Banzai does
Diffstat (limited to 'lib/helpers_.rb')
-rw-r--r--lib/helpers_.rb12
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'>&para;</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