Created
November 12, 2019 07:34
-
-
Save joeybeninghove/087bc47ac96bf5cd81d618d9448e902d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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