Skip to content

Commit 280829a

Browse files
committed
Add a nofollow filter for links
1 parent 1aac164 commit 280829a

3 files changed

Lines changed: 25 additions & 4 deletions

File tree

lib/html/pipeline.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Pipeline
3131
autoload :TableOfContentsFilter, 'html/pipeline/toc_filter'
3232
autoload :SVGTeX, 'html/pipeline/svgtex'
3333
autoload :SyntaxHighlightFilter, 'html/pipeline/syntax_highlight_filter'
34+
autoload :NoFollowLinksFilter, 'html/pipeline/no_follow_links_filter'
3435
autoload :RelativeLinksFilter, 'html/pipeline/relative_links_filter'
3536
autoload :CustomLinksFilter, 'html/pipeline/custom_links_filter'
3637
autoload :SanitizationFilter, 'html/pipeline/sanitization_filter'

lib/html/pipeline/linuxfr.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# encoding: utf-8
21
module HTML
32
class Pipeline
43

@@ -8,20 +7,21 @@ class LinuxFr
87
toc_header: "<h2 class=\"sommaire\">Sommaire</h2>\n",
98
svgtex_url: "http://localhost:16000",
109
host: "linuxfr.org"
11-
}
10+
}.freeze
1211

13-
def self.render(text)
12+
def self.render(text, context = {})
1413
pipeline = HTML::Pipeline.new [
1514
HTML::Pipeline::SVGTeX::PreFilter,
1615
HTML::Pipeline::MarkdownFilter,
1716
HTML::Pipeline::SanitizationFilter,
1817
HTML::Pipeline::TableOfContentsFilter,
1918
HTML::Pipeline::SVGTeX::PostFilter,
2019
HTML::Pipeline::SyntaxHighlightFilter,
20+
HTML::Pipeline::NoFollowLinksFilter,
2121
HTML::Pipeline::RelativeLinksFilter,
2222
HTML::Pipeline::CustomLinksFilter,
2323
], CONTEXT
24-
result = pipeline.call text
24+
result = pipeline.call text, context
2525
result[:output].to_s
2626
end
2727

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module HTML
2+
class Pipeline
3+
4+
# Add rel="nofollow" to <a> links if enabled in the context to avoid giving
5+
# SEO juice to potential spam links.
6+
class NoFollowLinksFilter < Filter
7+
8+
def call
9+
return doc unless context[:nofollow]
10+
11+
doc.css("a[href]").each do |element|
12+
element['rel'] = 'nofollow'
13+
end
14+
doc
15+
end
16+
17+
end
18+
19+
end
20+
end

0 commit comments

Comments
 (0)