DEV Community

Discussion on: How to create a no-DB blog with Sinatra?

Collapse
 
crispinheneise profile image
Ryan Crispin Heneise • Edited

Here's a blog that's done in Sinatra, similar to what you describe: github.com/cesarsalazar/Sinatra-Ma...

To read the frontmatter, you'd need to read the file and parse out the YAML:

# app.rb
# ...
get '/:article' do
  @content = RDiscount.new( File.open("contents/" + params["article"].gsub("-", "_").concat(".md")).read ).to_html
  @frontmatter = YAML.load(@content)
  @title = @frontmatter['title']
  @excerpt = @frontmatter['excerpt']
end

YAML should load the frontmatter at the beginning of the file and ignore the rest.

Collapse
 
shpigford profile image
Josh Pigford

This is perfect. Thanks Ryan!

Collapse
 
crispinheneise profile image
Ryan Crispin Heneise

I just re-read the code, and realized you'll probably want to load the file into a variable before running RDiscount on it. Otherwise you'll be trying to parse an HTML file instead of the original markdown file.

get '/:article' do
  markdown = File.open("contents/" + params["article"].gsub("-", "_").concat(".md")).read )

  @frontmatter = YAML.load(markdown)
  @title = @frontmatter['title']
  @excerpt = @frontmatter['excerpt']

  @content = RDiscount.new(markdown).to_html
end