diff options
author | Achilleas Pipinellis <axil@gitlab.com> | 2023-11-20 14:20:19 +0300 |
---|---|---|
committer | Achilleas Pipinellis <axil@gitlab.com> | 2023-11-21 17:16:03 +0300 |
commit | 52b0c905a3989437bb1cbc334ea55eb89e9c7379 (patch) | |
tree | 3355563144f7f30a4fdfc30d865145e57c13d2a9 | |
parent | 35ccf6fbef4dfc583f818b2df573d3241c1323f7 (diff) |
Add filter to replace full docs URLsaxil-full-docs-urls-to-relative
-rw-r--r-- | Rules | 9 | ||||
-rw-r--r-- | lib/filters/full_docs_urls_to_relative.rb | 17 | ||||
-rw-r--r-- | lib/helpers/generic.rb | 12 | ||||
-rw-r--r-- | lib/helpers/versions.rb | 2 | ||||
-rw-r--r-- | lib/tasks/task_helpers.rb | 2 |
5 files changed, 41 insertions, 1 deletions
@@ -2,6 +2,7 @@ # vi: set ft=ruby : TITLE_FILTER_REGEXP = /(#\s|#{BadgesFilter::BADGES_MARKDOWN_PATTERN})/.freeze +STABLE_VERSION_FORMAT = %r{^(?<major>\d{1,2})\.(?<minor>\d{1,2})$}.freeze preprocess do badges_filter = BadgesFilter.new @@ -65,6 +66,10 @@ preprocess do config[:release_dates] = get_release_dates end +def production_and_stable_branch? + ENV['NANOC_ENV'] == 'production' && stable_version?(ENV.fetch('CI_COMMIT_REF_NAME', nil)) +end + compile '/404.*' do filter :erb layout '/404.*' @@ -81,6 +86,10 @@ ignore '/ee/drawers/*.md' ignore '/operator/adr/*.md' compile '/**/*.md' do + # This filter needs to run before we check if there's a redirect, because we + # also need to to run it in redirected pages as well. + filter :full_docs_urls_to_relative if production_and_stable_branch? + if item[:redirect_to].nil? # If 'toc' is absent in a file's yaml frontmatter, show ToC. diff --git a/lib/filters/full_docs_urls_to_relative.rb b/lib/filters/full_docs_urls_to_relative.rb new file mode 100644 index 00000000..f8611683 --- /dev/null +++ b/lib/filters/full_docs_urls_to_relative.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +module Nanoc::Filters + class FullDocsUrlsToRelative < Nanoc::Filter + identifier :full_docs_urls_to_relative + + # Turn external URLs that start with 'https://docs.gitlab.com' into + # relative URLs. Basically, remove 'https://docs.gitlab.com'. + def run(content, _params = {}) + content.gsub(%r{"(https://docs\.gitlab\.com\S*)"}) do |result| # Fetch all links that include 'https://docs.gitlab.com' in the HTML Document + result.gsub!(%r{https://docs\.gitlab\.com}, '') # Remove 'https://docs.gitlab.com' + + result + end + end + end +end diff --git a/lib/helpers/generic.rb b/lib/helpers/generic.rb index 288cfb30..5bb84848 100644 --- a/lib/helpers/generic.rb +++ b/lib/helpers/generic.rb @@ -1,7 +1,11 @@ # frozen_string_literal: true +require_relative 'versions' + module Nanoc::Helpers module Generic + include Nanoc::Helpers::VersionsDropdown + # # Check if NANOC_ENV is set to production # @@ -20,6 +24,14 @@ module Nanoc::Helpers end # + # Check if NANOC_ENV is set to production and the branch is a stable one. + # For things that should only be built in production of the stable branch. + # + def production_and_stable_branch? + ENV['NANOC_ENV'] == 'production' && stable_version?(ENV.fetch('CI_COMMIT_REF_NAME', nil)) + end + + # # Find the current branch. If CI_COMMIT_BRANCH is not defined, that means # we're working locally, and Git is used to find the branch. # diff --git a/lib/helpers/versions.rb b/lib/helpers/versions.rb index 7605dbb4..ba958574 100644 --- a/lib/helpers/versions.rb +++ b/lib/helpers/versions.rb @@ -23,7 +23,7 @@ module Nanoc::Helpers # At most two digits for major and minor numbers. # def stable_version?(version) - version.match?(STABLE_VERSIONS_REGEX) + version.nil? ? false : version.match?(STABLE_VERSIONS_REGEX) end end end diff --git a/lib/tasks/task_helpers.rb b/lib/tasks/task_helpers.rb index 6882d17e..614e34b3 100644 --- a/lib/tasks/task_helpers.rb +++ b/lib/tasks/task_helpers.rb @@ -5,9 +5,11 @@ require 'json' require 'date' require 'nanoc' require_relative '../helpers/generic' +require_relative '../helpers/versions' class TaskHelpers include Nanoc::Helpers::Generic + include Nanoc::Helpers::VersionsDropdown PRODUCTS = %w[ee omnibus runner charts operator].freeze VERSION_FORMAT = %r{^(?<major>\d{1,2})\.(?<minor>\d{1,2})$}.freeze |