From b6ca523cf7b8f7339d13d730c9fa24e7cac3263b Mon Sep 17 00:00:00 2001 From: Willian Balmant Date: Thu, 4 Apr 2019 23:03:51 +0000 Subject: No leading/trailing spaces when generating heading ids (Fixes #57528) --- lib/banzai/filter/table_of_contents_filter.rb | 2 ++ 1 file changed, 2 insertions(+) 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 -- cgit v1.2.1 From 2075ef7d071286ad156ca13640336572bb20bceb Mon Sep 17 00:00:00 2001 From: Willian Balmant Date: Wed, 10 Apr 2019 20:50:26 +0000 Subject: No leading/trailing spaces when generating heading ids (Fixes #57528) Update based on comments in MR #27025 --- lib/banzai/filter/table_of_contents_filter.rb | 4 ++-- spec/lib/banzai/filter/table_of_contents_filter_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/banzai/filter/table_of_contents_filter.rb b/lib/banzai/filter/table_of_contents_filter.rb index d4cf08be0ae..8d79f5bd58b 100644 --- a/lib/banzai/filter/table_of_contents_filter.rb +++ b/lib/banzai/filter/table_of_contents_filter.rb @@ -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] diff --git a/spec/lib/banzai/filter/table_of_contents_filter_spec.rb b/spec/lib/banzai/filter/table_of_contents_filter_spec.rb index 7213cd58ea7..f776e21a89e 100644 --- a/spec/lib/banzai/filter/table_of_contents_filter_spec.rb +++ b/spec/lib/banzai/filter/table_of_contents_filter_spec.rb @@ -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')) -- cgit v1.2.1 From b27b8dc0c2e38689f519198ea127b60437e13983 Mon Sep 17 00:00:00 2001 From: Willian Balmant Date: Thu, 11 Apr 2019 16:09:03 +0000 Subject: Use strip to remove leading/trailing spaces Change based on comments in MR #27025 --- lib/banzai/filter/table_of_contents_filter.rb | 3 +-- spec/lib/banzai/filter/table_of_contents_filter_spec.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/banzai/filter/table_of_contents_filter.rb b/lib/banzai/filter/table_of_contents_filter.rb index 8d79f5bd58b..ade4d260be1 100644 --- a/lib/banzai/filter/table_of_contents_filter.rb +++ b/lib/banzai/filter/table_of_contents_filter.rb @@ -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 diff --git a/spec/lib/banzai/filter/table_of_contents_filter_spec.rb b/spec/lib/banzai/filter/table_of_contents_filter_spec.rb index f776e21a89e..4a9880ac85a 100644 --- a/spec/lib/banzai/filter/table_of_contents_filter_spec.rb +++ b/spec/lib/banzai/filter/table_of_contents_filter_spec.rb @@ -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 -- cgit v1.2.1 From ce32f71bd6a6844276bbc6299c5ddc13bac743e4 Mon Sep 17 00:00:00 2001 From: Willian Balmant Date: Sun, 5 May 2019 17:56:52 -0700 Subject: Changelog update for MR #27025, Issue #57528 --- changelogs/unreleased/patch-49.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelogs/unreleased/patch-49.yml diff --git a/changelogs/unreleased/patch-49.yml b/changelogs/unreleased/patch-49.yml new file mode 100644 index 00000000000..2c8af1e5c48 --- /dev/null +++ b/changelogs/unreleased/patch-49.yml @@ -0,0 +1,5 @@ +--- +title: Remove leading / trailing spaces from heading when generating header ids +merge_request: 27025 +author: Willian Balmant +type: fixed -- cgit v1.2.1