Sha256: 25a219047164371bdcf87e2c81fb47eefc7b7f8ef00f2c0a16735af42a04c8a8
Contents?: true
Size: 1.17 KB
Versions: 3
Compression:
Stored size: 1.17 KB
Contents
# frozen_string_literal: true module Bridgetown module FrontMatter module Loaders # Reads YAML-formatted front matter delineated by triple hyphens # # As an example, this resource loads to the hash `{"published": false, # "title": "My post"}`. # # ~~~ # --- # published: false # title: My post # --- # ~~~ class YAML < Base HEADER = %r!\A---[ \t]*\n! BLOCK = %r!#{HEADER.source}(.*?\n?)^((---|\.\.\.)[ \t]*$\n?)!m # Determines whether a given file has YAML front matter # # @param file [String] the path to the file # @return [Boolean] true if the file has YAML front matter, false otherwise def self.header?(file) File.open(file, "rb", &:gets)&.match?(HEADER) || false end # @see {Base#read} def read(file_contents, **) yaml_content = file_contents.match(BLOCK) or return Result.new( content: yaml_content.post_match.lstrip, front_matter: YAMLParser.load(yaml_content[1]), line_count: yaml_content[1].lines.size - 1 ) end end end end end
Version data entries
3 entries across 3 versions & 1 rubygems