Skip to content

Commit 2056471

Browse files
committed
Add support for canonical URL link
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.
1 parent 5e72d0f commit 2056471

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

lib/rdoc/generator/darkfish.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,10 @@ def excerpt(comment)
729729
extracted_text[0...150].tr_s("\n", " ").squeeze(" ")
730730
end
731731

732+
def canonical_url(context)
733+
File.join(@options.canonical_root, context&.path.to_s)
734+
end
735+
732736
def generate_ancestor_list(ancestors, klass)
733737
return '' if ancestors.empty?
734738

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

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

28+
<%- if @options.canonical_root -%>
29+
<% context = klass if defined?(klass) %>
30+
<% context = file if defined?(file) %>
31+
<link rel="canonical" href="<%= canonical_url(context) %>">
32+
<%- end -%>
33+
2834
<script type="text/javascript">
2935
var rdoc_rel_prefix = "<%= h asset_rel_prefix %>/";
3036
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/test_rdoc_generator_darkfish.rb

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

517+
def test_canonical_url_for_index
518+
@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+
525+
end
526+
527+
def test_canonical_url_for_classes
528+
top_level = @store.add_file("file.rb")
529+
top_level.add_class(@klass.class, @klass.name)
530+
inner = @klass.add_class(RDoc::NormalClass, "Inner")
531+
532+
@options.canonical_root = "https://docs.ruby-lang.org/en/master/"
533+
@g.generate
534+
535+
content = File.binread("Klass/Inner.html")
536+
537+
assert_include(content, '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/Klass/Inner.html">')
538+
end
539+
517540
##
518541
# Asserts that +filename+ has a link count greater than 1 if hard links to
519542
# @tmpdir are supported.

test/rdoc/test_rdoc_options.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)