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 ...@@ -18,7 +18,6 @@ module Banzai
# `li` child elements. # `li` child elements.
class TableOfContentsFilter < HTML::Pipeline::Filter class TableOfContentsFilter < HTML::Pipeline::Filter
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u.freeze PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u.freeze
LEADING_OR_TRAILING_SPACE_REGEXP = /^\p{Space}+|\p{Space}+$/.freeze
def call def call
return doc if context[:no_header_anchors] return doc if context[:no_header_anchors]
...@@ -32,7 +31,7 @@ module Banzai ...@@ -32,7 +31,7 @@ module Banzai
if header_content = node.children.first if header_content = node.children.first
id = node id = node
.text .text
.gsub(LEADING_OR_TRAILING_SPACE_REGEXP, '') # remove leading and trailing spaces .strip
.downcase .downcase
.gsub(PUNCTUATION_REGEXP, '') # remove punctuation .gsub(PUNCTUATION_REGEXP, '') # remove punctuation
.tr(' ', '-') # replace spaces with dash .tr(' ', '-') # replace spaces with dash
......
...@@ -59,7 +59,7 @@ describe Banzai::Filter::TableOfContentsFilter do ...@@ -59,7 +59,7 @@ describe Banzai::Filter::TableOfContentsFilter do
end end
it 'removes any leading or trailing spaces' do 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' expect(doc.css('h1 a').first.attr('href')).to eq '#title-with-spaces'
end 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