diff options
author | Vasilii Iakliushin <viakliushin@gitlab.com> | 2020-08-19 12:44:04 +0300 |
---|---|---|
committer | Vasilii Iakliushin <viakliushin@gitlab.com> | 2020-08-19 12:44:04 +0300 |
commit | 821415d35c31dbc4f8bd9ae45b9e317f18199fd1 (patch) | |
tree | 999d07f4f5e76163015a1f307b89c266ec7c4ff5 /lib | |
parent | 66d621c02a289bf7f068bf5926f24c840c0a37dc (diff) |
Remove product suffixes from generated anchors
Issue: https://gitlab.com/gitlab-org/gitlab/-/issues/218764
* Update id of headers with product suffixes
* Update href of links with product suffixes
Diffstat (limited to 'lib')
-rw-r--r-- | lib/filters/gitlab_kramdown.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/filters/gitlab_kramdown.rb b/lib/filters/gitlab_kramdown.rb index 5923508f..a7ba987d 100644 --- a/lib/filters/gitlab_kramdown.rb +++ b/lib/filters/gitlab_kramdown.rb @@ -13,6 +13,8 @@ module Nanoc::Filters PATCH + PRODUCT_SUFFIX = /-(core|starter|premium|ultimate)(-only)?/.freeze + # Runs the content through [GitLab Kramdown](https://gitlab.com/brodock/gitlab_kramdown). # Parameters passed to this filter will be passed on to Kramdown. # @@ -27,6 +29,8 @@ module Nanoc::Filters content = with_toc ? TOC_PATCH+raw_content : raw_content document = ::Kramdown::Document.new(content, params) + update_anchors_with_product_suffixes!(document.root.children) + if warning_filters r = Regexp.union(warning_filters) warnings = document.warnings.reject { |warning| r =~ warning } @@ -43,5 +47,39 @@ module Nanoc::Filters document.to_html end + + private + + def update_anchors_with_product_suffixes!(elements) + headers = find_type_elements(:header, elements) + + headers.each do |header| + next unless header.attr['id'].match(PRODUCT_SUFFIX) + + remove_product_suffix!(header, 'id') + + link = find_type_elements(:a, header.children).first + + remove_product_suffix!(link, 'href') if link + end + end + + def remove_product_suffix!(element, attr) + element.attr[attr] = element.attr[attr].gsub(PRODUCT_SUFFIX, '') + end + + def find_type_elements(type, elements) + results = [] + + elements.each do |e| + results.push(e) if type == e.type + + unless e.children.empty? + results.concat(find_type_elements(type, e.children)) + end + end + + results + end end end |