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.
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.
Add to your Gemfile:
gem 'mark-don'No initializer needed. The gem hooks into Rails automatically via a Railtie.
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
endA 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
.mdextension only works if your routes allow format suffixes. Rails disables them by default in newer apps. Either addformat: trueto the route, or use theAcceptheader instead.
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
endThe 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- Ruby >= 3.0
- Rails >= 6.1
MIT