Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-docs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorSuzanne Selhorn <sselhorn@gitlab.com>2022-09-23 18:45:59 +0300
committerSuzanne Selhorn <sselhorn@gitlab.com>2022-09-23 18:45:59 +0300
commit1eb30e77f647b33b18447f3752049dcd8dfb5ae9 (patch)
tree2e8595df39e1945ef452ecdb240cc9ac07c7eeed /lib
parent0424099e5bad12b60aa91c7f087217fb83988fc6 (diff)
parent9d4f6d4f179ccfb8bf60c14ffdd61e9a67584971 (diff)
Merge branch '1130-algolia-custom-ranking' into 'main'
Add page ranking capabilities for Algolia search Closes #1130 See merge request https://gitlab.com/gitlab-org/gitlab-docs/-/merge_requests/2976 Merged-by: Suzanne Selhorn <sselhorn@gitlab.com> Approved-by: Suzanne Selhorn <sselhorn@gitlab.com> Approved-by: David O'Regan <doregan@gitlab.com> Co-authored-by: Sarah German <sgerman@gitlab.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/helpers/algolia_rank.rb50
-rw-r--r--lib/helpers_.rb1
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