Skip to content

Add support for canonical URL link tag #1354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 6, 2025

Conversation

p8
Copy link
Contributor

@p8 p8 commented May 1, 2025

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:

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

@p8 p8 force-pushed the features/canonical-meta-tag branch from 8e8e536 to 2056471 Compare May 1, 2025 10:38
@p8 p8 changed the title Add support for canonical URL link Add support for canonical URL link tag May 1, 2025
Copy link
Member

@st0012 st0012 left a comment

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
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
Contributor Author

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.

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
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
Contributor Author

Choose a reason for hiding this comment

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

Done!

Copy link
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
Contributor Author

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.

@@ -729,6 +729,10 @@ def excerpt(comment)
extracted_text[0...150].tr_s("\n", " ").squeeze(" ")
end

def canonical_url(context)
File.join(@options.canonical_root, context&.path.to_s)
Copy link
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
Contributor Author

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 p8 force-pushed the features/canonical-meta-tag branch from ab9dded to ca5afb8 Compare May 1, 2025 10:59
Copy link
Member

@kou kou left a comment

Choose a reason for hiding this comment

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

+1

@p8 p8 force-pushed the features/canonical-meta-tag branch 3 times, most recently from 84858e4 to 0c82bbe Compare May 1, 2025 17:22
@p8
Copy link
Contributor Author

p8 commented May 1, 2025

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 p8 force-pushed the features/canonical-meta-tag branch from 0c82bbe to f07af27 Compare May 4, 2025 16:34
Copy link
Member

@st0012 st0012 left a comment

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
Member

st0012 commented May 6, 2025

@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 <[email protected]>
@p8 p8 force-pushed the features/canonical-meta-tag branch from f07af27 to cec51e3 Compare May 6, 2025 07:55
@p8
Copy link
Contributor Author

p8 commented May 6, 2025

Thanks @st0012 ! I've rebased to master.

@st0012 st0012 merged commit c68ae93 into ruby:master May 6, 2025
29 of 30 checks passed
@p8 p8 deleted the features/canonical-meta-tag branch May 6, 2025 17:28
@st0012 st0012 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
Member

st0012 commented May 22, 2025

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

@p8
Copy link
Contributor Author

p8 commented May 23, 2025

@st0012 Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants