ruby-on-railsmarkdownbluecloth

Using BlueCloth how can I turn off markdown for part of the text?


We're using Bluecloth for the text formatting on a rails website but I've run into a problem with some email addresses. When they have double underscores in the address BlueCloth is translating it into EM tags.

In the rails console:

BlueCloth::new("bob_smith@foo.com").to_html
"<p>bob_smith@foo.com</p>"

An email address with a single underscore works fine, but when there are two underscores in the address the underscores are changed to em tags:

BlueCloth::new("bob_jones_smith@foo.com").to_html
"<p>bob<em>jones</em>smith@foo.com</p>"

I need the formatting for the rest of the text but I need away to turn it off for the email addresses. I feel like I used to be able to use a notextile tag but that doesn't seem to work anymore.

BlueCloth::new("<notextile> bob_jones_smith@foo.com </notextile>").to_html
"<p><notextile> bob<em>jones</em>smith@foo.com </notextile></p>"

Anyone know how to deal with this? We're using bluecloth version 2.2.0.


Solution

  • One option is to switch the :relaxed setting to true:

    BlueCloth.new('bob_jones_smith@foo.com', relaxed: true).to_html
    

    The option documentation is available online but the option names and the way they map to the options in the underlying C library is not always obvious.

    If you want to use the default options for text where you need in-word emphasis in some places but not others you can explicitly escape the underscores:

    escaped_email = 'bob_jones_smith@foo.com'.gsub('_', '\_')
    BlueCloth.new(escaped_email).to_html