Sha256: 42a257eb9f43a862d74427a2352f67c206282a703134260f2b79339c9dae7410

Contents?: true

Size: 1.12 KB

Versions: 48

Compression:

Stored size: 1.12 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|
          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

48 entries across 48 versions & 1 rubygems

Version Path
bridgetown-core-0.21.5 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.4 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.3 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.2 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.1 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.0 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.0.beta4 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.0.beta3 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.0.beta2 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.21.0.beta1 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.20.0 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.19.3 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.19.2 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.19.1 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.19.0 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.18.6 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.18.5 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.18.4 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.18.3 lib/bridgetown-core/tags/link.rb
bridgetown-core-0.18.2 lib/bridgetown-core/tags/link.rb