summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWillian Balmant <willchb@gmail.com>2019-04-04 23:03:51 +0000
committerWillian Balmant <willchb@gmail.com>2019-04-04 23:03:51 +0000
commitb6ca523cf7b8f7339d13d730c9fa24e7cac3263b (patch)
tree540dc99188394159fd226ed87a6984ded770c6b0
parent3ccb4d954f4c51f4f3cc77ebd53f21425e0d4d09 (diff)
downloadgitlab-ce-b6ca523cf7b8f7339d13d730c9fa24e7cac3263b.tar.gz
No leading/trailing spaces when generating heading ids (Fixes #57528)
-rw-r--r--lib/banzai/filter/table_of_contents_filter.rb2
1 files changed, 2 insertions, 0 deletions
diff --git a/lib/banzai/filter/table_of_contents_filter.rb b/lib/banzai/filter/table_of_contents_filter.rb
index f2ae17b44fa..d4cf08be0ae 100644
--- a/lib/banzai/filter/table_of_contents_filter.rb
+++ b/lib/banzai/filter/table_of_contents_filter.rb
@@ -18,6 +18,7 @@ module Banzai
# `li` child elements.
class TableOfContentsFilter < HTML::Pipeline::Filter
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
+ LEADING_OR_TRAILING_SPACE_REGEXP = /^\p{Space}|\p{Space}$/
def call
return doc if context[:no_header_anchors]
@@ -31,6 +32,7 @@ module Banzai
if header_content = node.children.first
id = node
.text
+ .gsub(LEADING_OR_TRAILING_SPACE_REGEXP, '') # remove leading and trailing spaces
.downcase
.gsub(PUNCTUATION_REGEXP, '') # remove punctuation
.tr(' ', '-') # replace spaces with dash