I needed to do something similar, add target="_new" to all the links. Solved it using Kramdown and the custom class Kramdown::Converter::Html .
Define a subclass of Kramdown::Converter::Html (kramdown / converter / my_html.rb in some startup path)
class Kramdown::Converter::MyHtml < Kramdown::Converter::Html def convert_a(el, indent) el.attr['target'] = '_new' super end end
I also have a view helper in app / helpers / application_helper.rb
def markdown(str) Kramdown::Converter::MyHtml.convert(Kramdown::Document.new(str).root)[0].html_safe end
Ideally, it should be possible to just use Kramdown::Document.new(str).to_my_html.html_safe , but I can't get it to work in rails development mode, since Kramdown uses const_defined? to see if the converter is available and which does not start the autoloader. Please comment if you know how to fix this.
source share