Module: MarkDon::ControllerHelpers::ClassMethods

Defined in:
lib/mark_don/controller_helpers.rb

Instance Method Summary collapse

Instance Method Details

#markdown_render(only: nil, except: nil) ⇒ Object

Declares that this controller supports markdown responses. With no arguments, this is a semantic label only — the global around_action already handles all markdown requests. Pass ‘only:` or `except:` to restrict markdown conversion to specific actions.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mark_don/controller_helpers.rb', line 14

def markdown_render(only: nil, except: nil)
  return unless only || except

  skip_around_action :mark_don_handle_markdown_request
  around_action(only: only, except: except) do |controller, action|
    if MarkDon::MARKDOWN_FORMAT_SYMBOLS.include?(controller.request.format.symbol)
      controller.request.format = :html
      action.call
      if controller.performed? && controller.response.successful?
        html     = Array(controller.response_body).join
        markdown = MarkDon::Converter.convert(html)
        controller.response_body          = markdown
        controller.response.content_type  = Mime[:markdown].to_s
      end
    else
      action.call
    end
  end
end