Skip to content

Instantly share code, notes, and snippets.

@joeybeninghove
Created November 12, 2019 07:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joeybeninghove/087bc47ac96bf5cd81d618d9448e902d to your computer and use it in GitHub Desktop.
Save joeybeninghove/087bc47ac96bf5cd81d618d9448e902d to your computer and use it in GitHub Desktop.
class Post
include ActiveModel::Model
YAML_FRONT_MATTER_REGEXP =
%r{\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)}m.freeze
attr_accessor :title, :date, :html, :filename
def slug
File.basename(filename, ".md").remove(filename[0, 11])
end
def display_date
date.strftime("%b %d %Y")
end
class << self
def find(filepath)
content = File.read(filepath)
if content =~ YAML_FRONT_MATTER_REGEXP
frontmatter = YAML.safe_load(Regexp.last_match.to_s)
html = Kramdown::Document.new($POSTMATCH, input: "GFM").to_html
return Post.new(
title: frontmatter["title"],
date: DateTime.parse(frontmatter["date"]),
html: html,
filename: File.basename(filepath)
)
end
unknown
end
def unknown
Post.new(title: "Unknown", date: Time.now, html: "Not Found")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment