我的rails应用程序使用RDiscount从用户提供的标记文本中生成超文本标记,我注意到锚标记没有rel="nofollow“。这对我来说是一个大问题,因为我的应用程序是向公众开放的。有没有办法启用nofollow链接,或者有更好的解决方案?
谢谢!
发布于 2011-01-16 21:34:19
我认为只有使用Kramdown才能做到这一点,这是一个具有扩展语法的ruby Markdown解析器。然后,您可以这样做,如链接所示:
[link](test.html){:rel='nofollow'}发布于 2011-01-17 08:28:04
同时,我使用了这个技巧,通过重新解析RDiscount输出并为每个锚点添加一个rel="nofollow“:
def markdown(input)
html = RDiscount.new(input).to_html
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.css("a").each do |link|
link['rel'] = 'nofollow'
end
doc.to_html
end尽管我认为这真的应该由markdown解析器来处理。
发布于 2011-10-18 21:14:06
我需要做一些类似的事情,将target="_new"添加到所有链接。使用Kramdown和一个自定义的Kramdown::Converter::Html类解决了这个问题。
定义一个Kramdown::Converter::Html子类(kramdown/converter/my_html.rb在某个自动加载路径中)
class Kramdown::Converter::MyHtml < Kramdown::Converter::Html
def convert_a(el, indent)
el.attr['target'] = '_new'
super
end
end我在app/helpers/application_helper.rb中也有一个视图助手
def markdown(str)
Kramdown::Converter::MyHtml.convert(Kramdown::Document.new(str).root)[0].html_safe
end理想情况下,只使用Kramdown::Document.new(str).to_my_html.html_safe应该是可行的,但我不能让它在rails开发模式下工作,因为Kramdown使用const_defined?来查看转换器是否可用,这不会触发自动加载器。如果你知道如何解决这个问题,请发表评论。
https://stackoverflow.com/questions/4704667
复制相似问题