Skip to content
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

Use sphinxext-opengraph to generate OpenGraph metadata #953

Merged
merged 13 commits into from Nov 30, 2022

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Oct 6, 2022

conf.py Outdated

# sphinxext-opengraph config
ogp_site_url = "https://devguide.python.org/"
ogp_image = "https://devguide.python.org/_static/python-logo.svg"
Copy link
Member

Choose a reason for hiding this comment

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

Apparently SVGs are not supported, so we should use a different image.

Should we also add more variables?

Copy link
Member Author

Choose a reason for hiding this comment

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

Will find another image. The recommended image size is 1200 x 630, should we use a Python logo or something else?

I don't think we need any of the other variables.

Copy link
Member Author

Choose a reason for hiding this comment

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

Re: other variables:

I'll check ogp_custom_meta_tags for <meta name="description">.

python/docs-community#65 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll check ogp_custom_meta_tags for <meta name="description">.

We can use ogp_custom_meta_tags to add it, but it would be hardcoded and we'd like to generate it from each page.

Copy link
Member

Choose a reason for hiding this comment

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

The recommended image size is 1200 x 630, should we use a Python logo or something else?

Since the image is rectangular (and somewhat big), it would be nice if it had both the Python logo and the text "Devguide". If/when we do the same for cpython (or for the peps repo), we can have the same logo but different texts, so that the links are easily distinguishable.

This however would require creating new images, so if you don't want to do that, just a simple logo will do (we can always update the image later).

I don't think we need any of the other variables.

ok

We can use ogp_custom_meta_tags to add it, but it would be hardcoded and we'd like to generate it from each page.

Right, it would be better if the description was created dynamically.

Copy link
Member Author

@hugovk hugovk Oct 10, 2022

Choose a reason for hiding this comment

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

SVG replaced with a PNG of the Python logo + "Devguide".

og-image

I wrote a script to create it (hugovk/pixel-tools#15), so we can re-use that for other repos if we want.

I also squashed out the extra bytes from the PNG using optipng -o7 -zm1-9 _static/og-image.png (57,118 -> 9,573 bytes, 16.76% decrease).

When temporarily setting ogp_image = "https://cpython-devguide--953.org.readthedocs.build/_static/og-image.png" Facebook's sharing debugger shows the following OG metadata/preview:

image

https://developers.facebook.com/tools/debug/?q=https%3A%2F%2Fcpython-devguide--953.org.readthedocs.build%2F

Also included og:image:width and og:image:height because the debugger said "Using these tags will specify the image to the crawler so that it can render it immediately without having to asynchronously".

Copy link
Member

Choose a reason for hiding this comment

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

The new image looks good -- I wonder if we can use the same font used with the logo though.

https://www.python.org/community/logos/ says

The font used in the logo is called "Flux Regular". The PSF owns a copy but we cannot distribute it, except for work on the PSF's behalf.

Further down on the same page it says:

In general, we want the logo to be used as widely as possible to indicate use of Python or suitability for Python. However, please ask first when using a derived version of the logo or when in doubt.

So we should ask there first, and if they agree we can regenerate the logos with the right font.

This is not a blocker by any means though, so we can merge with the current image and recreate it later.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's taking a while to get the font. It would be nice to get this merged to see how it works when live. Shall we merge this as-is, or with the image removed?

Copy link
Member

Choose a reason for hiding this comment

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

Let's merge it.
I'll ping Deb again about the font and we can update that in a follow-up PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

(Continued in main thread: #953 (comment))

@ezio-melotti
Copy link
Member

The OGP tags are also used to generate previews (e.g. on social media). I did some testing using the preview build, and on Discord I got:
image

The preview (including title and text) show up, but there is no image -- apparently SVGs are not supported, so we need a different image.
I got similar results elsewhere, except for Google Chat (it didn't generate any preview there, not sure why).

conf.py Show resolved Hide resolved
conf.py Outdated

# sphinxext-opengraph config
ogp_site_url = "https://devguide.python.org/"
ogp_image = "https://devguide.python.org/_static/python-logo.svg"
Copy link
Member

Choose a reason for hiding this comment

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

The new image looks good -- I wonder if we can use the same font used with the logo though.

https://www.python.org/community/logos/ says

The font used in the logo is called "Flux Regular". The PSF owns a copy but we cannot distribute it, except for work on the PSF's behalf.

Further down on the same page it says:

In general, we want the logo to be used as widely as possible to indicate use of Python or suitability for Python. However, please ask first when using a derived version of the logo or when in doubt.

So we should ask there first, and if they agree we can regenerate the logos with the right font.

This is not a blocker by any means though, so we can merge with the current image and recreate it later.

Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
Copy link
Member

@ezio-melotti ezio-melotti left a comment

Choose a reason for hiding this comment

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

I tested the latest iteration and got this:
image

The website name works, but the image still doesn't (it loads for a while and then 💩).
This is because it currently points to https://devguide.python.org/_static/og-image.png, so the suggestion below should fix it.

conf.py Outdated Show resolved Hide resolved
Co-authored-by: Ezio Melotti <ezio.melotti@gmail.com>
@ezio-melotti
Copy link
Member

I saw that in the source of https://cpython-devguide--953.org.readthedocs.build/ there is a <meta property="og:url" content="https://devguide.python.org/index/" />, and https://devguide.python.org/index/ is a 404.

With the dirhtml builder sphinxext-opengraph is adding the name of the current page to the url:
https://github.com/wpilibsuite/sphinxext-opengraph/blob/9f1a8c890ede83e11d24f20e569220873f5f71f8/sphinxext/opengraph/__init__.py#L89-L97

On the main page (i.e. /) this ends up appending an extra index/ resulting in the 404.
We could add a redirect on our end, but I wonder if index should be special-cased upstream. 🤔

@hugovk
Copy link
Member Author

hugovk commented Oct 11, 2022

This would be better fixed upstream, as it will break for other sites using dirhtml. Please could you report?

@hugovk
Copy link
Member Author

hugovk commented Oct 30, 2022

Reported at wpilibsuite/sphinxext-opengraph#77 and created PR wpilibsuite/sphinxext-opengraph#78.

@hugovk
Copy link
Member Author

hugovk commented Nov 2, 2022

Reported at wpilibsuite/sphinxext-opengraph#77 and created PR wpilibsuite/sphinxext-opengraph#78.

The dirhtml bug is fixed and released in sphinxext-opengraph 0.7.1! 🚀

Also, to help python/docs-community#65, my PR wpilibsuite/sphinxext-opengraph#71 to use og:description as description is also part of 0.7.1 and enabled by default.


Here's a demo branch and build with the URL fixed so the image URL works (hugovk@483ee38)

Here's a page in the Facebook sharing debugger:

image

Here's a Slack share (demo and current site):

image

And Discord:

image

@TheTripleV
Copy link

Noticed you have a Discord example. Adding a "theme-color" meta tag lets you customize the color of the vertical bar on the left side of the Discord embed.
Example color config: https://github.com/wpilibsuite/frc-docs/blob/main/source/conf.py#L123
Example url with a color: https://docs.wpilib.org/en/stable/index.html

@hugovk
Copy link
Member Author

hugovk commented Nov 4, 2022

Thanks for the tip! I updated this and the demo branch to use Python blue:

image

@hugovk
Copy link
Member Author

hugovk commented Nov 28, 2022

I found the fonts in https://github.com/python/pythondotorg/tree/main/static/fonts so have regenerated the image using https://github.com/hugovk/pixel-tools/blob/1618b2a09bd5998899958856b7fef4503f95cba2/og_image.py like:

python og_image.py --logo tests/python-logo.png --size 210 --font ../pythondotorg/static/fonts/FluxRegular.ttf
...
optipng -o7 -zm1-9 _static/og-image.png # 16.28% decrease

og-image

Demo

https://hugovk-devguide.readthedocs.io/en/add-og-metadata2/testing/run-write-tests/

Preview in Facebook

image

https://developers.facebook.com/tools/debug/?q=https%3A%2F%2Fhugovk-devguide.readthedocs.io%2Fen%2Fadd-og-metadata2%2Ftesting%2Frun-write-tests%2F

Preview in Slack

image

ogp_custom_meta_tags = [
'<meta property="og:image:width" content="1200">',
'<meta property="og:image:height" content="630">',
'<meta name="theme-color" content="#3776ab" />',
Copy link
Member

Choose a reason for hiding this comment

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

FWIW I looked into using a gradient that matches both colors of the Python logo, but this doesn't seem supported.

@hugovk hugovk merged commit 86770f2 into python:main Nov 30, 2022
@hugovk hugovk deleted the add-og-metadata branch November 30, 2022 13:42
@hugovk
Copy link
Member Author

hugovk commented Nov 30, 2022

Google's SEO checks

Using their PageSpeed Insights tool:

https://pagespeed.web.dev/report?url=https%3A%2F%2Fdevguide.python.org%2F&form_factor=desktop

Before: 91 After: 100

@hugovk
Copy link
Member Author

hugovk commented Nov 30, 2022

Facebook sharing preview

https://developers.facebook.com/tools/debug/?q=https%3A%2F%2Fdevguide.python.org%2F

Before After

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

Successfully merging this pull request may close these issues.

None yet

3 participants