Skip to content

Add support for canonical URL link tag - #1354

Merged
st0012 merged 1 commit into
ruby:masterfrom
p8:features/canonical-meta-tag
May 6, 2025
Merged

Add support for canonical URL link tag#1354
st0012 merged 1 commit into
ruby:masterfrom
p8:features/canonical-meta-tag

Conversation

@p8

@p8p8 commented May 1, 2025

Copy link
Copy Markdown
Contributor

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.

canonical_root: https://docs.ruby-lang.org/en/master

This will add the canonical URL link tag to relevant pages:

<linkrel="canonical" href="https://docs.ruby-lang.org/en/master/String.html">

@p8
p8force-pushed the features/canonical-meta-tag branch from 8e8e536 to 2056471CompareMay 1, 2025 10:38
@p8p8 changed the title Add support for canonical URL linkAdd support for canonical URL link tagMay 1, 2025

@st0012st0012 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this idea ❤️ Just some suggestions.

<%- if @options.canonical_root -%>
<% context = klass if defined?(klass) %>
<% context = file if defined?(file) %>
<link rel="canonical" href="<%= canonical_url(context) %>">

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be included in the if statements above? Then we don't need the context conditions separately.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the implementation to use current instead of klass and file to simplify.
If I'd use the if statement I would need to add an else as well, resulting in adding the tag 4 times.

Comment threadtest/rdoc/test_rdoc_generator_darkfish.rb Outdated
content = File.binread("Klass/Inner.html")

assert_include(content, '<link rel="canonical" href="https://docs.ruby-lang.org/en/master/Klass/Inner.html">')
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have another test for file pages?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm sorry I don't see it?

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, didn't push my changes.
Should be present now.

Comment threadlib/rdoc/generator/darkfish.rb Outdated
end

def canonical_url(context)
File.join(@options.canonical_root, context&.path.to_s)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer making canonical_url taking the path already instead of a nilable context. It'll likely be easier to maintain and debug.

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the implementation to take the path instead.

@p8
p8force-pushed the features/canonical-meta-tag branch from ab9dded to ca5afb8CompareMay 1, 2025 10:59
kou
kou approved these changes May 1, 2025

@koukou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@p8
p8force-pushed the features/canonical-meta-tag branch 3 times, most recently from 84858e4 to 0c82bbeCompareMay 1, 2025 17:22
@p8

p8 commented May 1, 2025

Copy link
Copy Markdown
ContributorAuthor

I’ve changed the implementation to have less logic in the template and moved it to RDoc::Markup. This will allow other templates to use it as well.

@p8
p8force-pushed the features/canonical-meta-tag branch from 0c82bbe to f07af27CompareMay 4, 2025 16:34

@st0012st0012 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thank you!

@st0012

Copy link
Copy Markdown
Member

@p8 can you rebase and push again? I'll merge it then, thx

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 <stan001212@gmail.com>
@p8
p8force-pushed the features/canonical-meta-tag branch from f07af27 to cec51e3CompareMay 6, 2025 07:55
@p8

p8 commented May 6, 2025

Copy link
Copy Markdown
ContributorAuthor

Thanks @st0012 ! I've rebased to master.

@st0012
st0012 merged commit c68ae93 into ruby:masterMay 6, 2025
@p8
p8 deleted the features/canonical-meta-tag branch May 6, 2025 17:28
@st0012st0012 mentioned this pull request May 19, 2025
st0012 added a commit that referenced this pull request May 19, 2025
v6.13.1...master
We can then bump `ruby/ruby`'s RDoc to [set canonical
url](#1354).
@st0012

Copy link
Copy Markdown
Member

I've applied this to Ruby's master doc and 3.4 doc.

@p8

p8 commented May 23, 2025

Copy link
Copy Markdown
ContributorAuthor

@st0012 Thanks!

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@p8@st0012@kou