Skip to content

Instantly share code, notes, and snippets.

@KeithP
Created November 6, 2023 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KeithP/5f959ca70f0b09a1ce5b5cf4831829f6 to your computer and use it in GitHub Desktop.
Save KeithP/5f959ca70f0b09a1ce5b5cf4831829f6 to your computer and use it in GitHub Desktop.
MarkdownTemplateHandler fixed for Rails 7.1
# config/initializers/markdown_template_handler.rb
module MarkdownTemplateHandler
def self.call( template, source=nil )
source ||= template.source
# This worked prior to Rails 7.1:
# compiled_source = erb.call( template, source )
# But with Rails 7.1 it threw "ActionView::Template::Error: wrong argument type ActionView::OutputBuffer (expected String)"
# Because the '.to_s' had been removed in Rails 7.1 by this commit: https://github.com/rails/rails/commit/ee68644c284aa7512d06b4b5514b1236c1b63f55
# So now you need to add it back to make it work with Redcarpet 3.6:
compiled_source = ActionView::OutputBuffer.new( erb.call( template, source ) )
compiled_source << '.to_s'
"Redcarpet::Markdown.new(Redcarpet::Render::HTML.new).render(begin;#{compiled_source};end).html_safe"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment