Module: Bridgetown::Filters::URLFilters
- Extended by:
- URLFilters
- Included in:
- Bridgetown::Filters, URLFilters, Tags::Link, Tags::PostUrl
- Defined in:
- bridgetown-core/lib/bridgetown-core/filters/url_filters.rb
Instance Method Summary collapse
-
#absolute_url(input) ⇒ String
Produces an absolute URL based on site.url and site.base_path.
-
#in_locale(input, use_locale = nil) ⇒ String, Array
For string input, adds a prefix of the current site locale to a relative URL, unless it’s a default locale and prefix_current_locale config is false.
-
#relative_url(input) ⇒ String
Produces a URL relative to the domain root based on site.base_path unless it is already an absolute url with an authority (host).
-
#strip_extname(input) ⇒ String
Strips the extension (if present) off a path/URL.
-
#strip_index(input) ⇒ String
Strips trailing
/index.html
from URLs to create pretty permalinks.
Instance Method Details
#absolute_url(input) ⇒ String
Produces an absolute URL based on site.url and site.base_path.
12 13 14 15 |
# File 'bridgetown-core/lib/bridgetown-core/filters/url_filters.rb', line 12 def absolute_url(input) cache = (@context.registers[:cached_absolute_urls] ||= {}) cache[input] ||= compute_absolute_url(input) end |
#in_locale(input, use_locale = nil) ⇒ String, Array
For string input, adds a prefix of the current site locale to a relative URL, unless it’s a default locale and prefix_current_locale config is false. For a resources array input, return a filtered resources array based on the locale.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'bridgetown-core/lib/bridgetown-core/filters/url_filters.rb', line 36 def in_locale(input, use_locale = nil) site = @context.registers[:site] use_locale ||= site.locale # If we're given a collection, filter down and return if input.is_a?(Array) return input.select do |res| res.data[:locale].to_sym == use_locale.to_sym end end if !site.config.prefix_default_locale && use_locale.to_sym == site.config.default_locale return input end return input unless site.config.available_locales.include?(use_locale.to_sym) "#{use_locale}/#{input.to_s.delete_prefix("/")}" end |
#relative_url(input) ⇒ String
Produces a URL relative to the domain root based on site.base_path unless it is already an absolute url with an authority (host).
22 23 24 25 |
# File 'bridgetown-core/lib/bridgetown-core/filters/url_filters.rb', line 22 def relative_url(input) cache = (@context.registers[:cached_relative_urls] ||= {}) cache[input] ||= compute_relative_url(input) end |
#strip_extname(input) ⇒ String
Strips the extension (if present) off a path/URL
71 72 73 74 75 |
# File 'bridgetown-core/lib/bridgetown-core/filters/url_filters.rb', line 71 def strip_extname(input) Pathname.new(input.to_s).then do |path| path.dirname + path.basename(".*") end.to_s end |
#strip_index(input) ⇒ String
Strips trailing /index.html
from URLs to create pretty permalinks
61 62 63 64 65 |
# File 'bridgetown-core/lib/bridgetown-core/filters/url_filters.rb', line 61 def strip_index(input) return if input.nil? || input.to_s.empty? input.sub(%r!/index\.html?$!, "/") end |