I am using bb-code in a Rails application for postings and comments. At the moment, I have the following to put a post’s content in a view:
<%= @post.content.bbcode_to_html.html_safe.gsub('<a', '<a rel="nofollow"') %>
What is the best way to convert the bb-code to html and add “nofollow” to all links?
Thanks!
The
bb-rubygem you are using allows for using custom BBCode translations passed as parameters to thebbcode_to_htmlmethod. However, if you really want ALL links to contain therel="nofollow", I think your best bet is going to be monkey patching them gem itself. Based on the BBRuby source, you want to do this:This will rewrite the BBRuby translator to always include a nofollow attribute. I would put this in
config/initializerswith a descriptive filename such asbbruby_nofollow_monkeypatch.rbAs for the
html_safe, I would leave that as is. As I understand it that is a preferred way of doing it and in my opinion it keeps your intent clear. The above monkey patch makes the line in your view more readable: