mkdocs-material/docs/setup/setting-up-social-cards.md
2021-07-25 20:37:57 +02:00

3.8 KiB
Raw Blame History

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, fonts and logo1 defined in mkdocs.yml.

Configuration

Built-in social cards

:octicons-file-code-24: Source · :octicons-cpu-24: Plugin · :octicons-beaker-24: Experimental · :octicons-heart-fill-24:{ .mdx-heart } Insiders only{ .mdx-insiders }

The built-in social cards plugin generates a social card image for every page and adds the necessary meta tags, so it's displayed on social media when shared. Enable it via mkdocs.yml:

plugins:
  - social

For example, the page on setting up site analytics renders as:

Social Cards

Want to try it out? Copy the current URL and paste it into Twitter's Card validator to see how social cards look in action.

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:

cards_directory{ #cards_directory }

: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.

plugins:
  - social:
      cards_directory: assets/images/social

Caching

When enabled, the social cards plugin 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:

  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, add the following lines:

    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
    

Usage

If you want to adjust the title or set a custom description for the social card, you can use the Metadata extension, which takes precedence over the default values.


  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. ↩︎