Sha256: 49e6662f2aed62d02ea42e9a81667463048c08310811f20dd860e74278363518

Contents?: true

Size: 1021 Bytes

Versions: 4

Compression:

Stored size: 1021 Bytes

Contents

require "front_matter_parser"
require_relative "node"
require_relative "relationship"

module Marmerdo
  class MarkdownParser
    def initialize(path, content)
      @path = path
      @content = content
    end

    # @return [Node, nil] the parsed node or nil if the file has no marmerdo front matter.
    def parse
      return nil unless marmerdo_file?

      Node.new(
        path: @path,
        name: marmerdo_matter["name"] || File.basename(@path, ".*"),
        relationships: relationships
      )
    end

    private

    def marmerdo_file?
      front_matter.key?("marmerdo")
    end

    def front_matter
      @front_matter ||= FrontMatterParser::Parser.new(:md).call(@content).front_matter || {}
    end

    def marmerdo_matter
      @marmerdo_matter ||= front_matter["marmerdo"] || {}
    end

    def relationships
      @relationships ||= marmerdo_matter.filter { |k| Relationship::TYPES.include?(k.to_sym) }.map do |type, to|
        Relationship.new(type: type, to: to)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
marmerdo-0.2.3 lib/marmerdo/markdown_parser.rb
marmerdo-0.2.2 lib/marmerdo/markdown_parser.rb
marmerdo-0.2.1 lib/marmerdo/markdown_parser.rb
marmerdo-0.2.0 lib/marmerdo/markdown_parser.rb