Generate nofollow links in RDiscount output

My rails application uses RDiscount to generate HTML from user-provided markup text, and I noticed that there are no rel = "nofollow" in the anchor tags. This is a big problem for me, as my application is open to the public. Is there a way to include nofollow links or are there any better solutions?

Thanks!

+4
source share
4 answers

I think this is only possible with Kramdown , which is a ruby ​​Markdown analyzer with extended syntax. You would do this as shown in the link:

[link](test.html){:rel='nofollow'} 
+3
source

At the same time, I use this hack by re-parsing the output of RDiscount and adding rel = "nofollow" for each anchor:

 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 

Although I think that this should really be handled by the markdown parser.

+2
source

I needed to do something similar, add target="_new" to all the links. Solved it using Kramdown and the custom class Kramdown::Converter::Html .

Define a subclass of Kramdown::Converter::Html (kramdown / converter / my_html.rb in some startup path)

 class Kramdown::Converter::MyHtml < Kramdown::Converter::Html def convert_a(el, indent) el.attr['target'] = '_new' super end end 

I also have a view helper in app / helpers / application_helper.rb

 def markdown(str) Kramdown::Converter::MyHtml.convert(Kramdown::Document.new(str).root)[0].html_safe end 

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, since Kramdown uses const_defined? to see if the converter is available and which does not start the autoloader. Please comment if you know how to fix this.

+1
source

RDiscount has an open property request to support link modifications in this way.

It is planned for one of the upcoming releases of RDiscount 2.1.5.x.

0
source

Source: https://habr.com/ru/post/1335717/


All Articles