RedCloth and ‘target’ for links

I always wondered why RedCloth, or more exact the underlying Textile, does not give you any possibility to define ‘target’ attributes for links.

Searching the web for a solution ended up with no pleasing results. What I found were a bunch of fellows who suffered like I did :(

So I coded this little snippet that does exactly what I wanted. It add to all links “target=’blank’”. I use this method as replacement for texitilize

def redclothify(text, add_blank_targets_to_link = true)
res = RedCloth.new(text).to_html
res.gsub!(/<a/,"<a target='_blank' ") if add_blank_targets_to_link
return res
end

Way far from being perfect, but does just that what I needed. … and maybe some tip for a fellow out there to dig deeper!

4 Responses to “RedCloth and ‘target’ for links”

  1. Ryan says:

    I could be wrong, here, but I think it’s not supported because the “target” attribute isn’t valid XHTML, which is what Textile produces. You could also think about using a javascript solution for this, and ensuring that all links open in new windows when the document is ready.

    You could do something like add a “rel=’external’” to all of your links, and then (using jQuery) do something like this:

    $(document).ready(function() {
    $(”a[@rel='external']“).addClass(”external”).
    .click(function() { window.open($(this).href); return false; });
    });

  2. mercy says:

    Ryan you are right. “target” is not XHTML strict.
    Neither is it quite accessible webdesign if one uses it.

    But, I actually have not tried your code until now: does “window.open” open the page inside a new tab (if the browser option is set?)
    And I am quite sure, this is exactly what most users want.

  3. MARC says:


    PillSpot.org. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. High quality drugs. Order pills online

    Buy:Seroquel.Female Pink Viagra.Lasix.Buspar.Benicar.Female Cialis.Cozaar.Amoxicillin.Ventolin.SleepWell.Wellbutrin SR.Advair.Lipitor.Prozac.Acomplia.Nymphomax.Zetia.Aricept.Zocor.Lipothin….

Leave a Reply

For spam filtering purposes, please copy the number 3108 to the field below: