Sha256: eff8cdf73949c506dae8d1072b822926395f6153fb41872d96bfe2d91c1f610c

Contents?: true

Size: 1005 Bytes

Versions: 3

Compression:

Stored size: 1005 Bytes

Contents

# frozen_string_literal: true

module Bridgetown
  module FrontMatter
    module Importer
      # Requires klass#content and klass#front_matter_line_count accessors
      def self.included(klass)
        klass.include Bridgetown::FrontMatter::RubyDSL
      end

      def read_front_matter(file_path)
        file_contents = File.read(
          file_path, **Bridgetown::Utils.merged_file_read_opts(Bridgetown::Current.site, {})
        )
        fm_result = nil
        Loaders.for(self).each do |loader|
          fm_result = loader.read(file_contents, file_path:) and break
        end

        if fm_result
          self.content = fm_result.content
          self.front_matter_line_count = fm_result.line_count
          fm_result.front_matter
        elsif is_a?(Layout)
          self.content = file_contents
          {}
        else
          yaml_data = YAMLParser.load_file(file_path)
          (yaml_data.is_a?(Array) ? { rows: yaml_data } : yaml_data)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bridgetown-core-2.0.0.beta3 lib/bridgetown-core/front_matter/importer.rb
bridgetown-core-2.0.0.beta2 lib/bridgetown-core/front_matter/importer.rb
bridgetown-core-2.0.0.beta1 lib/bridgetown-core/front_matter/importer.rb