Merge branch 'patch-49' into 'master'

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

Closes #57528

See merge request gitlab-org/gitlab-ce!27025
This commit is contained in:
Sean McGivern 2019-05-06 07:41:10 +00:00
commit ff64584cfa
3 changed files with 11 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
title: Remove leading / trailing spaces from heading when generating header ids
merge_request: 27025
author: Willian Balmant
type: fixed

View File

@ -31,6 +31,7 @@ module Banzai
if header_content = node.children.first
id = node
.text
.strip
.downcase
.gsub(PUNCTUATION_REGEXP, '') # remove punctuation
.tr(' ', '-') # replace spaces with dash

View File

@ -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\tTitle 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'))