Textitilize method shows <p> tags on browser

101 views Asked by At

With this code
<dd><%= textilize @box.description %></dd> browser shows <p>description text</p> How i can hide the

tags??

1

There are 1 answers

0
Tim On

It depends on your requirements. If you don't want block-level tags in general, you can use lite mode:

From the docs: http://redcloth.rubyforge.org/classes/RedCloth/TextileDoc.html

r = RedCloth.new( "And then? She *fell*!", [:lite_mode] )
r.to_html
#=> "And then? She <strong>fell</strong>!"

Otherwise, I've done something for markdown where I've allowed a very restricted set of tags and only allowed block elements if there are newlines in the original text something like this:

def markdown_to_html(markdown_text)
  allowed_tags = %w(a em strong)
  # Only allow block elements if there are line breaks within the text.  This avoids
  # unnecessary <p> elements wrapping short single-line texts.
  allowed_tags += %w(ul ol li p) if markdown_text.strip.index("\n")

  ActionController::Base.helpers.sanitize(Kramdown::Document.new(markdown_text).to_html, :tags => allowed_tags)
end

Tweak to suit!

See also this old thread https://www.ruby-forum.com/topic/175551 which mentions a method textilize_without_paragraph() from Rails pre-3 which moved to a now-abandoned gem. Perhaps the code there would help you: https://github.com/dtrasbo/formatize/blob/master/lib/formatize/helper.rb

EDIT: Ah, I see from comments that have since appeared on the question that you were seeing the actual tag text being rendered and seem to be happy with using html_safe and keeping the paragraph tags in the HTML rather than getting rid of them completely which is what I assumed you wanted. Oh well! Perhaps I'll leave this answer for posterity.