Project

mark-don

0.0
No release in over 3 years
A Rails gem that converts HTML views to Markdown on-the-fly via format.markdown in respond_to blocks.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

Runtime

 Project Readme

mark-don

Serve any Rails HTML view as Markdown : no templates to write.

When a client requests text/markdown (via Accept header or .md extension), mark-don intercepts the normal HTML render, converts the output on the fly, and returns it with Content-Type: text/markdown. Your existing .html.erb views are reused as-is.

Background

Inspired by this Evil Martians article on making Rails apps visible to LLMs. The .md routes technique they describe is exactly what this gem automates.

Installation

Add to your Gemfile:

gem 'mark-don'

No initializer needed. The gem hooks into Rails automatically via a Railtie.

Usage

Per-action

Add format.markdown to any respond_to block:

class RoomsController < ApplicationController
  def show
    @room = Room.find(params[:id])

    respond_to do |format|
      format.html
      format.markdown
    end
  end
end

A request with Accept: text/markdown or the .md extension triggers the markdown response:

GET /rooms/1.md
GET /rooms/1        # with Accept: text/markdown

Note: the .md extension only works if your routes allow format suffixes. Rails disables them by default in newer apps. Either add format: true to the route, or use the Accept header instead.

Controller macro

To enable markdown for multiple actions without repeating format.markdown everywhere:

class RoomsController < ApplicationController
  markdown_render                  # all actions
  markdown_render only: :show
  markdown_render except: :index
end

Output

The gem converts the <body> content of the rendered HTML to GitHub Flavored Markdown using reverse_markdown under the hood. Only the body is converted — the layout's <head> and surrounding HTML are discarded. <script>, <style>, <meta>, and <link> tags are stripped. Inline styles and unknown elements are dropped; their text content is preserved.

Input:

<h1>My Room</h1>
<p>This is a <strong>great</strong> room with a <a href="/view">nice view</a>.</p>
<ul>
  <li>WiFi included</li>
  <li>Breakfast at 8am</li>
</ul>

Output:

# My Room

This is a **great** room with a [nice view](/view).

- WiFi included
- Breakfast at 8am

Requirements

  • Ruby >= 3.0
  • Rails >= 6.1

License

MIT