Module: Jekyll::Algolia::Configurator
- Includes:
- Jekyll::Algolia
- Defined in:
- lib/jekyll/algolia/configurator.rb
Overview
Single source of truth for access to configuration variables
Constant Summary collapse
- ALGOLIA_DEFAULTS =
Algolia default values
{ 'extensions_to_index' => nil, 'files_to_exclude' => nil, 'nodes_to_index' => 'p', 'indexing_batch_size' => 1000, 'max_record_size' => 10_000, 'settings' => { # Searchable attributes 'searchableAttributes' => %w[ title headings unordered(content) collection,categories,tags ], # Custom Ranking 'customRanking' => [ 'desc(date)', 'desc(custom_ranking.heading)', 'asc(custom_ranking.position)' ], 'unretrievableAttributes' => [ 'custom_ranking' ], # Highlight 'attributesToHighlight' => %w[ title headings content html collection categories tags ], 'highlightPreTag' => '<em class="ais-Highlight">', 'highlightPostTag' => '</em>', # Snippet 'attributesToSnippet' => %w[ content:55 ], 'snippetEllipsisText' => '…', # Distinct 'distinct' => true, 'attributeForDistinct' => 'url', # Faceting 'attributesForFaceting' => %w[ type searchable(collection) searchable(categories) searchable(tags) searchable(title) ] } }.freeze
Constants included from Jekyll::Algolia
MissingCredentialsError, VERSION
Class Method Summary collapse
-
.algolia(key) ⇒ Object
Public: Get the value of a specific Algolia configuration option, or revert to the default value otherwise.
-
.api_key ⇒ Object
Public: Return the api key.
-
.application_id ⇒ Object
Public: Return the application id.
-
.assert_valid_credentials ⇒ Object
Public: Check that all credentials are set.
-
.config ⇒ Object
Public: Access to the global configuration object.
-
.default_extensions_to_index ⇒ Object
Public: Setting a default values to index only html and markdown files.
-
.default_files_to_exclude ⇒ Object
Public: Setting a default value to ignore index.html/index.md files in the root.
-
.disable_other_plugins(config) ⇒ Object
Public: Disable features from other Jekyll plugins that might interfere with the indexing Note that if other jekyll plugins are defined as part of the :jekyll_plugins group in the Gemfile, we might be able to override them using .load_overwrites in jekyll-algolia.rb.
-
.dry_run? ⇒ Boolean
Public: Returns true if the command is run in verbose mode.
-
.extensions_to_index ⇒ Object
Public: Returns a list of extensions to index.
-
.force_settings? ⇒ Boolean
Public: Returns true if the command should always update the settings.
-
.get(key) ⇒ Object
Public: Get the value of a specific Jekyll configuration option.
-
.index_name ⇒ Object
Public: Return the index name.
-
.index_object_ids_name ⇒ Object
Public: Return the name of the index used to store the object ids.
-
.init(config = nil) ⇒ Object
Public: Init the configurator with the Jekyll config.
-
.settings ⇒ Object
Public: Get the index settings.
-
.verbose? ⇒ Boolean
Public: Returns true if the command is run in verbose mode.
-
.warn_of_deprecated_options ⇒ Object
Public: Check for any deprecated config option and warn the user.
Methods included from Jekyll::Algolia
Class Method Details
.algolia(key) ⇒ Object
Public: Get the value of a specific Algolia configuration option, or revert to the default value otherwise
key - Algolia key to read
Returns the value of this option, or the default value
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/jekyll/algolia/configurator.rb', line 104 def self.algolia(key) config = get('algolia') || {} value = config[key].nil? ? ALGOLIA_DEFAULTS[key] : config[key] # No value found but we have a method to define the default value if value.nil? && respond_to?("default_#{key}") value = send("default_#{key}") end value end |
.api_key ⇒ Object
Public: Return the api key
Will first try to read the ENV variable. Will otherwise try to read the _algolia_api_key file in the Jekyll folder
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/jekyll/algolia/configurator.rb', line 128 def self.api_key # Alway taking the ENV variable first return ENV['ALGOLIA_API_KEY'] if ENV['ALGOLIA_API_KEY'] # Reading from file on disk otherwise source_dir = get('source') if source_dir api_key_file = File.join(source_dir, '_algolia_api_key') if File.exist?(api_key_file) && File.size(api_key_file).positive? return File.open(api_key_file).read.strip end end nil end |
.application_id ⇒ Object
Public: Return the application id
Will first try to read the ENV variable, and fallback to the one configured in Jekyll config
120 121 122 |
# File 'lib/jekyll/algolia/configurator.rb', line 120 def self.application_id ENV['ALGOLIA_APPLICATION_ID'] || algolia('application_id') end |
.assert_valid_credentials ⇒ Object
Public: Check that all credentials are set
Returns true if everything is ok, false otherwise. Will display helpful error messages for each missing credential
172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/jekyll/algolia/configurator.rb', line 172 def self.assert_valid_credentials checks = %w[application_id index_name api_key] checks.each do |check| if send(check.to_sym).nil? Logger.("missing_#{check}") return false end end true end |
.config ⇒ Object
Public: Access to the global configuration object
This is a method around @config so we can mock it in the tests
85 86 87 |
# File 'lib/jekyll/algolia/configurator.rb', line 85 def self.config @config end |
.default_extensions_to_index ⇒ Object
Public: Setting a default values to index only html and markdown files
Markdown files can have many different extensions. We keep the one defined in the Jekyll config
188 189 190 191 |
# File 'lib/jekyll/algolia/configurator.rb', line 188 def self.default_extensions_to_index markdown_ext = get('markdown_ext') || '' ['html'] + markdown_ext.split(',') end |
.default_files_to_exclude ⇒ Object
Public: Setting a default value to ignore index.html/index.md files in the root
Chances are high that the main page is not worthy of indexing (it can be the list of the most recent posts or some landing page without much content). We ignore it by default.
User can still add it by manually specifying a ‘files_to_exclude` to an empty array
202 203 204 205 206 |
# File 'lib/jekyll/algolia/configurator.rb', line 202 def self.default_files_to_exclude extensions_to_index.map do |extension| "index.#{extension}" end end |
.disable_other_plugins(config) ⇒ Object
Public: Disable features from other Jekyll plugins that might interfere with the indexing Note that if other jekyll plugins are defined as part of the :jekyll_plugins group in the Gemfile, we might be able to override them using .load_overwrites in jekyll-algolia.rb. If they are simply required in Gemfile, then we might need to revert their values to nil values from here
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/jekyll/algolia/configurator.rb', line 258 def self.disable_other_plugins(config) # Disable archive pages from jekyll-archives config.delete('jekyll-archives') # Disable pagination from jekyll-paginate config.delete('paginate') # Disable pagination for jekyll-paginate-v2 config['pagination'] = {} unless config['pagination'].is_a?(Hash) config['pagination']['enabled'] = false # Disable autopages for jekyll-paginate-v2 config['autopages'] = {} unless config['autopages'].is_a?(Hash) config['autopages']['enabled'] = false # Disable tags from jekyll-tagging config.delete('tag_page_dir') config.delete('tag_page_layout') config end |
.dry_run? ⇒ Boolean
Public: Returns true if the command is run in verbose mode
When set to true, no indexing operations will be sent to the API
221 222 223 224 225 226 |
# File 'lib/jekyll/algolia/configurator.rb', line 221 def self.dry_run? value = get('dry_run') return true if value == true false end |
.extensions_to_index ⇒ Object
Public: Returns a list of extensions to index
Will use default values or read the algolia.extensions_to_index key. Accepts both an array or a comma-separated list
243 244 245 246 247 248 249 |
# File 'lib/jekyll/algolia/configurator.rb', line 243 def self.extensions_to_index extensions = algolia('extensions_to_index') return [] if extensions.nil? extensions = extensions.split(',') if extensions.is_a? String extensions end |
.force_settings? ⇒ Boolean
Public: Returns true if the command should always update the settings
When set to true, the index settings will always be updated, no matter if they’ve been modified or not
232 233 234 235 236 237 |
# File 'lib/jekyll/algolia/configurator.rb', line 232 def self.force_settings? value = get('force_settings') return true if value == true false end |
.get(key) ⇒ Object
Public: Get the value of a specific Jekyll configuration option
key - Key to read
Returns the value of this configuration option, nil otherwise
94 95 96 |
# File 'lib/jekyll/algolia/configurator.rb', line 94 def self.get(key) config[key] end |
.index_name ⇒ Object
Public: Return the index name
Will first try to read the ENV variable, and fallback to the one configured in Jekyll config
148 149 150 |
# File 'lib/jekyll/algolia/configurator.rb', line 148 def self.index_name ENV['ALGOLIA_INDEX_NAME'] || algolia('index_name') end |
.index_object_ids_name ⇒ Object
Public: Return the name of the index used to store the object ids
153 154 155 |
# File 'lib/jekyll/algolia/configurator.rb', line 153 def self.index_object_ids_name "#{index_name}_object_ids" end |
.init(config = nil) ⇒ Object
Public: Init the configurator with the Jekyll config
config - The config passed by the ‘jekyll algolia` command. Default to the default Jekyll config
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jekyll/algolia/configurator.rb', line 70 def self.init(config = nil) # Use the default Jekyll configuration if none specified. Silence the # warning about no config set Logger.silent { config = Jekyll.configuration } if config.nil? @config = config @config = disable_other_plugins(@config) self end |
.settings ⇒ Object
Public: Get the index settings
This will be a merge of default settings and the one defined in the _config.yml file
161 162 163 164 165 166 |
# File 'lib/jekyll/algolia/configurator.rb', line 161 def self.settings return {} if algolia('settings') == false user_settings = algolia('settings') || {} ALGOLIA_DEFAULTS['settings'].merge(user_settings) end |
.verbose? ⇒ Boolean
Public: Returns true if the command is run in verbose mode
When set to true, more logs will be displayed
211 212 213 214 215 216 |
# File 'lib/jekyll/algolia/configurator.rb', line 211 def self.verbose? value = get('verbose') return true if value == true false end |
.warn_of_deprecated_options ⇒ Object
Public: Check for any deprecated config option and warn the user
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/jekyll/algolia/configurator.rb', line 281 def self. # indexing_mode is no longer used return if algolia('indexing_mode').nil? # rubocop:disable Metrics/LineLength Logger.log('I:') Logger.log('W:[jekyll-algolia] You are using the algolia.indexing_mode option which has been deprecated in v1.1') Logger.log('I: Indexing is now always using an atomic diff algorithm.') Logger.log('I: This option is no longer necessary, you can remove it from your _config.yml') Logger.log('I:') # rubocop:enable Metrics/LineLength end |