Sha256: 6238840a60a75b2dd75ddc3c0f5a5511a852451bb5fe683408be05e1da35e58f

Contents?: true

Size: 1.86 KB

Versions: 9

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Jekyll
  module Drops
    class DocumentDrop < Drop
      extend Forwardable

      NESTED_OBJECT_FIELD_BLACKLIST = %w(
        content output excerpt next previous
      ).freeze

      mutable false

      def_delegator :@obj, :relative_path, :path
      def_delegators :@obj, :id, :output, :content, :to_s, :relative_path, :url

      def collection
        @obj.collection.label
      end

      def excerpt
        fallback_data["excerpt"].to_s
      end

      def <=>(other)
        return nil unless other.is_a? DocumentDrop
        cmp = self["date"] <=> other["date"]
        cmp = self["path"] <=> other["path"] if cmp.nil? || cmp.zero?
        cmp
      end

      def previous
        @obj.previous_doc.to_liquid
      end

      def next
        @obj.next_doc.to_liquid
      end

      # Generate a Hash for use in generating JSON.
      # This is useful if fields need to be cleared before the JSON can generate.
      #
      # state - the JSON::State object which determines the state of current processing.
      #
      # Returns a Hash ready for JSON generation.
      def hash_for_json(state = nil)
        to_h.tap do |hash|
          if state && state.depth >= 2
            hash["previous"] = collapse_document(hash["previous"]) if hash["previous"]
            hash["next"]     = collapse_document(hash["next"]) if hash["next"]
          end
        end
      end

      # Generate a Hash which breaks the recursive chain.
      # Certain fields which are normally available are omitted.
      #
      # Returns a Hash with only non-recursive fields present.
      def collapse_document(doc)
        doc.keys.each_with_object({}) do |(key, _), result|
          result[key] = doc[key] unless NESTED_OBJECT_FIELD_BLACKLIST.include?(key)
        end
      end

      private
      def_delegator :@obj, :data, :fallback_data
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
jekyll-3.6.3 lib/jekyll/drops/document_drop.rb
jekyll-3.7.4 lib/jekyll/drops/document_drop.rb
jekyll-3.7.3 lib/jekyll/drops/document_drop.rb
jekyll-3.7.2 lib/jekyll/drops/document_drop.rb
jekyll-3.7.0 lib/jekyll/drops/document_drop.rb
jekyll-docs-3.6.2 lib/jekyll/drops/document_drop.rb
jekyll-docs-3.6.1 lib/jekyll/drops/document_drop.rb
jekyll-3.6.2 lib/jekyll/drops/document_drop.rb
jekyll-3.6.1 lib/jekyll/drops/document_drop.rb