Sha256: 465da4b2a71de570f7462f224755bb13daa321bd45efbd7892740cca1b8ffe12

Contents?: true

Size: 1.28 KB

Versions: 37

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Bridgetown
  module Tags
    class Link < Liquid::Tag
      include Bridgetown::Filters::URLFilters

      class << self
        def tag_name
          name.split("::").last.downcase
        end
      end

      def initialize(tag_name, relative_path, tokens)
        super

        @relative_path = relative_path.strip
      end

      def render(context)
        @context = context
        site = context.registers[:site]
        relative_path = Liquid::Template.parse(@relative_path).render(context)

        site.each_site_file do |item|
          # Resource engine:
          if item.respond_to?(:relative_url) && item.relative_path.to_s == relative_path
            return item.relative_url
          end
          return relative_url(item) if item.relative_path == relative_path
          # This takes care of the case for static files that have a leading /
          return relative_url(item) if item.relative_path == "/#{relative_path}"
        end

        raise ArgumentError, <<~MSG
          Could not find document '#{relative_path}' in tag '#{self.class.tag_name}'.

          Make sure the document exists and the path is correct.
        MSG
      end
    end
  end
end

Liquid::Template.register_tag(Bridgetown::Tags::Link.tag_name, Bridgetown::Tags::Link)

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
bridgetown-core-2.0.0.beta4 lib/bridgetown-core/tags/link.rb
bridgetown-core-2.0.0.beta3 lib/bridgetown-core/tags/link.rb
bridgetown-core-2.0.0.beta2 lib/bridgetown-core/tags/link.rb
bridgetown-core-2.0.0.beta1 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.3.4 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.3.3 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.3.2 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.3.1 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.3.0 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.3.0.beta3 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.3.0.beta2 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.3.0.beta1 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.2.0 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.2.0.beta5 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.2.0.beta4 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.2.0.beta3 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.2.0.beta2 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.2.0.beta1 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.1.0 lib/bridgetown-core/tags/link.rb
bridgetown-core-1.1.0.beta3 lib/bridgetown-core/tags/link.rb