Commit b27b8dc0 authored by Willian Balmant's avatar Willian Balmant

Use strip to remove leading/trailing spaces

Change based on comments in MR #27025
parent 2075ef7d
......@@ -18,7 +18,6 @@ module Banzai
# `li` child elements.
class TableOfContentsFilter < HTML::Pipeline::Filter
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u.freeze
LEADING_OR_TRAILING_SPACE_REGEXP = /^\p{Space}+|\p{Space}+$/.freeze
def call
return doc if context[:no_header_anchors]
......@@ -32,7 +31,7 @@ module Banzai
if header_content = node.children.first
id = node
.text
.gsub(LEADING_OR_TRAILING_SPACE_REGEXP, '') # remove leading and trailing spaces
.strip
.downcase
.gsub(PUNCTUATION_REGEXP, '') # remove punctuation
.tr(' ', '-') # replace spaces with dash
......
......@@ -59,7 +59,7 @@ describe Banzai::Filter::TableOfContentsFilter do
end
it 'removes any leading or trailing spaces' do
doc = filter(header(1, " \r\n \t Title with spaces \r\n\t "))
doc = filter(header(1, " \r\n\tTitle with spaces\r\n\t "))
expect(doc.css('h1 a').first.attr('href')).to eq '#title-with-spaces'
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment