2021-07-25 15:40:40 +02:00
|
|
|
|
# Setting up social cards
|
|
|
|
|
|
|
|
|
|
Social cards, also known as social previews, are images that are displayed when
|
|
|
|
|
a link to your project documentation is shared on social media. Material for
|
2022-09-11 19:25:40 +02:00
|
|
|
|
MkDocs can generate beautiful social cards automatically, using the [colors],
|
|
|
|
|
[fonts] and [logo][^1] defined in `mkdocs.yml`,
|
2021-10-20 23:02:55 +02:00
|
|
|
|
e.g.:
|
|
|
|
|
|
|
|
|
|
<figure markdown>
|
|
|
|
|
|
|
|
|
|
[![Social cards preview]][Social cards preview]
|
|
|
|
|
|
|
|
|
|
<figcaption markdown>
|
|
|
|
|
|
|
|
|
|
The social preview image for the page on [setting up site analytics].
|
|
|
|
|
[Twitter's Card validator] shows how it will look when shared.
|
|
|
|
|
|
|
|
|
|
</figcaption>
|
|
|
|
|
</figure>
|
2021-07-25 20:36:30 +02:00
|
|
|
|
|
|
|
|
|
[^1]:
|
|
|
|
|
Both types of logos, images (`theme.logo`) and icons (`theme.icon.logo`)
|
|
|
|
|
are supported. While an image logo is used as-is, icons are filled with the
|
|
|
|
|
color used in the header (white or black), which depends on the primary
|
|
|
|
|
color.
|
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
[colors]: changing-the-colors.md#primary-color
|
|
|
|
|
[fonts]: changing-the-fonts.md#regular-font
|
2021-10-10 19:22:13 +02:00
|
|
|
|
[logo]: changing-the-logo-and-icons.md#logo
|
2021-10-20 23:02:55 +02:00
|
|
|
|
[Social cards preview]: ../assets/screenshots/social-cards.png
|
|
|
|
|
[setting up site analytics]: setting-up-site-analytics.md
|
|
|
|
|
[Twitter's Card validator]: https://cards-dev.twitter.com/validator
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
2022-02-27 13:19:44 +01:00
|
|
|
|
### Built-in social plugin
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
2022-09-13 13:27:13 +02:00
|
|
|
|
[:octicons-tag-24: 8.5.0][Social cards support] ·
|
2021-10-03 20:28:52 +02:00
|
|
|
|
:octicons-cpu-24: Plugin ·
|
2021-10-10 19:22:13 +02:00
|
|
|
|
:octicons-beaker-24: Experimental
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
2021-10-20 23:02:55 +02:00
|
|
|
|
First, ensure you've installed all [dependencies] and have a valid [`site_url`]
|
|
|
|
|
[site_url], as social preview images must be referenced via absolute URLs.
|
|
|
|
|
Then, add the following lines to `mkdocs.yml`:
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-20 23:02:55 +02:00
|
|
|
|
The following configuration options are available:
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
[`cards`](#+social.cards){ #+social.cards }
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
2021-12-05 12:39:54 +01:00
|
|
|
|
: :octicons-milestone-24: Default: `true` – This option specifies whether
|
2021-10-20 23:02:55 +02:00
|
|
|
|
to generate social card images. If you want to switch the plugin off, e.g.
|
|
|
|
|
for local builds, you can use an [environment variable]:
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
2021-10-20 23:02:55 +02:00
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social:
|
2022-02-20 19:49:23 +01:00
|
|
|
|
cards: !ENV [CARDS, false]
|
2021-10-20 23:02:55 +02:00
|
|
|
|
```
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
[`cards_color`](#+social.cards_color){ #+social.cards_color }
|
2021-08-07 13:12:05 +02:00
|
|
|
|
|
2022-09-13 13:27:13 +02:00
|
|
|
|
: :octicons-milestone-24: Default: [`theme.palette.primary`][palette.primary]
|
|
|
|
|
– This option specifies the colors for the background `fill` and foreground
|
|
|
|
|
`text` when generating the social card:
|
2021-08-07 13:12:05 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social:
|
|
|
|
|
cards_color:
|
2021-12-11 14:30:07 +01:00
|
|
|
|
fill: "#0FF1CE" # (1)!
|
2021-08-07 13:12:05 +02:00
|
|
|
|
text: "#FFFFFF"
|
|
|
|
|
```
|
|
|
|
|
|
2021-10-10 19:22:13 +02:00
|
|
|
|
1. Colors can either be defined as HEX colors, or as [CSS color keywords].
|
|
|
|
|
Note that HEX colors must be enclosed in quotes.
|
2021-08-07 13:12:05 +02:00
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
[`cards_font`](#+social.cards_font){ #+social.cards_font }
|
2021-12-05 12:39:54 +01:00
|
|
|
|
|
2022-09-13 13:27:13 +02:00
|
|
|
|
: :octicons-milestone-24: Default: [`theme.font.text`][font.text] – This
|
|
|
|
|
option specifies which font to use for rendering the social card, which can
|
|
|
|
|
be any font hosted on [Google Fonts]:
|
2021-12-05 12:39:54 +01:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social:
|
|
|
|
|
cards_font: Roboto
|
|
|
|
|
```
|
|
|
|
|
|
2022-09-18 16:03:45 +02:00
|
|
|
|
!!! question "Why do social cards render boxes for CJK languages?"
|
|
|
|
|
|
|
|
|
|
Some fonts do not contain CJK characters, like for example the
|
|
|
|
|
[default font, `Roboto`][font.text]. In case your `site_name`,
|
|
|
|
|
`site_description`, or [page title] contain CJK characters, choose
|
|
|
|
|
another font from [Google Fonts] which comes with CJK characters, e.g.
|
|
|
|
|
one from the `Noto Sans` font family:
|
|
|
|
|
|
|
|
|
|
=== "Chinese (Simplified)"
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social:
|
|
|
|
|
cards_font: Noto Sans SC
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Chinese (Traditional)"
|
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social:
|
|
|
|
|
cards_font: Noto Sans TC
|
|
|
|
|
```
|
2022-09-18 15:53:41 +02:00
|
|
|
|
|
|
|
|
|
=== "Japanese"
|
2022-09-18 16:03:45 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social:
|
|
|
|
|
cards_font: Noto Sans JP
|
|
|
|
|
```
|
2022-09-18 15:53:41 +02:00
|
|
|
|
|
|
|
|
|
=== "Korean"
|
2022-09-18 16:03:45 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social:
|
|
|
|
|
cards_font: Noto Sans KR
|
|
|
|
|
```
|
2022-09-18 15:53:41 +02:00
|
|
|
|
|
2022-09-11 19:25:40 +02:00
|
|
|
|
[`cards_dir`](#+social.cards_dir){ #+social.cards_dir }
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
|
|
|
|
: :octicons-milestone-24: Default: `assets/images/social` – This option
|
2021-10-10 19:22:13 +02:00
|
|
|
|
specifies where the generated social card images will be written to. It's
|
|
|
|
|
normally not necessary to change this option:
|
2021-07-25 15:40:40 +02:00
|
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
|
plugins:
|
|
|
|
|
- social:
|
2022-09-11 19:25:40 +02:00
|
|
|
|
cards_dir: path/to/folder
|
2021-07-25 15:40:40 +02:00
|
|
|
|
```
|
|
|
|
|
|
2022-09-13 13:27:13 +02:00
|
|
|
|
[Social cards support]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.5.0
|
2021-10-20 23:02:55 +02:00
|
|
|
|
[dependencies]: #dependencies
|
2021-10-10 21:04:22 +02:00
|
|
|
|
[site_url]: https://www.mkdocs.org/user-guide/configuration/#site_url
|
2022-09-11 19:25:40 +02:00
|
|
|
|
[palette.primary]: changing-the-colors.md#primary-color
|
|
|
|
|
[font.text]: changing-the-fonts.md#regular-font
|
2021-10-20 23:02:55 +02:00
|
|
|
|
[environment variable]: https://www.mkdocs.org/user-guide/configuration/#environment-variables
|
2021-10-10 19:22:13 +02:00
|
|
|
|
[CSS color keywords]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#color_keywords
|
2021-12-05 12:39:54 +01:00
|
|
|
|
[Google Fonts]: https://fonts.google.com
|
2022-09-18 16:03:45 +02:00
|
|
|
|
[page title]: ../reference/index.md#setting-the-page-title
|
2021-07-25 20:36:30 +02:00
|
|
|
|
|
2021-10-20 23:02:55 +02:00
|
|
|
|
#### Dependencies
|
|
|
|
|
|
2022-09-15 19:02:13 +02:00
|
|
|
|
Two Python libraries must be installed alongside Material for MkDocs to generate
|
|
|
|
|
the social preview images, both of which are based on [Cairo Graphics] –
|
|
|
|
|
[Pillow] and [CairoSVG]:
|
2021-10-20 23:02:55 +02:00
|
|
|
|
|
2022-09-15 19:02:13 +02:00
|
|
|
|
```
|
|
|
|
|
pip install pillow cairosvg
|
|
|
|
|
```
|
2021-10-20 23:02:55 +02:00
|
|
|
|
|
2022-09-15 19:02:13 +02:00
|
|
|
|
Both libraries are built with native extensions which need to be installed as
|
|
|
|
|
well. The [Docker image] comes with all dependencies pre-installed. If you don't
|
|
|
|
|
want to use Docker, see the following section which explains how to install all
|
|
|
|
|
dependencies on your system:
|
2021-10-20 23:02:55 +02:00
|
|
|
|
|
2021-10-27 23:26:07 +02:00
|
|
|
|
=== ":material-apple: macOS"
|
|
|
|
|
|
|
|
|
|
Make sure [Homebrew] is installed, which is a modern package manager for
|
|
|
|
|
macOS. Next, use the following command to install all necessary
|
|
|
|
|
dependencies:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
brew install cairo freetype libffi libjpeg libpng zlib
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== ":fontawesome-brands-windows: Windows"
|
|
|
|
|
|
|
|
|
|
As stated in the [installation guide], the easiest way to get up and running
|
|
|
|
|
with the [Cairo Graphics] library on Windows is by installing [GTK+], since
|
2022-09-30 19:48:50 +02:00
|
|
|
|
it has Cairo as a dependency. You can also download and install a
|
|
|
|
|
precompiled [GTK runtime].
|
2021-10-27 23:26:07 +02:00
|
|
|
|
|
|
|
|
|
=== ":material-linux: Linux"
|
2021-10-20 23:02:55 +02:00
|
|
|
|
|
2021-10-27 23:26:07 +02:00
|
|
|
|
There are several package managers for Linux with varying availability per
|
|
|
|
|
distribution. The [installation guide] explains how to install the [Cairo
|
|
|
|
|
Graphics] library for your distribution:
|
|
|
|
|
|
|
|
|
|
=== ":material-ubuntu: Ubuntu"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
apt-get install libcairo2-dev libfreetype6-dev libffi-dev libjpeg-dev libpng-dev libz-dev
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== ":material-fedora: Fedora"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
yum install cairo-devel freetype-devel libffi-devel libjpeg-devel libpng-devel zlib-devel
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== ":fontawesome-brands-suse: openSUSE"
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
zypper install cairo-devel freetype-devel libffi-devel libjpeg-devel libpng-devel zlib-devel
|
|
|
|
|
```
|
2021-10-20 23:02:55 +02:00
|
|
|
|
|
|
|
|
|
[Cairo Graphics]: https://www.cairographics.org/
|
|
|
|
|
[Pillow]: https://pillow.readthedocs.io/
|
|
|
|
|
[CairoSVG]: https://cairosvg.org/
|
2022-09-13 13:27:13 +02:00
|
|
|
|
[Docker image]: https://hub.docker.com/r/squidfunk/mkdocs-material/
|
2021-10-27 23:26:07 +02:00
|
|
|
|
[Homebrew]: https://brew.sh/
|
|
|
|
|
[installation guide]: https://www.cairographics.org/download/
|
|
|
|
|
[GTK+]: https://www.gtk.org/docs/installations/windows/
|
2022-09-30 19:48:50 +02:00
|
|
|
|
[GTK runtime]: https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer/releases
|
2021-10-20 23:02:55 +02:00
|
|
|
|
|
2021-10-10 21:04:22 +02:00
|
|
|
|
#### Caching <small>recommended</small> { #caching data-toc-label="Caching" }
|
2021-07-25 20:36:30 +02:00
|
|
|
|
|
2022-02-27 13:19:44 +01:00
|
|
|
|
The [built-in social plugin] automatically fetches the fonts you define in
|
|
|
|
|
`mkdocs.yml` from Google Fonts, and uses them to render the text that is
|
2021-10-10 19:22:13 +02:00
|
|
|
|
displayed on the social card. The font files and generated cards are both
|
2021-07-25 20:36:30 +02:00
|
|
|
|
written to the `.cache` directory, which is used in subsequent builds to detect
|
|
|
|
|
whether the social cards need to be regenerated. You might want to:
|
|
|
|
|
|
2021-10-10 12:19:14 +02:00
|
|
|
|
1. Ignore the `.cache` directory in your project, by adding it to `.gitignore`.
|
|
|
|
|
2. When building your site for publishing, use a build cache to save the
|
|
|
|
|
`.cache` directory in between builds. Taking the example from the
|
2021-10-10 19:22:13 +02:00
|
|
|
|
[publishing guide], add the following lines:
|
2021-07-25 20:36:30 +02:00
|
|
|
|
|
|
|
|
|
``` yaml hl_lines="15-18"
|
|
|
|
|
name: ci
|
|
|
|
|
on:
|
|
|
|
|
push:
|
|
|
|
|
branches:
|
|
|
|
|
- master
|
|
|
|
|
- main
|
|
|
|
|
jobs:
|
|
|
|
|
deploy:
|
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
|
steps:
|
|
|
|
|
- uses: actions/checkout@v2
|
|
|
|
|
- uses: actions/setup-python@v2
|
|
|
|
|
with:
|
|
|
|
|
python-version: 3.x
|
|
|
|
|
- uses: actions/cache@v2
|
|
|
|
|
with:
|
|
|
|
|
key: ${{ github.ref }}
|
|
|
|
|
path: .cache
|
|
|
|
|
- run: pip install mkdocs-material
|
|
|
|
|
- run: mkdocs gh-deploy --force
|
|
|
|
|
```
|
|
|
|
|
|
2022-02-27 13:19:44 +01:00
|
|
|
|
[built-in social plugin]: #built-in-social-plugin
|
2021-10-10 19:22:13 +02:00
|
|
|
|
[publishing guide]: ../publishing-your-site.md#with-github-actions
|
2021-07-25 20:36:30 +02:00
|
|
|
|
|
2021-10-10 19:22:13 +02:00
|
|
|
|
#### Meta tags
|
2021-07-25 20:36:30 +02:00
|
|
|
|
|
2022-02-27 13:19:44 +01:00
|
|
|
|
The [built-in social plugin] automatically sets all necessary `meta` tags,
|
2021-10-10 19:22:13 +02:00
|
|
|
|
equivalent to the following two customizations, which you can set manually when
|
|
|
|
|
you don't want to use it:
|
2021-10-10 13:17:39 +02:00
|
|
|
|
|
|
|
|
|
=== ":material-graph: Open Graph"
|
|
|
|
|
|
|
|
|
|
``` html
|
2022-01-16 13:44:31 +01:00
|
|
|
|
{% extends "base.html" %}
|
|
|
|
|
|
2021-10-10 13:17:39 +02:00
|
|
|
|
{% block extrahead %}
|
|
|
|
|
{% set title = config.site_name %}
|
|
|
|
|
{% if page and page.meta and page.meta.title %}
|
|
|
|
|
{% set title = title ~ " - " ~ page.meta.title %}
|
|
|
|
|
{% elif page and page.title and not page.is_homepage %}
|
2021-10-10 19:22:13 +02:00
|
|
|
|
{% set title = title ~ " - " ~ page.title %}
|
2021-10-10 13:17:39 +02:00
|
|
|
|
{% endif %}
|
|
|
|
|
<meta property="og:type" content="website" />
|
|
|
|
|
<meta property="og:title" content="{{ title }}" />
|
|
|
|
|
<meta property="og:description" content="{{ config.site_description }}" />
|
|
|
|
|
<meta property="og:url" content="{{ page.canonical_url }}" />
|
|
|
|
|
<meta property="og:image" content="<url>" />
|
|
|
|
|
<meta property="og:image:type" content="image/png" />
|
|
|
|
|
<meta property="og:image:width" content="1200" />
|
|
|
|
|
<meta property="og:image:height" content="630" />
|
|
|
|
|
{% endblock %}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== ":fontawesome-brands-twitter: Twitter Cards"
|
|
|
|
|
|
|
|
|
|
``` html
|
2022-01-16 13:44:31 +01:00
|
|
|
|
{% extends "base.html" %}
|
|
|
|
|
|
2021-10-10 13:17:39 +02:00
|
|
|
|
{% block extrahead %}
|
|
|
|
|
{% set title = config.site_name %}
|
|
|
|
|
{% if page and page.meta and page.meta.title %}
|
|
|
|
|
{% set title = title ~ " - " ~ page.meta.title %}
|
|
|
|
|
{% elif page and page.title and not page.is_homepage %}
|
2021-10-10 19:22:13 +02:00
|
|
|
|
{% set title = title ~ " - " ~ page.title %}
|
2021-10-10 13:17:39 +02:00
|
|
|
|
{% endif %}
|
|
|
|
|
<meta name="twitter:card" content="summary_large_image" />
|
|
|
|
|
<meta name="twitter:title" content="{{ title }}" />
|
|
|
|
|
<meta name="twitter:description" content="{{ config.site_description }}" />
|
|
|
|
|
<meta name="twitter:image" content="<url>" />
|
|
|
|
|
{% endblock %}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[Twitter Cards]: https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/abouts-cards
|
2021-10-10 19:22:13 +02:00
|
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
|
|
If you want to adjust the title or set a custom description for the social card,
|
2022-08-13 10:57:43 +02:00
|
|
|
|
you can set the front matter `title` and `description` properties, which take
|
|
|
|
|
precedence over the default values.
|
2021-10-10 19:22:13 +02:00
|
|
|
|
|
|
|
|
|
- [Changing the title]
|
|
|
|
|
- [Changing the description]
|
|
|
|
|
|
2021-12-16 17:08:57 +01:00
|
|
|
|
[Changing the title]: ../reference/index.md#setting-the-page-title
|
|
|
|
|
[Changing the description]: ../reference/index.md#setting-the-page-description
|