Sha256: 00f3e84237c211e0a47ab11266e84c9ba6dba2cd8473bd6113b4fc6dc81beaea

Contents?: true

Size: 1.7 KB

Versions: 7

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

require "addressable/uri"

module Jekyll
  module Filters
    module URLFilters
      # Produces an absolute URL based on site.url and site.baseurl.
      #
      # input - the URL to make absolute.
      #
      # Returns the absolute URL as a String.
      def absolute_url(input)
        return if input.nil?
        return input if Addressable::URI.parse(input.to_s).absolute?
        site = @context.registers[:site]
        return relative_url(input) if site.config["url"].nil?
        Addressable::URI.parse(
          site.config["url"].to_s + relative_url(input)
        ).normalize.to_s
      end

      # Produces a URL relative to the domain root based on site.baseurl.
      #
      # input - the URL to make relative to the domain root
      #
      # Returns a URL relative to the domain root as a String.
      def relative_url(input)
        return if input.nil?
        parts = [sanitized_baseurl, input]
        Addressable::URI.parse(
          parts.compact.map { |part| ensure_leading_slash(part.to_s) }.join
        ).normalize.to_s
      end

      # Strips trailing `/index.html` from URLs to create pretty permalinks
      #
      # input - the URL with a possible `/index.html`
      #
      # Returns a URL with the trailing `/index.html` removed
      def strip_index(input)
        return if input.nil? || input.to_s.empty?
        input.sub(%r!/index\.html?$!, "/")
      end

      private

      def sanitized_baseurl
        site = @context.registers[:site]
        site.config["baseurl"].to_s.chomp("/")
      end

      def ensure_leading_slash(input)
        return input if input.nil? || input.empty? || input.start_with?("/")
        "/#{input}"
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
jekyll-3.6.3 lib/jekyll/filters/url_filters.rb
jekyll-docs-3.6.2 lib/jekyll/filters/url_filters.rb
jekyll-docs-3.6.1 lib/jekyll/filters/url_filters.rb
jekyll-3.6.2 lib/jekyll/filters/url_filters.rb
jekyll-3.6.1 lib/jekyll/filters/url_filters.rb
jekyll-3.6.0 lib/jekyll/filters/url_filters.rb
jekyll-3.6.0.pre.beta1 lib/jekyll/filters/url_filters.rb