How to create a no-DB blog with Sinatra?

twitter logo github logo ・1 min read

I'm using Sinatra to build a site and that site will have, among other things, a blog area that I'll infrequently post articles to.

What I'd ideally like to do is have a directory of markdown files that the articles are in and to publish a new article, I'd just add a new markdown file.

The main /articles page would loop through that directory and pull the title from the markdown file's metadata.

title: "This is my title"
excerpt: This is an excerpt from the article

So, what I'm not sure of here is how to approach this. How would I loop through a directory of markdown files and properly display them?

FWIW, I'm using a "classic style" Sinatra app here.

twitter logo DISCUSS (3)
markdown guide

Here's a blog that's done in Sinatra, similar to what you describe:

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

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

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


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 ="contents/" + params["article"].gsub("-", "_").concat(".md")).read )

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

  @content =
Classic DEV Post from Jun 6

How do you onboard a new team member?

My team have a lot of roles to fill this year and recruitment is underway. I'm curious, when you joined your team, what made it a smooth experience?

Josh Pigford profile image

DEV is visited by over 2 million software developers per month. You are welcome to publish here or simply read great content.

Get Started