在 RDiscount 输出中生成 nofollow 链接
我的 Rails 应用程序正在使用 RDiscount 从用户提供的 Markdown 文本生成 HTML,我注意到锚标记没有 rel="nofollow"。这对我来说是一个大问题,因为我的应用程序向公众开放。有没有办法启用nofollow链接,或者有更好的解决方案吗?
谢谢!
My rails app is using RDiscount to generate HTML from user-supplied markdown text, and I noticed that the anchor tags don't have rel="nofollow". This is a big issue for me as my app is open to the public. Is there a way to enable nofollow links, or are there better solutions?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这只有使用 Kramdown 才有可能,它是一个具有扩展功能的 ruby Markdown 解析器句法。然后您将按照链接所示执行此操作:
I think this is possible only with Kramdown, which is a ruby Markdown parser with extended syntax. You would do that then as shown in the link:
与此同时,我正在使用这个 hack,通过重新解析 RDiscount 输出并向每个锚点添加 rel="nofollow" :
尽管我认为这实际上应该由 markdown 解析器处理。
In the mean time, I am using this hack, by re-parsing the RDiscount output and add a rel="nofollow" to each anchor:
Though I think this should really be handled by the markdown parser.
我需要做类似的事情,将
target="_new"
添加到所有链接。使用 Kramdown 和自定义Kramdown::Converter::Html
类解决了这个问题。定义一个
Kramdown::Converter::Html
子类(在某些自动加载路径中的 kramdown/converter/my_html.rb )我在 app/helpers/application_helper.rb 中也有一个视图助手
理想情况下应该可以只需使用
Kramdown::Document.new(str).to_my_html.html_safe
但我无法让它在 Rails 开发模式下工作,因为 Kramdown 使用const_define?
来查看是否转换器可用且不会触发自动加载器。如果您知道如何解决此问题,请发表评论。I needed to do something similar, add
target="_new"
to all links. Solved it using Kramdown and a customKramdown::Converter::Html
class.Define a
Kramdown::Converter::Html
subclass (kramdown/converter/my_html.rb in some autoload path)I also have a view helper in app/helpers/application_helper.rb
Ideally it should be possible to just use
Kramdown::Document.new(str).to_my_html.html_safe
but I can't get it to work in rails development mode as Kramdown usesconst_defined?
to see if a converter is available and that does not trigger the autoloader. Please comment if you know how to fix this.RDiscount 上有一个开放的功能请求,以支持以这种方式修改链接。
它计划用于即将发布的 RDiscount 2.1.5.x 版本之一。
There is an open feature request on RDiscount to support modifying links in this fashion.
It is scheduled for one of the upcoming RDiscount 2.1.5.x releases.