Ruby on rails 在RDiscount输出中生成nofollow链接
我的rails应用程序正在使用rdisont从用户提供的标记文本生成HTML,我注意到锚标记没有rel=“nofollow”。这对我来说是个大问题,因为我的应用程序是向公众开放的。有没有办法启用nofollow链接,或者有更好的解决方案Ruby on rails 在RDiscount输出中生成nofollow链接,ruby-on-rails,ruby,markdown,rdiscount,Ruby On Rails,Ruby,Markdown,Rdiscount,我的rails应用程序正在使用rdisont从用户提供的标记文本生成HTML,我注意到锚标记没有rel=“nofollow”。这对我来说是个大问题,因为我的应用程序是向公众开放的。有没有办法启用nofollow链接,或者有更好的解决方案 谢谢 我认为这只能通过使用扩展语法的ruby Markdown解析器来实现。然后,您将按照链接中所示执行此操作: [link](test.html){:rel='nofollow'} 同时,我正在使用这种方法,通过重新解析RDiscount输出并向每个锚添加r
谢谢 我认为这只能通过使用扩展语法的ruby Markdown解析器来实现。然后,您将按照链接中所示执行此操作:
[link](test.html){:rel='nofollow'}
同时,我正在使用这种方法,通过重新解析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
虽然我认为这应该由标记解析器来处理。我需要做一些类似的事情,将
target=“\u new”
添加到所有链接。使用自定义的Kramdown::Converter::Html
类解决了这个问题
定义Kramdown::Converter::Html
子类(自动加载路径中的Kramdown/Converter/my_Html.rb)
理想情况下,应该可以只使用Kramdown::Document.new(str).来确保我的html.html\u安全
,但我无法让它在rails开发模式下工作,因为Kramdown使用const\u defined?
来查看转换器是否可用,而这不会触发自动加载器。如果您知道如何解决此问题,请发表评论 有一个开放的RDisont支持以这种方式修改链接
它计划在即将发布的RDisont 2.1.5.x版本中发布。实际上,内容将由用户提供,因此我需要一种方法来自动向每个链接添加rel=“nofollow”,作为阻止SEO垃圾邮件的措施。我想是stackoverflow做的。
class Kramdown::Converter::MyHtml < Kramdown::Converter::Html
def convert_a(el, indent)
el.attr['target'] = '_new'
super
end
end
def markdown(str)
Kramdown::Converter::MyHtml.convert(Kramdown::Document.new(str).root)[0].html_safe
end