diff options
author | Achilleas Pipinellis <axilleas@axilleas.me> | 2016-12-06 00:15:42 +0300 |
---|---|---|
committer | Achilleas Pipinellis <axilleas@axilleas.me> | 2016-12-06 00:15:42 +0300 |
commit | 32eab0e6871c5d9700f3853eaa2c35847ad2542f (patch) | |
tree | 8fe0d2e9fe29db12652ffd951428811485cf0143 /lib | |
parent | 52c752534164b991d07663be4c01cd18491c16fb (diff) |
Remove any first/last dashes from anchors
There are cases where the title might start/end with a non-alphanumerical
character like a period or a parenthesis. Stripping those characters
fixes the ToC not being the same as the anchor links.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/helpers_.rb | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/lib/helpers_.rb b/lib/helpers_.rb index 19b77dfc..e23e00da 100644 --- a/lib/helpers_.rb +++ b/lib/helpers_.rb @@ -17,7 +17,8 @@ class HTML < Redcarpet::Render::HTML anchor = text.gsub(/\s+/, '-').gsub(/<\/?[^>]*>/, '').downcase # https://github.com/rails/rails/blob/e491b2c06329afb3c989261a2865d2a93c8b84b8/activesupport/lib/active_support/inflector/transliterate.rb#L86 anchor.gsub!(/[^a-z0-9\-_]+/i, '-') - anchor.squeeze!('-') # replace multiple dashes with one + anchor.squeeze!('-') # replace multiple dashes with one + anchor.gsub!(/^-|-$/, '') # remove any first or last dashes %(<h#{header_level} id='#{anchor}'>#{text} <a class='anchor' href='##{anchor}' title='Permalink'></a></h#{header_level}>) end |