1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-12-22 20:36:02 +01:00
mkdocs-material/docs/setup/setting-up-social-cards.md

1111 lines
35 KiB
Markdown
Raw Normal View History

2023-05-08 10:25:09 +02:00
---
status: new
---
2021-07-25 15:40:40 +02:00
# Setting up social cards
2023-05-08 10:25:09 +02:00
Material for MkDocs can automatically create beautiful social cards for your
documentation, which appear as link previews on social media platforms. You
2023-05-08 16:44:39 +02:00
can select from several [pre-designed layouts][default layouts] or create
2023-05-08 10:25:09 +02:00
[custom layouts] to match your unique style and branding.
2021-10-20 23:02:55 +02:00
---
:fontawesome-brands-youtube:{ style="color: #EE0F0F" }
__[How to build custom social cards]__ by @james-willett :octicons-clock-24:
24m Learn how to create entirely custom social cards perfectly matching your
branding for each page automatically!
[How to build custom social cards]: https://www.youtube.com/watch?v=4OjnOc6ftJ8
2021-10-20 23:02:55 +02:00
<figure markdown>
2023-05-08 10:25:09 +02:00
[![Layout default variant]][Layout default variant]
2021-10-20 23:02:55 +02:00
<figcaption markdown>
2023-05-08 16:44:39 +02:00
Social card of our [formatting] reference
2021-10-20 23:02:55 +02:00
</figcaption>
</figure>
2023-05-08 16:44:39 +02:00
[custom layouts]: #customization
2023-05-08 10:25:09 +02:00
[formatting]: ../reference/formatting.md
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
2023-05-08 10:25:09 +02:00
The built-in social plugin automatically generate a custom preview image for
each page. Install all [dependencies for image processing][^1] and add the
following lines to `mkdocs.yml`:
[^1]:
The awesome thing about social cards is that they are generated during
build time and directly distributed with your documentation, no external
services involved. While it would technically be simpler to generate
social cards using a web browser and an automation framework like
[Puppeteer], it would add further liabilities to the toolchain, with the
potential to make build pipelines more complex and resource intense.
For this reason, Material for MkDocs again follows its core principle of
making it as simple and powerful as possible, providing an easy-to-use
framework for building [custom layouts] using Python image processing
2023-05-08 16:44:39 +02:00
libraries.
2021-07-25 15:40:40 +02:00
``` yaml
plugins:
- social
```
2023-05-08 10:25:09 +02:00
> Note that [Insiders] contains a ground up rewrite of the social plugin that
> generates images much more efficiently in parallel and allows to build
> entirely [custom layouts].
2023-08-19 14:42:44 +02:00
!!! info "The [`site_url`][site_url] setting must be set"
Note that you must set [`site_url`][site_url] when using the social plugin,
or the generated cards will not be correctly linked. Social media services
like Twitter and Facebook demand that social previews point to an absolute
URL, which the plugin can only compute when [`site_url`][site_url] is set.
Example:
``` yaml
site_url: https://example.com
```
[site_url]: https://www.mkdocs.org/user-guide/configuration/#site_url
2021-10-20 23:02:55 +02:00
The following configuration options are available:
2021-07-25 15:40:40 +02:00
2023-05-08 10:25:09 +02:00
[`enabled`](#+social.enabled){ #+social.enabled }
: :octicons-milestone-24: Default: `true` This option specifies whether
the plugin is enabled when building your project. If you want to speed up
local builds, you can use an [environment variable]:
``` yaml
plugins:
- social:
enabled: !ENV [CI, false]
```
[`concurrency`](#+social.concurrency){ #+social.concurrency }
: [:octicons-tag-24: insiders-4.33.0][Insiders] · :octicons-milestone-24:
Default: _number of CPUs_ How many CPUs the plugin is allowed to use when
generating social cards. With more CPUs, the plugin can do more work in the
same time, thus complete generation faster. Concurrent processing can be
disabled with:
``` yaml
plugins:
- social:
concurrency: 1
```
[Social cards support]: https://github.com/squidfunk/mkdocs-material/releases/tag/8.5.0
[built-in plugins]: ../insiders/getting-started.md#built-in-plugins
[dependencies for image processing]: dependencies/image-processing.md
[Puppeteer]: https://github.com/puppeteer/puppeteer
[Insiders]: ../insiders/index.md
[environment variable]: https://www.mkdocs.org/user-guide/configuration/#environment-variables
#### Social cards
2023-05-08 16:44:39 +02:00
The following configuration options are available for card generation:
2023-05-08 10:25:09 +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:
2023-05-08 10:25:09 +02:00
cards: !ENV [CI, false]
```
[`cards_dir`](#+social.cards_dir){ #+social.cards_dir }
: :octicons-milestone-24: Default: `assets/images/social` This option
specifies where the generated social cards will be stored. While it's
usually not necessary to change this option, change it with:
``` yaml
plugins:
- social:
cards_dir: assets/images/social
2021-10-20 23:02:55 +02:00
```
2021-07-25 15:40:40 +02:00
2023-05-08 16:44:39 +02:00
<div class="mdx-deprecated" markdown>
[`cards_color`](#+social.cards_color){ #+social.cards_color } <small>:material-trash-can: Deprecated, use [`cards_layout_options`][layout options]</small>
2021-08-07 13:12:05 +02:00
2023-05-08 10:25:09 +02:00
: :octicons-milestone-24: Default: [`theme.palette.primary`][primary color]
This option specifies the colors for the background `fill` and foreground
2022-09-13 13:27:13 +02:00
`text` when generating the social card:
2021-08-07 13:12:05 +02:00
``` yaml
plugins:
- social:
cards_color:
2023-05-08 10:25:09 +02:00
fill: "#0FF1CE"
2021-08-07 13:12:05 +02:00
text: "#FFFFFF"
```
2023-05-08 16:44:39 +02:00
[`cards_font`](#+social.cards_font){ #+social.cards_font } <small>:material-trash-can: Deprecated, use [`cards_layout_options`][layout options]</small>
2021-12-05 12:39:54 +01:00
2023-05-08 10:25:09 +02:00
: :octicons-milestone-24: Default: [`theme.font.text`][font] 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:
2023-05-08 10:25:09 +02:00
cards_font: Ubuntu
2021-12-05 12:39:54 +01:00
```
2023-05-08 16:44:39 +02:00
</div>
2022-09-18 16:03:45 +02:00
2023-05-08 10:25:09 +02:00
[`cards_layout_dir`](#+social.cards_layout_dir){ #+social.cards_layout_dir }
2022-09-18 16:03:45 +02:00
2023-05-08 10:25:09 +02:00
: [:octicons-tag-24: insiders-4.33.0][Insiders] · :octicons-milestone-24:
2023-05-08 16:44:39 +02:00
Default: _none_ This option specifies where the social plugin should try
to resolve [custom layouts] from, taking precedence over the included
layouts:
2022-09-18 16:03:45 +02:00
2023-05-08 10:25:09 +02:00
``` yaml
plugins:
- social:
2023-05-08 16:44:39 +02:00
cards_layout_dir: layouts
2023-05-08 10:25:09 +02:00
```
2022-09-18 16:03:45 +02:00
2023-05-08 10:25:09 +02:00
[`cards_layout`](#+social.cards_layout){ #+social.cards_layout } :material-alert-decagram:{ .mdx-pulse title="Added on May 8, 2023" }
2022-09-18 16:03:45 +02:00
2023-05-08 10:25:09 +02:00
: [:octicons-tag-24: insiders-4.33.0][Insiders] · :octicons-milestone-24:
Default: `default` Layout specification the social card should use. The
plugin includes the following layouts which make use of the [color palette]
and [font]:
2023-05-08 10:25:09 +02:00
=== "`default`"
2022-09-18 16:03:45 +02:00
2023-05-08 10:25:09 +02:00
``` yaml
plugins:
- social:
cards_layout: default
```
2023-05-08 10:25:09 +02:00
This layout uses the configured [primary color] as a background:
2022-09-18 16:03:45 +02:00
2023-05-08 10:25:09 +02:00
[![Layout default]][Layout default]
2023-05-08 10:25:09 +02:00
=== "`default/variant`"
2021-07-25 15:40:40 +02:00
2023-05-08 10:25:09 +02:00
``` yaml
plugins:
- social:
cards_layout: default/variant
```
2021-07-25 15:40:40 +02:00
2023-05-08 10:25:09 +02:00
This layout includes the [page icon] as a watermark, if defined:
2021-07-25 15:40:40 +02:00
2023-05-08 10:25:09 +02:00
[![Layout default variant]][Layout default variant]
2023-05-08 10:25:09 +02:00
=== "`default/accent`"
2021-10-20 23:02:55 +02:00
2023-05-08 10:25:09 +02:00
``` yaml
plugins:
- social:
cards_layout: default/accent
```
2021-10-20 23:02:55 +02:00
2023-05-08 10:25:09 +02:00
This layout uses the configured [accent color] as a background:
2021-10-20 23:02:55 +02:00
2023-05-08 10:25:09 +02:00
[![Layout default accent]][Layout default accent]
2021-10-20 23:02:55 +02:00
2023-05-08 10:25:09 +02:00
=== "`default/invert`"
2021-10-27 23:26:07 +02:00
2023-05-08 10:25:09 +02:00
``` yaml
plugins:
- social:
cards_layout: default/invert
```
2021-10-27 23:26:07 +02:00
2023-05-08 10:25:09 +02:00
This layout inverts the background and foreground colors:
2021-10-27 23:26:07 +02:00
2023-05-08 10:25:09 +02:00
[![Layout default invert]][Layout default invert]
2021-10-27 23:26:07 +02:00
2023-07-07 11:45:40 +02:00
=== "`default/only/image`"
``` yaml
plugins:
- social:
cards_layout: default/only/image
cards_layout_options:
background_image: layouts/background.png
```
This layout will only show the given background image and scale to fit:
[![Layer background image]][Layer background image]
2023-05-08 16:44:39 +02:00
All [`default`][default layouts] layouts make use of the following
[template variables]:
2021-10-27 23:26:07 +02:00
2023-05-08 10:25:09 +02:00
- :material-page-layout-header: `config.site_name`
- :material-page-layout-body: `page.meta.title` or `page.title`
- :material-page-layout-footer: `page.meta.description` or `config.site_description`
- :material-page-layout-sidebar-right: `theme.logo` or `theme.icon.logo`
2021-10-20 23:02:55 +02:00
2023-05-08 16:44:39 +02:00
[`cards_layout_options`](#+social.cards_layout_options){ #+social.cards_layout_options }
2021-10-27 23:26:07 +02:00
2023-05-20 17:23:53 +02:00
: [:octicons-tag-24: 9.1.10][Layout options support] · :octicons-milestone-24:
2023-05-08 10:25:09 +02:00
Default: _none_ This option allows to set [parameters] as provided by
the layout specification. For [custom layouts], this key can be used to
2023-05-20 17:23:53 +02:00
provide layout-specific options, making layouts entirely configurable.
2021-10-27 23:26:07 +02:00
2023-05-08 10:25:09 +02:00
---
2023-05-08 16:44:39 +02:00
All [`default`][default layouts] layouts expose the following parameters:
2021-10-27 23:26:07 +02:00
2023-05-08 16:44:39 +02:00
[`background_color`](#+social.cards_layout_options.background_color){ #+social.cards_layout_options.background_color }
2021-10-27 23:26:07 +02:00
2023-05-08 16:44:39 +02:00
: Set a background color, which can be a [CSS color keyword], or a 3, 4, 6
2023-05-08 10:25:09 +02:00
or 8 letter HEX color code. Alpha channels are supported as well:
``` yaml
plugins:
- social:
2023-05-08 16:44:39 +02:00
cards_layout_options:
2023-05-08 10:25:09 +02:00
background_color: "#0FF1CE"
2021-10-27 23:26:07 +02:00
```
2023-05-08 10:25:09 +02:00
2023-05-08 16:44:39 +02:00
[`background_image`](#+social.cards_layout_options.background_image){ #+social.cards_layout_options.background_image }
2023-05-08 10:25:09 +02:00
2023-05-12 21:57:50 +02:00
: [:octicons-tag-24: insiders-4.33.0][Insiders] Set a background image.
If a `background_color` is set, like for the
2023-05-08 10:25:09 +02:00
[`default`][default layouts] layouts, the image is tinted (overlayed)
with the color. Thus, the background color must be (partially)
transparent for the image to become visible:
``` yaml
plugins:
- social:
2023-05-08 16:44:39 +02:00
cards_layout_options:
2023-05-08 10:25:09 +02:00
background_color: "#00000000"
2023-05-08 16:44:39 +02:00
background_image: layouts/background.png
2021-10-27 23:26:07 +02:00
```
2023-05-08 10:25:09 +02:00
The path of the image must be defined relative to the project root.
2021-10-27 23:26:07 +02:00
2023-05-08 16:44:39 +02:00
[`color`](#+social.cards_layout_options.color){ #+social.cards_layout_options.color }
2023-05-08 10:25:09 +02:00
2023-05-08 16:44:39 +02:00
: Set a foreground color, which can be a [CSS color keyword], or a 3, 4, 6
2023-05-08 10:25:09 +02:00
or 8 letter HEX color code. The color is primarily used to tint text and
icons:
``` yaml
plugins:
- social:
2023-05-08 16:44:39 +02:00
cards_layout_options:
2023-05-08 10:25:09 +02:00
color: "#0FF1CE"
2021-10-27 23:26:07 +02:00
```
2023-05-08 10:25:09 +02:00
2023-05-08 16:44:39 +02:00
[`font_family`](#+social.cards_layout_options.font_family){ #+social.cards_layout_options.font_family }
2023-05-08 10:25:09 +02:00
: Set a font family. This overrides the [font] that is set as part of the
theme configuration. The [built-in social plugin] will automatically
download the font from [Google Fonts]:
``` yaml
plugins:
- social:
2023-05-08 16:44:39 +02:00
cards_layout_options:
2023-05-08 10:25:09 +02:00
font_family: Ubuntu
2021-10-27 23:26:07 +02:00
```
2021-10-20 23:02:55 +02:00
2023-08-26 13:59:23 +02:00
[`title`](#+social.cards_layout_options.title){ #+social.cards_layout_options.title }
: [:octicons-tag-24: insiders-4.40.0][Insiders] Set the social card
title, which takes precedence over `page.title` and `page.meta.title`:
``` yaml
plugins:
- social:
cards_layout_options:
title: Social card title
```
[`description`](#+social.cards_layout_options.description){ #+social.cards_layout_options.description }
: [:octicons-tag-24: insiders-4.40.0][Insiders] Set the social card
description, which takes precedence over `site_description` and
`page.meta.description`:
``` yaml
plugins:
- social:
cards_layout_options:
description: Social card description
```
[`logo`](#+social.cards_layout_options.logo){ #+social.cards_layout_options.logo }
: [:octicons-tag-24: insiders-4.40.0][Insiders] Set the logo used as
part of the social card, overriding the `theme.logo` or
`theme.icon.logo` settings which are used as defaults:
``` yaml
plugins:
- social:
cards_layout_options:
logo: layouts/logo.png
```
The path of the image must be defined relative to the project root.
2023-05-20 17:23:53 +02:00
[`cards_include`](#+privacy.cards_include){ #+privacy.cards_include }
: [:octicons-tag-24: insiders-4.35.0][Insiders] · :octicons-milestone-24:
Default: _none_ This option allows to only generate social cards for
certain subsections of your documentation, e.g. to generate different
cards for different subfolders with multiple instances of the plugin:
``` yaml
plugins:
- social:
cards_include:
- blog/*
```
[`cards_exclude`](#+privacy.cards_exclude){ #+privacy.cards_exclude }
: :octicons-milestone-24: Default: _none_ This option allows to exclude
certain subsections of your documentation from generating social cards:
``` yaml
plugins:
- social:
cards_exclude:
- changelog/*.md
```
2023-05-08 10:25:09 +02:00
[color palette]: ./changing-the-colors.md#color-palette
[primary color]: ./changing-the-colors.md#primary-color
[accent color]: ./changing-the-colors.md#accent-color
[font]: ./changing-the-fonts.md#regular-font
[Google Fonts]: https://fonts.google.com/
2023-05-08 16:44:39 +02:00
[layout options]: #+social.cards_layout_options
2023-05-08 10:25:09 +02:00
[page icon]: ../reference/index.md#setting-the-page-icon
[Layout default]: ../assets/screenshots/social-cards.png
[Layout default variant]: ../assets/screenshots/social-cards-variant.png
[Layout default accent]: ../assets/screenshots/social-cards-accent.png
[Layout default invert]: ../assets/screenshots/social-cards-invert.png
2023-05-08 16:44:39 +02:00
[template variables]: https://www.mkdocs.org/dev-guide/themes/#template-variables
2023-05-08 10:25:09 +02:00
[parameters]: #parameters
[default layouts]: #+social.cards_layout
[CSS color keyword]: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#color_keywords
2023-05-20 17:23:53 +02:00
[Layout options support]: https://github.com/squidfunk/mkdocs-material/releases/tag/9.1.10
2023-05-08 10:25:09 +02:00
2023-05-08 16:44:39 +02:00
#### Debugging
The following configuration options are available for debugging:
[`debug`](#+social.debug){ #+social.debug }
: [:octicons-tag-24: insiders-4.33.0][Insiders] · :octicons-milestone-24:
Default: `false` This option enables a special debug mode, which renders
each layer with an outline and its `x` and `y` offset in order to understand
how the layout is composed, and optionally renders a grid for easier
alignment:
``` yaml
plugins:
- social:
debug: true
```
=== "With debug mode"
[![Debug mode enabled]][Debug mode enabled]
=== "Without"
[![Debug mode disabled]][Debug mode disabled]
[Debug mode enabled]: ../assets/screenshots/social-cards-debug.png
[Debug mode disabled]: ../assets/screenshots/social-cards-variant.png
2023-05-20 10:57:22 +02:00
[`debug_on_build`](#+social.debug_on_build){ #+social.debug_on_build }
: [:octicons-tag-24: insiders-4.34.1][Insiders] · :octicons-milestone-24:
Default: `false` Whether debug mode should be automatically disabled
[when building your site] with `mkdocs build`. It can be changed with:
``` yaml
plugins:
- social:
debug_on_build: true
```
This setting is just intended to be a safety net, so that when building
your site social cards definitely won't contain the dot grid or layer
outlines by accident.
2023-05-08 16:44:39 +02:00
[`debug_grid`](#+social.debug_grid){ #+social.debug_grid }
: [:octicons-tag-24: insiders-4.33.0][Insiders] · :octicons-milestone-24:
Default: `true` This option enables the rendering of a dot grid when
[`debug`][debug] is enabled (see screenshot above). The grid can be switched
off with:
``` yaml
plugins:
- social:
debug_grid: false
```
[`debug_grid_step`](#+social.debug_grid_step){ #+social.debug_grid_step }
: [:octicons-tag-24: insiders-4.33.0][Insiders] · :octicons-milestone-24:
Default: `32` This option specifies the step size of the grid in pixels,
if enabled, which can be used to align elements. It can be changed with:
``` yaml
plugins:
- social:
debug_grid_step: 64
```
[`debug_color`](#+social.debug_color){ #+social.debug_color }
: [:octicons-tag-24: insiders-4.33.0][Insiders] · :octicons-milestone-24:
Default: `grey` This option sets the color of the layer outlines and
the grid which are rendered when [`debug`][debug] is enabled. It can be
changed with:
``` yaml
plugins:
- social:
debug_color: yellow
```
[debug]: #+social.debug
2023-05-20 10:57:22 +02:00
[when building your site]: ../creating-your-site.md#building-your-site
2023-05-08 16:44:39 +02:00
2023-05-08 10:25:09 +02:00
#### Caching
The [built-in social plugin] implements an intelligent caching mechanism,
ensuring that social cards are only re-generated when they're not contained in
the cache or their contents change. If any of the variables used in a layout
changes, the plugin will detect it and re-generate the card.
The following configuration options are available for caching:
[`cache`](#+social.cache){ #+social.cache }
: [:octicons-tag-24: insiders-4.33.0][Insiders] · :octicons-milestone-24:
Default: `true` Whether the plugin queries its cache for an existing
artifact before starting a generation job. It's normally not necessary to
change this setting, except for when debugging the plugin itself. Caching
can be disabled with:
``` yaml
plugins:
- social:
cache: false
```
2023-05-08 10:25:09 +02:00
[`cache_dir`](#+social.cache_dir){ #+social.cache_dir }
2023-05-08 10:25:09 +02:00
: :octicons-milestone-24: Default: `.cache/plugins/social` This option
specifies the file system location of the plugin's cache. It's normally not
necessary to change this setting, except for when debugging the plugin
itself. The cache directory can be changed with:
2023-05-08 10:25:09 +02:00
``` yaml
plugins:
- social:
cache_dir: .cache/plugins/social
```
2023-05-08 10:25:09 +02:00
By default, all built-in plugins that implement caching will create a
`.cache` directory in the same folder your `mkdocs.yml` resides, and create
subfolders to not interfere with each other. If you use multiple instances
of this plugin, it could be necessary to change this setting.
[built-in social plugin]: #built-in-social-plugin
[publishing guide]: ../publishing-your-site.md#with-github-actions
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,
2023-08-26 13:59:23 +02:00
you can set the front matter [`title`][Changing the title] and
[`description`][Changing the description] properties, which take precedence over
the defaults, or use:
2021-10-10 19:22:13 +02:00
2023-08-26 13:59:23 +02:00
- [`cards_layout_options.title`](#+social.cards_layout_options.title)
- [`cards_layout_options.description`](#+social.cards_layout_options.description)
2021-10-10 19:22:13 +02:00
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
2023-05-08 10:25:09 +02:00
### Choosing a font
Some fonts do not contain CJK characters, like for example the
[default font, `Roboto`][font]. 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:
2023-07-07 11:45:40 +02:00
cards_layout_options:
font_family: Noto Sans SC
2023-05-08 10:25:09 +02:00
```
=== "Chinese (Traditional)"
``` yaml
plugins:
- social:
2023-07-07 11:45:40 +02:00
cards_layout_options:
font_family: Noto Sans TC
2023-05-08 10:25:09 +02:00
```
=== "Japanese"
``` yaml
plugins:
- social:
2023-07-07 11:45:40 +02:00
cards_layout_options:
font_family: Noto Sans JP
2023-05-08 10:25:09 +02:00
```
=== "Korean"
``` yaml
plugins:
- social:
2023-07-07 11:45:40 +02:00
cards_layout_options:
font_family: Noto Sans KR
2023-05-08 10:25:09 +02:00
```
2023-07-07 11:45:40 +02:00
### Changing the layout
[:octicons-tag-24: insiders-4.37.0][Insiders] ·
:octicons-beaker-24: Experimental
If you want to use a different layout for a single page (e.g. your landing
page), you can use the `social` front matter property together with the
[`cards_layout`](#+social.cards_layout) key, exactly as in `mkdocs.yml`:
``` yaml
---
social:
cards_layout: custom
---
# Headline
...
```
You can apply those changes for entire subtrees of your documentation, e.g.,
to generate different social cards for your blog and API reference, by using
the [built-in meta plugin].
[built-in meta plugin]: ../reference/index.md#built-in-meta-plugin
### Parametrizing the layout
[:octicons-tag-24: insiders-4.37.0][Insiders] ·
:octicons-beaker-24: Experimental
Besides changing the entire layout, you can override all options that a layout
exposes. This means you can parametrize social cards with custom front matter
properties, such as `tags`, `date`, `author` or anything you can think of.
Simply define [`cards_layout_options`](#+social.cards_layout_options):
``` yaml
---
social:
cards_layout_options:
background_color: blue # Change background color
background_image: null # Remove background image
---
# Headline
...
```
You can apply those changes for entire subtrees of your documentation, e.g.,
to generate different social cards for your blog and API reference, by using
the [built-in meta plugin].
### Disabling social cards
[:octicons-tag-24: insiders-4.37.0][Insiders] ·
:octicons-beaker-24: Experimental
If you wish to disable social cards for a page, simply add the following to the
front matter of the Markdown document:
``` yaml
---
social:
cards: false
---
# Headline
...
```
2023-05-08 10:25:09 +02:00
## Customization
[:octicons-heart-fill-24:{ .mdx-heart } Sponsors only][Insiders]{ .mdx-insiders } ·
[:octicons-tag-24: insiders-4.33.0][Insiders] ·
:octicons-beaker-24: Experimental
2023-05-08 16:44:39 +02:00
[Insiders] ships a ground up rewrite of the [built-in social plugin] and
introduces a brand new layout system based on a combination of YAML and
[Jinja templates] the same engine Material for MkDocs uses for HTML
templating allowing for the creation of complex custom layouts:
<div class="mdx-social">
<div class="mdx-social__layer">
<div class="mdx-social__image">
<span class="mdx-social__label">Layer 0</span>
<img src="../../assets/screenshots/social-cards-layer-0.png" />
</div>
</div>
<div class="mdx-social__layer">
<div class="mdx-social__image">
<span class="mdx-social__label">Layer 1</span>
<img src="../../assets/screenshots/social-cards-layer-1.png" />
</div>
</div>
<div class="mdx-social__layer">
<div class="mdx-social__image">
<span class="mdx-social__label">Layer 2</span>
<img src="../../assets/screenshots/social-cards-layer-2.png" />
</div>
</div>
<div class="mdx-social__layer">
<div class="mdx-social__image">
<span class="mdx-social__label">Layer 3</span>
<img src="../../assets/screenshots/social-cards-layer-3.png" />
</div>
</div>
<div class="mdx-social__layer">
<div class="mdx-social__image">
<span class="mdx-social__label">Layer 4</span>
<img src="../../assets/screenshots/social-cards-layer-4.png" />
</div>
</div>
<div class="mdx-social__layer">
<div class="mdx-social__image">
<span class="mdx-social__label">Layer 5</span>
<img src="../../assets/screenshots/social-cards-layer-5.png" />
</div>
</div>
</div>
Social cards are composed of layers, analogous to how they are represented in
graphic design software such as Adobe Photoshop. As many layers are common
across the cards generated for each page (e.g., backgrounds or logos), the
built-in social plugin can automatically deduplicate layers and render them
just once, substantially accelerating card generation. The generated cards are
cached to ensure they are only regenerated when their contents change.
Layouts are written in YAML syntax. Before starting to create a custom layout,
it is a good idea to [study the pre-designed layouts] (link to [Insiders]
repository), in order to get a better understanding of how they work. Then,
create a new layout and reference it in `mkdocs.yml`:
=== ":octicons-file-code-16: `layouts/custom.yml`"
``` yaml
size: { width: 1200, height: 630 }
layers: []
```
=== ":octicons-file-code-16: `mkdocs.yml`"
``` yaml
plugins:
- social:
cards_layout_dir: layouts
cards_layout: custom
debug: true
```
Note that the `.yml` file extension should be omitted. Next, run `mkdocs serve`,
and see how the `.cache` directory is populated with the generated cards. Open
any card in your editor, so you can see your changes immediately. Since we
haven't defined any layers, the cards are transparent.
The following sections explain how to create custom layouts.
[Jinja templates]: https://jinja.palletsprojects.com/en/3.1.x/
[study the pre-designed layouts]: https://github.com/squidfunk/mkdocs-material-insiders/tree/master/src/plugins/social/layouts
### Size and offset
Each layer has an associated size and offset, which is defined in pixels. The
`size` is defined by a `width` and `height` property, and the `offset` by `x`
and `y` properties:
``` yaml
size: { width: 1200, height: 630 }
layers:
- size: { width: 1200, height: 630 }
offset: { x: 0, y: 0 }
```
If the `size` is omitted, it defaults to the size of the layout. If the `offset`
2023-05-20 17:23:53 +02:00
is omitted, it defaults to the top left corner, which is the defaut `origin`.
Saving the layout and reloading renders:
2023-05-08 16:44:39 +02:00
![Layer size]
The layer outline and grid are visible because we enabled [`debug`][debug]
mode in `mkdocs.yml`. The top left shows the layer index and offset, which is
useful for alignment and composition.
[Layer size]: ../assets/screenshots/social-cards-layer-size.png
2023-05-20 17:23:53 +02:00
#### Origin
[:octicons-tag-24: insiders-4.35.0][Insiders] ·
:octicons-beaker-24: Experimental
The `origin` for the `x` and `y` values can be changed, so that the layer is
aligned to one of the edges or corners of the layout, e.g., to the bottom right
corner of the layout:
``` yaml hl_lines="5"
size: { width: 1200, height: 630 }
layers:
- size: { width: 1200, height: 630 }
offset: { x: 0, y: 0 }
origin: end bottom
```
The following table shows the supported values:
<figure markdown>
| Origin | | |
| -------------- | --------------- | ------------ |
| :material-arrow-top-left: `start top` | :material-arrow-up: `center top` | :material-arrow-top-right: `end top` |
| :material-arrow-left: `start center` | :material-circle-small: `center` | :material-arrow-right: `end center` |
| :material-arrow-bottom-left: `start bottom` | :material-arrow-down: `center bottom` | :material-arrow-bottom-right: `end bottom` |
<figcaption>
Supported values for origin
</figcaption>
</figure>
2023-05-08 16:44:39 +02:00
### Backgrounds
Each layer can be assigned a background color and image. If both are given, the
color is rendered on top of the image, allowing for semi-transparent, tinted
backgrounds:
=== "Background color"
``` yaml
size: { width: 1200, height: 630 }
layers:
- background:
color: "#4051b5"
```
![Layer background color]
=== "Background image"
``` yaml
size: { width: 1200, height: 630 }
layers:
- background:
2023-07-07 11:45:40 +02:00
image: layouts/background.png
2023-05-08 16:44:39 +02:00
```
![Layer background image]
=== "Background image, tinted"
``` yaml
size: { width: 1200, height: 630 }
layers:
- background:
2023-07-07 11:45:40 +02:00
image: layouts/background.png
2023-05-08 16:44:39 +02:00
color: "#4051b5ee" # (1)!
```
1. The color value can be set to a [CSS color keyword], or a 3, 4, 6 or 8
letter HEX color code, allowing for semi-transparent layers.
![Layer background]
Background images are automatically scaled to fit the layer while preserving
aspect-ratio. Notice how we omitted `size` and `offset`, because we want to
fill the entire area of the social card.
[Layer background color]: ../assets/screenshots/social-cards-layer-background-color.png
[Layer background image]: ../assets/screenshots/social-cards-layer-background-image.png
[Layer background]: ../assets/screenshots/social-cards-layer-background.png
### Typography
Now, we can add dynamic typography that is sourced from Markdown files - this is
the actual raison d'être of the [built-in social plugin]. [Jinja templates] are
used to render a text string that is then added to the image:
``` yaml
size: { width: 1200, height: 630 }
layers:
- size: { width: 832, height: 310 }
offset: { x: 62, y: 160 }
typography:
content: "{{ page.title }}" # (1)!
align: start
color: white
line:
amount: 3
height: 1.25
font:
family: Roboto
style: Bold
```
1. The following variables can be used in [Jinja templates]:
- [`config.*`][config variable]
- [`page.*`][page variable]
- [`layout.*`][layout options]
The author is free in defining `layout.*` options, which can be used to pass
arbitrary data to the layout from `mkdocs.yml`.
This renders a text layer with the title of the page with a line height of 1.25,
and a maximum number of 3 lines. The plugin automatically computes the font size
from the line height, the number of lines, and font metrics like ascender and
descender.[^2] This renders:
[^2]:
If the plugin would require the author to specify the font size and line
height manually, it would be impossible to guarantee that the text fits
into the layer. For this reason we implemented a declarative approach,
where the author specifies the desired line height and number of lines, and
the plugin computes the font size automatically.
![Layer typography]
[config variable]: https://www.mkdocs.org/dev-guide/themes/#config
[page variable]: https://www.mkdocs.org/dev-guide/themes/#page
[Layer typography]: ../assets/screenshots/social-cards-layer-typography.png
#### Overflow
2023-05-14 18:02:18 +02:00
If the text overflows the layer, there are two possible behaviors: either the
text is automatically truncated and shortened with an ellipsis, or the text is
automatically scaled down to fit the layer:
2023-05-08 16:44:39 +02:00
``` { .markdown .no-copy }
# If we use a very long headline, we can see how the text will be truncated
```
2023-05-14 18:02:18 +02:00
=== ":octicons-ellipsis-16: Ellipsis"
![Layer typography ellipsis]
=== ":material-arrow-collapse: Shrink"
![Layer typography shrink]
While truncating with an ellipsis is the default, auto-shrinking can be enabled
by setting `overflow` to `shrink`:
``` yaml hl_lines="7"
size: { width: 1200, height: 630 }
layers:
- size: { width: 832, height: 310 }
offset: { x: 62, y: 160 }
typography:
content: "{{ page.title }}"
overflow: shrink
align: start
color: white
line:
amount: 3
height: 1.25
font:
family: Roboto
style: Bold
```
2023-05-08 16:44:39 +02:00
[Layer typography ellipsis]: ../assets/screenshots/social-cards-layer-typography-ellipsis.png
2023-05-14 18:02:18 +02:00
[Layer typography shrink]: ../assets/screenshots/social-cards-layer-typography-shrink.png
2023-05-08 16:44:39 +02:00
#### Alignment
Text can be aligned to all corners and edges of the layer. For example, if we
want to align the text to the middle of the layer, we can set `align` to `start center`, which will render as:
![Layer typography align]
[Layer typography align]: ../assets/screenshots/social-cards-layer-typography-align.png
The following table shows the supported values:
<figure markdown>
| Alignment | | |
| -------------- | --------------- | ------------ |
| :material-arrow-top-left: `start top` | :material-arrow-up: `center top` | :material-arrow-top-right: `end top` |
| :material-arrow-left: `start center` | :material-circle-small: `center` | :material-arrow-right: `end center` |
| :material-arrow-bottom-left: `start bottom` | :material-arrow-down: `center bottom` | :material-arrow-bottom-right: `end bottom` |
<figcaption>
Supported values for text alignment
</figcaption>
</figure>
#### Font
The [built-in social plugin] integrates with [Google Fonts] and will
automatically download the font files for you. The `font` property accepts a
`family` and `style` property, where the `family` must be set to the name of the
font, and the `style` to one of the supported font styles. For example, setting
`family` to `Roboto` will automatically download the following files:
``` { .sh .no-copy #example }
.cache/plugins/social/fonts
└─ Roboto/
├─ Black.ttf
├─ Black Italic.ttf
├─ Bold.ttf
├─ Bold Italic.ttf
├─ Italic.ttf
├─ Light.ttf
├─ Light Italic.ttf
├─ Medium.ttf
├─ Medium Italic.ttf
├─ Regular.ttf
├─ Thin.ttf
└─ Thin Italic.ttf
```
In that case, the author can use `Bold` or `Medium Italic` as the `style`. If
the font style specified in the layer is not part of the font family, the
font always falls back to `Regular` and prints a warning in [`debug`][debug]
mode, as `Regular` is included with all font families.
### Icons
Authors can leverage the full range of icons that are shipped with Material for
MkDocs, or even provide custom icons by using theme extension and going through
the process described in the guide on [additional icons]. Icons can even be
tinted by using the `color` property:
``` yaml
size: { width: 1200, height: 630 }
layers:
- background:
color: "#4051b5"
- size: { width: 144, height: 144 }
offset: { x: 992, y: 64 }
icon:
value: material/cat
color: white
```
This will render the icon in the top right corner of the social card:
![Layer icon]
The possibilities are endless. For example, icons can be used to draw shapes
like circles:
``` yaml
size: { width: 1200, height: 630 }
layers:
- background:
color: "#4051b5"
- size: { width: 2400, height: 2400 }
offset: { x: -1024, y: 64 }
icon:
value: material/circle
color: "#5c6bc0"
- size: { width: 1800, height: 1800 }
offset: { x: 512, y: -1024 }
icon:
value: material/circle
color: "#3949ab"
```
This will add two circles to the background:
![Layer icon circles]
2023-05-08 10:25:09 +02:00
2023-07-07 11:55:18 +02:00
### Tags
The new [built-in social plugin] gives full flexibility of the meta tags that
are added to your site, which are necessary to instruct services like Twitter
or Discord how to display your social card. All default layouts use the following
set of tags, which you can copy to your layout and adapt:
``` yaml
definitions:
- &page_title_with_site_name >-
{%- if not page.is_homepage -%}
{{ page.meta.get("title", page.title) }} - {{ config.site_name }}
{%- else -%}
{{ page.meta.get("title", page.title) }}
{%- endif -%}
- &page_description >-
{{ page.meta.get("description", config.site_description) or "" }}
tags:
og:type: website
og:title: *page_title_with_site_name
og:description: *page_description
og:image: "{{ image.url }}"
og:image:type: "{{ image.type }}"
og:image:width: "{{ image.width }}"
og:image:height: "{{ image.height }}"
og:url: "{{ page.canonical_url }}"
twitter:card: summary_large_image
twitter.title: *page_title_with_site_name
twitter:description: *page_description
twitter:image: "{{ image.url }}"
```
Note that this examples makes use of [YAML anchors] to minify repetition. The
`definitions` property is solely intended for the definition on aliases that
can then be referenced with anchors.
[YAML anchors]: https://support.atlassian.com/bitbucket-cloud/docs/yaml-anchors/
2023-05-08 16:44:39 +02:00
__Are you missing something? Please [open a discussion] and let us know!__
2023-05-08 10:25:09 +02:00
2023-05-08 16:44:39 +02:00
[additional icons]: ./changing-the-logo-and-icons.md#additional-icons
[Layer icon]: ../assets/screenshots/social-cards-layer-icon.png
[Layer icon circles]: ../assets/screenshots/social-cards-layer-icon-circles.png
[open a discussion]: https://github.com/squidfunk/mkdocs-material/discussions/new