Commit 2075ef7d authored by Willian Balmant's avatar Willian Balmant

No leading/trailing spaces when generating heading ids (Fixes #57528)

Update based on comments in MR #27025
parent b6ca523c
......@@ -17,8 +17,8 @@ module Banzai
# :toc - String containing Table of Contents data as a `ul` element with
# `li` child elements.
class TableOfContentsFilter < HTML::Pipeline::Filter
PUNCTUATION_REGEXP = /[^\p{Word}\- ]/u
LEADING_OR_TRAILING_SPACE_REGEXP = /^\p{Space}|\p{Space}$/
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]
......
......@@ -58,6 +58,11 @@ describe Banzai::Filter::TableOfContentsFilter do
expect(doc.css('h1 a').first.attr('href')).to eq '#this-header-is-filled-with-punctuation'
end
it 'removes any leading or trailing spaces' do
doc = filter(header(1, " \r\n \t Title with spaces \r\n\t "))
expect(doc.css('h1 a').first.attr('href')).to eq '#title-with-spaces'
end
it 'appends a unique number to duplicates' do
doc = filter(header(1, 'One') + header(2, 'One'))
......
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