Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ class ApplicationController < ActionController::Base

# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has.
allow_browser versions: :modern

private
def set_active_storage_url_options
ActiveStorage::Current.url_options = { protocol: request.protocol, host: request.host, port: request.port }
end
end
8 changes: 8 additions & 0 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ def create

def show
@leaves = @book.leaves.active.with_leafables.positioned

respond_to do |format|
format.html
format.md do
set_active_storage_url_options
render plain: @book.to_markdown
end
end
end

def edit
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/leafables_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ def create
end

def show
respond_to do |format|
format.html
format.md do
set_active_storage_url_options
render plain: @leaf.to_markdown
end
end
end

def edit
Expand Down
7 changes: 7 additions & 0 deletions app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ class Book < ApplicationRecord
def press(leafable, leaf_params)
leaves.create! leaf_params.merge(leafable: leafable)
end

def to_markdown(url_options: ActiveStorage::Current.url_options)
[ "# #{title}", leaves.active.with_leafables.positioned.map { |leaf| leaf.to_markdown(url_options: url_options) } ]
.flatten
.reject(&:blank?)
.join("\n\n")
end
end
4 changes: 4 additions & 0 deletions app/models/leaf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ class Leaf < ApplicationRecord
def slug
title.parameterize.presence || "-"
end

def to_markdown(url_options: ActiveStorage::Current.url_options)
leafable.to_markdown(title: title, url_options: url_options)
end
end
13 changes: 13 additions & 0 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ def html_preview
rendered_html(markdown_source.first(1024))
end

def to_markdown(title:, url_options: ActiveStorage::Current.url_options)
[ "### #{title}", expand_markdown_urls(markdown_source, url_options: url_options) ].join("\n\n")
end

private
def plain_text
html_body = rendered_html(markdown_source)
Expand All @@ -29,4 +33,13 @@ def rendered_html(source)
def markdown_source
body.content.to_s
end

def expand_markdown_urls(content, url_options:)
return content if url_options.blank?

content.gsub(%r{(?<=\()\/u\/([^\)\s]+)}) do
slug = Regexp.last_match(1)
Rails.application.routes.url_helpers.action_text_markdown_upload_url(slug, **url_options)
end
end
end
12 changes: 12 additions & 0 deletions app/models/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ class Picture < ApplicationRecord
has_one_attached :image do |attachable|
attachable.variant :large, resize_to_limit: [ 1500, 1500 ]
end

def to_markdown(title:, url_options: ActiveStorage::Current.url_options)
[ "### #{title}", image_markdown(url_options), caption.presence ].compact.join("\n\n")
end

private
def image_markdown(url_options)
return unless image.attached?

url = Rails.application.routes.url_helpers.rails_blob_url(image, **url_options)
"![#{title}](#{url})"
end
end
4 changes: 4 additions & 0 deletions app/models/section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ class Section < ApplicationRecord
def searchable_content
body
end

def to_markdown(title:, url_options: ActiveStorage::Current.url_options)
[ "## #{title}", body.to_s ].join("\n\n")
end
end
13 changes: 13 additions & 0 deletions test/controllers/books_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ class BooksControllerTest < ActionDispatch::IntegrationTest
assert_response :success
end

test "show exports markdown" do
get book_slug_url(books(:handbook), format: :md)

assert_response :success
assert_includes response.body, "# Handbook"
assert_includes response.body, "## The Welcome Section"
assert_includes response.body, "### Welcome to The Handbook!"
assert_includes response.body, "This is _such_ a great handbook."

url = rails_blob_url(pictures(:reading).image, host: "www.example.com")
assert_includes response.body, "![Reading](#{url})"
end

test "show includes OG metadata for public access" do
get book_slug_url(books(:handbook))
assert_response :success
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/leafables_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class LeafablesControllerTest < ActionDispatch::IntegrationTest
assert_select "p", "This is such a great handbook."
end

test "show markdown export" do
get leafable_slug_path(leaves(:welcome_page), format: :md)

assert_response :success
assert_includes response.body, "### Welcome to The Handbook!"
assert_includes response.body, "This is _such_ a great handbook."
end

test "show with public access to a published book" do
sign_out
books(:handbook).update!(published: true)
Expand Down
8 changes: 8 additions & 0 deletions test/controllers/pages_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ class PagesControllerTest < ActionDispatch::IntegrationTest
assert_select "h2", text: /Hello/
end

test "show as markdown" do
get leafable_path(leaves(:welcome_page), format: :md)

assert_response :ok
assert_includes response.body, "### Welcome to The Handbook!"
assert_includes response.body, "This is _such_ a great handbook."
end

test "show sanitizes dangerous content" do
get leafable_path(sample_page_leaf(%(<div id="test"><script>alert("ouch")</script></div>)))

Expand Down