1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-12-18 10:25:58 +01:00
mkdocs-material/docs/setup/setting-up-social-cards.md

195 lines
6.5 KiB
Markdown
Raw Normal View History

2021-07-25 15:40:40 +02:00
---
template: overrides/main.html
---
# 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
MkDocs can generate beautiful social cards automatically, using the [colors][1],
[fonts][2] and [logo][3][^1] defined in `mkdocs.yml`.
[^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.
[1]: changing-the-colors.md#primary-color
[2]: changing-the-fonts.md#regular-font
[3]: changing-the-logo-and-icons.md#logo
2021-07-25 15:40:40 +02:00
## Configuration
### Built-in social cards
2021-10-03 20:28:52 +02:00
[:octicons-heart-fill-24:{ .mdx-heart } Insiders][Insiders]{ .mdx-insiders } ·
:octicons-cpu-24: Plugin ·
2021-07-25 15:40:40 +02:00
:octicons-beaker-24: Experimental ·
2021-10-03 20:28:52 +02:00
[:octicons-tag-24: insiders-2.12.0 ... present][Insiders]
2021-07-25 15:40:40 +02:00
The [built-in social cards plugin][4] generates a social card image for every
2021-07-25 15:40:40 +02:00
page and adds the necessary meta tags, so it's displayed on social media when
shared. Enable it via `mkdocs.yml`:
``` yaml
plugins:
- social
```
For example, the page on [setting up site analytics][5] renders as:
2021-07-25 15:40:40 +02:00
2021-10-04 23:36:31 +02:00
<figure markdown>
2021-07-25 15:40:40 +02:00
[![Social Cards][6]][6]
2021-07-25 15:40:40 +02:00
2021-10-04 23:36:31 +02:00
<figcaption markdown>
2021-07-25 15:40:40 +02:00
2021-07-25 20:37:57 +02:00
Want to try it out? Copy the current URL and paste it into [Twitter's Card
validator][7] to see how social cards look in action.
2021-07-25 15:40:40 +02:00
</figcaption>
</figure>
This is a built-in plugin, which means that no third-party plugin needs to be
installed, as Material for MkDocs already bundles it. The following options
are available:
2021-10-10 12:19:14 +02:00
`cards_color`{ #cards-color } :material-new-box:
2021-08-07 13:12:05 +02:00
: :octicons-milestone-24: Default: _automatically set based on [primary
color][8]_ This option specifies which colors to use for the background
`fill` and foreground `text` when generating the social card.
``` yaml
plugins:
- social:
cards_color:
fill: "#0FF1CE"
text: "#FFFFFF"
```
Note that the values for `fill` and `text` can either be HEX color values
(e.g. `#0FF1CE`, must be enclosed in quotes) or CSS color keywords (e.g.
`red`, `green`, etc.).
2021-10-10 12:19:14 +02:00
`cards_directory`{ #cards-directory }
2021-07-25 15:40:40 +02:00
: :octicons-milestone-24: Default: `assets/images/social` This option
specifies where the generated social card images will be written to. It
should normally not be necessary to change this option.
``` yaml
plugins:
- social:
cards_directory: assets/images/social
```
2021-10-03 20:28:52 +02:00
[Insiders]: ../insiders/index.md
[4]: ../insiders/index.md
[5]: setting-up-site-analytics.md
[6]: ../assets/screenshots/social-cards.png
[7]: https://cards-dev.twitter.com/validator
2021-08-07 13:12:05 +02:00
[8]: changing-the-colors.md#primary-color
#### Caching
2021-08-07 13:12:05 +02:00
When enabled, the [social cards plugin][9] automatically fetches the fonts you
define in `mkdocs.yml` from Google Fonts, and uses them to render the text that
is displayed on the social card. The font files and generated cards are both
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
[publishing guide][10], add the following lines:
``` 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
```
2021-08-07 13:12:05 +02:00
[9]: #built-in-social-cards
[10]: ../publishing-your-site.md#with-github-actions
## Usage
If you want to adjust the title or set a custom description for the social card,
2021-08-07 13:12:05 +02:00
you can use the [Metadata][11] extension, which takes precedence over the
default values.
2021-08-07 13:12:05 +02:00
- [Changing the title][12]
- [Changing the description][13]
2021-08-07 13:12:05 +02:00
[11]: ../reference/meta-tags.md#metadata
[12]: ../reference/meta-tags.md#setting-the-page-title
[13]: ../reference/meta-tags.md#setting-the-page-description
### Using social media meta tags
The [built-in social cards plugin] generates beautiful image previews for social
media during the build and sets all necessary `meta` tags, equivalent to the
following two customizations:
=== ":material-graph: Open Graph"
``` html
{% 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 %}
{% set title = title ~ " - " ~ page.title | striptags %}
{% 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
{% 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 %}
{% set title = title ~ " - " ~ page.title | striptags %}
{% endif %}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:site" content="<username>" />
<meta name="twitter:creator" content="<username>" />
<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
[built-in social cards plugin]: ../setup/setting-up-social-cards.md#built-in-social-cards