Set up with Decant and kramdown

Decant is a dependency-free frontmatter-aware framework-agnostic wrapper around a directory of static content (probably Markdown).

Ben Pickles

kramdown (sic, not Kramdown or KramDown, just kramdown) is a free MIT-licensed Ruby library for parsing and converting a superset of Markdown.

kramdown

Use Decant and kramdown to parse static markdown content files through Rails’ router, controller, model and view.

Install Decant and kramdown

Gemfile
...

gem "decant"
gem "kramdown"
gem "kramdown-parser-gfm"

...
bundle

Create a Page model that configures Decant

app/models/page.rb
Page = Decant.define(dir: "content/pages", ext: "md") do
  # Declare frontmatter convenience readers.
  frontmatter :title

  # Add custom methods - it's a standard Ruby class.
  def shouty
    "#{title.upcase}!!!"
  end
end

Create a helper that configures kramdown

app/helpers/application_helper.rb
module ApplicationHelper
  def render_content_from(page)
    Kramdown::Document.new(page.content, input: "GFM").to_html.html_safe
  end
end

Create the controller, view and routes

app/controllers/pages_controller.rb
class PagesController < ApplicationController
  def show
    @page = Page.find(params[:slug])
  end
end
app/views/pages/show.html.erb
<%= render_content_from @page %>
config/routes.rb
Rails.application.routes.draw do
  ...
  root "pages#show", slug: "index"
  get "/*slug", to: "pages#show", as: :page
end

Create a first static page

content/pages/index.md
# Rails Static

Start the development server

dev

Visit localhost:3000 to see the first static page.

Add pages

To add a new web page, just create the corresponding markdown file in content/pages/ folder. Pages can be nested in sub folders.


Commit: Decant and Kramdown


Deploy with Parklife

Start with Rails and Hotwire