Skip to content

Commit ca5afb8

Browse files
p8st0012
andcommitted
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. Co-authored-by: Stan Lo <[email protected]>
1 parent 5e72d0f commit ca5afb8

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

lib/rdoc/generator/darkfish.rb

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

732+
def canonical_url(path)
733+
if path
734+
File.join(@options.canonical_root, path)
735+
else
736+
@options.canonical_root
737+
end
738+
end
739+
732740
def generate_ancestor_list(ancestors, klass)
733741
return '' if ancestors.empty?
734742

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 @options.canonical_root -%>
29+
<% path = current.path if defined?(current) %>
30+
<link rel="canonical" href="<%= canonical_url(path) %>">
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/test_rdoc_generator_darkfish.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,28 @@ 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+
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+
@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+
517539
##
518540
# Asserts that +filename+ has a link count greater than 1 if hard links to
519541
# @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)