【问题标题】:Textitilize method shows <p> tags on browserTextitilize 方法在浏览器上显示 <p> 标签
【发布时间】:2014-04-18 20:59:19
【问题描述】:

使用此代码
&lt;dd&gt;&lt;%= textilize @box.description %&gt;&lt;/dd&gt; 浏览器显示 &lt;p&gt;description text&lt;/p&gt; 我怎样才能隐藏

标签??

【问题讨论】:

  • 试试&lt;%= textilize(@box.description).html_safe %&gt;

标签: ruby-on-rails ruby redcloth


【解决方案1】:

这取决于您的要求。如果您一般不想要块级标签,可以使用精简模式:

来自文档: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>!"

否则,我已经为降价做了一些事情,我允许一组非常有限的标签,并且只允许在原始文本中有换行符的情况下使用块元素,如下所示:

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

调整以适应!

另请参阅这个旧线程https://www.ruby-forum.com/topic/175551,其中提到了 Rails pre-3 中的一个方法 textilize_without_paragraph() 已移至现已废弃的 gem。也许那里的代码会帮助你: https://github.com/dtrasbo/formatize/blob/master/lib/formatize/helper.rb

编辑:啊,我从 cmets 中看到,此后出现的问题是,您正在看到正在呈现的实际标记文本,并且似乎对使用 html_safe 并将段落标记保留在 HTML 中而不是摆脱它们感到满意完全这是我认为你想要的。那好吧!也许我会把这个答案留给后代。

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多