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

Instance Method Details

#absolute_url(input) ⇒ String

Produces an absolute URL based on site.url and site.base_path.

Parameters:

  • input (String)

    the URL to make absolute.

Returns:

  • (String)

    the absolute URL as a String.



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.

Parameters:

  • input (String, Array)

    the relative URL, or an array of resources

  • use_locale (String) (defaults to: nil)

    another locale to use beside the current one (must be in site’s available_locales config)

Returns:

  • (String, Array)

    the prefixed relative URL, or filtered resources



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).

Parameters:

  • input (String)

    the URL to make relative to the domain root

Returns:

  • (String)

    a URL relative to the domain root as a String.



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

Parameters:

  • input (Object)

    value which responds to to_s

Returns:

  • (String)


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

Parameters:

  • input (String)

    the URL with a possible /index.html

Returns:

  • (String)

    a URL with the trailing /index.html removed



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