Sha256: a25034549a6645d2403b56e117fc227288e86bf363d1af2d9d73d0308f4ef3d1

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

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, ".*"),
        namespace: marmerdo_matter["namespace"],
        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["relationships"]&.map do |relationship|
        Relationship.new(relationship)
      end || []
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
marmerdo-0.3.1 lib/marmerdo/markdown_parser.rb