diff options
author | Sarah German <sgerman@gitlab.com> | 2022-09-23 18:45:58 +0300 |
---|---|---|
committer | Suzanne Selhorn <sselhorn@gitlab.com> | 2022-09-23 18:45:58 +0300 |
commit | 9d4f6d4f179ccfb8bf60c14ffdd61e9a67584971 (patch) | |
tree | 8a4387b394ea500f345b853df4df5b3b685676a6 /lib | |
parent | 0c7e647a0bfc4d5f61b2ef15d34d02b417e53d18 (diff) |
Add page ranking capabilities for Algolia search
Diffstat (limited to 'lib')
-rw-r--r-- | lib/helpers/algolia_rank.rb | 50 | ||||
-rw-r--r-- | lib/helpers_.rb | 1 |
2 files changed, 51 insertions, 0 deletions
diff --git a/lib/helpers/algolia_rank.rb b/lib/helpers/algolia_rank.rb new file mode 100644 index 000000000..3e14f0d73 --- /dev/null +++ b/lib/helpers/algolia_rank.rb @@ -0,0 +1,50 @@ +# frozen_string_literal: true + +module Nanoc::Helpers + module AlgoliaRank + ### + # Add a URL pattern and weight to CUSTOM_PAGERANKS to modify Algolia search results. + # + # Weights greater than DEFAULT_PAGERANK will boost a page in search results, + # and weights below DEFAULT_PAGERANK will lower its ranking. + # + # The weight value appears in the "docsearch:pageRank" metatag + # on each page that contains the URL pattern. + # + # For example, the "/runner/development/reviewing-gitlab-runner.html" page + # will match "/runner/development" in CUSTOM_PAGERANKS and receive a weight of "20". + # + # @see https://docsearch.algolia.com/docs/record-extractor/#boost-search-results-with-pagerank + ## + DEFAULT_PAGERANK = 100 + CUSTOM_PAGERANKS = { + "/ee/api" => 50, + "/ee/development" => 20, + "/omnibus/development" => 20, + "/runner/development" => 20, + "/charts/development" => 20, + } + + def algolia_rank(item) + if result = CUSTOM_PAGERANKS.keys.find {|k| item.identifier.to_s.include? k} + return CUSTOM_PAGERANKS[result] + else + return DEFAULT_PAGERANK + end + end + + ### + # Calculate the navigation level of a given page. + # We use this on Algolia to increase relevance of higher-level pages. + ### + def algolia_level(item) + path = item.identifier.to_s + level = path.scan("/").count + if path.include? "index.md" + level -= 1 + end + return level + end + + end +end diff --git a/lib/helpers_.rb b/lib/helpers_.rb index 6037d04c3..ed7eaedaf 100644 --- a/lib/helpers_.rb +++ b/lib/helpers_.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +use_helper Nanoc::Helpers::AlgoliaRank use_helper Nanoc::Helpers::EditOnGitLab use_helper Nanoc::Helpers::Generic use_helper Nanoc::Helpers::GitLabKramdown |