Skip to content

Commit c68ae93

Browse files
p8st0012
andauthored
Add support for canonical URL link tag (#1354)
Currently, search engines don't know which version to show for API documentation. For example, searching for "ruby string" in Google will show the documentation for Ruby 2.0 `https://docs.ruby-lang.org/en/2.0.0/String.html` as the top result (for docs.ruby-lang.org). The canonical URL link tag will allow us to set the preferred version: > The canonical URL link tag defines the preferred URL for the current > document, which helps search engines reduce duplicate content. https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Attributes/rel#canonical For example, for the official Ruby documentation we can add the following to `.rdoc_options`. ```yaml canonical_root: https://docs.ruby-lang.org/en/master ``` This will add the canonical URL link tag to relevant pages: ```html <link rel="canonical" href="https://docs.ruby-lang.org/en/master/String.html"> ``` Co-authored-by: Stan Lo <[email protected]>
1 parent b436b3d commit c68ae93

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

lib/rdoc/generator/markup.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ def cvs_url(url, full_path)
5555
end
5656
end
5757

58+
##
59+
# The preferred URL for this object.
60+
61+
def canonical_url
62+
options = @store.options
63+
if path
64+
File.join(options.canonical_root, path.to_s)
65+
else
66+
options.canonical_root
67+
end
68+
end
69+
5870
end
5971

6072
class RDoc::CodeObject

lib/rdoc/generator/template/darkfish/_head.rhtml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<%- end -%>
2626
<%- end -%>
2727

28+
<%- if canonical_url = @options.canonical_root -%>
29+
<% canonical_url = current.canonical_url if defined?(current) %>
30+
<link rel="canonical" href="<%= canonical_url %>">
31+
<%- end -%>
32+
2833
<script type="text/javascript">
2934
var rdoc_rel_prefix = "<%= h asset_rel_prefix %>/";
3035
var index_rel_prefix = "<%= h rel_prefix %>/";

lib/rdoc/options.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,11 @@ class RDoc::Options
373373

374374
attr_accessor :file_path_prefix
375375

376+
##
377+
# The preferred root URL for the documentation
378+
379+
attr_accessor :canonical_root
380+
376381
def initialize(loaded_options = nil) # :nodoc:
377382
init_ivars
378383
override loaded_options if loaded_options
@@ -429,6 +434,7 @@ def init_ivars # :nodoc:
429434
@apply_default_exclude = true
430435
@class_module_path_prefix = nil
431436
@file_path_prefix = nil
437+
@canonical_root = nil
432438
end
433439

434440
def init_with(map) # :nodoc:
@@ -492,6 +498,7 @@ def override(map) # :nodoc:
492498
@webcvs = map['webcvs'] if map.has_key?('webcvs')
493499
@autolink_excluded_words = map['autolink_excluded_words'] if map.has_key?('autolink_excluded_words')
494500
@apply_default_exclude = map['apply_default_exclude'] if map.has_key?('apply_default_exclude')
501+
@canonical_root = map['canonical_root'] if map.has_key?('canonical_root')
495502

496503
@warn_missing_rdoc_ref = map['warn_missing_rdoc_ref'] if map.has_key?('warn_missing_rdoc_ref')
497504

test/rdoc/rdoc_generator_darkfish_test.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,39 @@ def test_meta_tags_for_empty_document
514514
)
515515
end
516516

517+
def test_canonical_url_for_index
518+
@store.options.canonical_root = @options.canonical_root = "https://docs.ruby-lang.org/en/master/"
519+
@g.generate
520+
521+
content = File.binread("index.html")
522+
523+
assert_include(content, '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/">')
524+
end
525+
526+
def test_canonical_url_for_classes
527+
top_level = @store.add_file("file.rb")
528+
top_level.add_class(@klass.class, @klass.name)
529+
inner = @klass.add_class(RDoc::NormalClass, "Inner")
530+
531+
@store.options.canonical_root = @options.canonical_root = "https://docs.ruby-lang.org/en/master/"
532+
@g.generate
533+
534+
content = File.binread("Klass/Inner.html")
535+
536+
assert_include(content, '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/Klass/Inner.html">')
537+
end
538+
539+
def test_canonical_url_for_rdoc_files
540+
top_level = @store.add_file("CONTRIBUTING.rdoc", parser: RDoc::Parser::Simple)
541+
542+
@store.options.canonical_root = @options.canonical_root = "https://docs.ruby-lang.org/en/master/"
543+
@g.generate
544+
545+
content = File.binread("CONTRIBUTING_rdoc.html")
546+
547+
assert_include(content, '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/CONTRIBUTING_rdoc.html">')
548+
end
549+
517550
##
518551
# Asserts that +filename+ has a link count greater than 1 if hard links to
519552
# @tmpdir are supported.

test/rdoc/rdoc_options_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def test_to_yaml
8989
'autolink_excluded_words' => [],
9090
'class_module_path_prefix' => nil,
9191
'file_path_prefix' => nil,
92+
'canonical_root' => nil,
9293
}
9394

9495
assert_equal expected, coder

0 commit comments

Comments
 (0)