Skip to content

New Social Media Card extension #261

@2bndy5

Description

@2bndy5

I'm exploring how to implement a theme-specific extension similar to upstream's builtin social plugin.

‼️ Coincidentally, there recently was a major overhaul of that plugin for the insiders edition. It now involves customizable "layouts" used to generate the social card image. I like the changes and want to include something similar in this new sphinx-immaterial extension, but I'm currently brainstorming an adequate approach (might end up with a pythonic approach in conf.py).

🚧 Locally, I have the basic features from upstream implemented, but it is still very fluid (optimizing code and execution overhead, adding thorough customizability, etc).

This will also rely on google fonts caching, so it will unfortunately add to the desired solution in #222

Alternate approaches

Currently there's nothing stopping doc authors from using other sphinx extensions (ie sphinxext-opengraph) or implementing it manually (via metadata roles/directives). However, these approaches lack in some way or another:

  • sphinxext-opengraph isn't extensible enough and doesn't include the metadata required by twitter.
  • manual implementation is best done with an added HTML template (almost identical to what was documented upstream), but can be laboriously done on a per-page basis using the metadata fields or the newer docutils meta directive. Either way, the card's image has to be created manually as well, making this approach even more laborious.

New dependencies

To generate the card image, I'll be using pillow.

cairosvg

Upstream uses pillow, but they also require a cairosvg to optimize SVGs and convert them to something pillow can use. My current focus is not on optimizing all images used in the generated docs.

The upstream docs state:

Cairo Graphics is a graphics library and dependency of Pillow

But I didn't find this to be true. Pillow works fine by itself for my needs here. Since cairosvg requires a multitude of non-python dependencies, I'm trying to avoid offloading that platform-specific setup (a perceivable nightmare without a docker container) to end-users and CI workflows. Unfortunately, this means that we can't support SVG images in card generation (unless pillow adds the necessary support). Remember that our html_logo config is not bound to only using SVGs (which is the behavior of an example in upstream's theme.icon.logo).

matplotlib

I looked at matplotlib as it is used in sphinxext-opengraph, but the matplotlib's docs recommend using pillow to open images. So, I moved away from the matplotlib idea entirely.

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationenhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions