| 1 |
=================== |
| 2 |
sphinxext-opengraph |
| 3 |
=================== |
| 4 |
|
| 5 |
.. toctree:: |
| 6 |
:hidden: |
| 7 |
|
| 8 |
socialcards |
| 9 |
|
| 10 |
.. role:: code-py(code) |
| 11 |
:language: Python |
| 12 |
|
| 13 |
Sphinx extension to generate `Open Graph metadata`_ for your documentation. |
| 14 |
|
| 15 |
|
| 16 |
Installation |
| 17 |
============ |
| 18 |
|
| 19 |
.. code-block:: sh |
| 20 |
|
| 21 |
python -m pip install sphinxext-opengraph |
| 22 |
|
| 23 |
The ``matplotlib`` package is required to generate social cards: |
| 24 |
|
| 25 |
.. code-block:: sh |
| 26 |
|
| 27 |
python -m pip install sphinxext-opengraph[social_cards] |
| 28 |
|
| 29 |
|
| 30 |
Usage |
| 31 |
===== |
| 32 |
|
| 33 |
Add ``sphinxext.opengraph`` to the extensions list in ``conf.py``: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
extensions = [ |
| 38 |
'sphinxext.opengraph', |
| 39 |
] |
| 40 |
|
| 41 |
Then set the :confval:`!ogp_site_url` configuration value |
| 42 |
to the public URL for your documentation, e.g.: |
| 43 |
|
| 44 |
.. code-block:: python |
| 45 |
|
| 46 |
ogp_site_url = 'https://docs.example.org/' |
| 47 |
|
| 48 |
.. tip:: |
| 49 |
Documentation hosted on Read The Docs automatically detects the site URL |
| 50 |
through the :envvar:`!READTHEDOCS_CANONICAL_URL` environment variable, |
| 51 |
so no configuration is needed. |
| 52 |
|
| 53 |
|
| 54 |
Options |
| 55 |
======= |
| 56 |
|
| 57 |
These values are placed in the ``conf.py`` of your Sphinx project. |
| 58 |
|
| 59 |
.. confval:: ogp_site_url |
| 60 |
:type: :code-py:`str` |
| 61 |
:default: :code-py:`''` |
| 62 |
|
| 63 |
This config option is important, set it to the URL the site is being hosted on. |
| 64 |
|
| 65 |
.. confval:: ogp_description_length |
| 66 |
:type: :code-py:`int` |
| 67 |
:default: :code-py:`200` |
| 68 |
|
| 69 |
Configure the number of characters taken from a page. |
| 70 |
|
| 71 |
.. confval:: ogp_site_name |
| 72 |
:type: :code-py:`str | Literal[False]` |
| 73 |
:default: :confval:`!project` |
| 74 |
|
| 75 |
Name of the site. |
| 76 |
This is displayed above the title. |
| 77 |
Defaults to the :confval:`!project` config value. |
| 78 |
Set to ``False`` to unset and use no default. |
| 79 |
|
| 80 |
.. confval:: ogp_social_cards |
| 81 |
:type: :code-py:`dict[str, bool | str]` |
| 82 |
:default: :code-py:`{}` |
| 83 |
|
| 84 |
Configuration for automatically creating social media card PNGs for each page. |
| 85 |
See :doc:`the social media cards <socialcards>` page for more information. |
| 86 |
|
| 87 |
.. confval:: ogp_image |
| 88 |
:type: :code-py:`str | None` |
| 89 |
:default: :code-py:`None` |
| 90 |
|
| 91 |
Link to image to show. |
| 92 |
Note that all relative paths are converted to be relative to |
| 93 |
the root of the HTML output as defined by :confval:`!ogp_site_url`. |
| 94 |
|
| 95 |
.. confval:: ogp_image_alt |
| 96 |
:type: :code-py:`str | Literal[False]` |
| 97 |
:default: :code-py:`None` |
| 98 |
|
| 99 |
Alt text for image. |
| 100 |
Defaults to using :confval:`!ogp_site_name` or the document's title as alt text, |
| 101 |
if available. |
| 102 |
Set to ``False`` to disable alt text. |
| 103 |
|
| 104 |
.. confval:: ogp_use_first_image |
| 105 |
:type: :code-py:`bool` |
| 106 |
:default: :code-py:`False` |
| 107 |
|
| 108 |
Set to ``True`` to use each page's first image, if available. |
| 109 |
If set to ``True`` but no image is found, Sphinx will use :confval:`!ogp_image` instead. |
| 110 |
|
| 111 |
.. confval:: ogp_type |
| 112 |
:type: :code-py:`str` |
| 113 |
:default: :code-py:`'website'` |
| 114 |
|
| 115 |
This sets the ogp type attribute. |
| 116 |
For more information on the types available, see at https://ogp.me/#types. |
| 117 |
By default, it is set to ``website``, which should be fine for most use cases. |
| 118 |
|
| 119 |
.. confval:: ogp_custom_meta_tags |
| 120 |
:type: :code-py:`Sequence[str]` |
| 121 |
:default: :code-py:`()` |
| 122 |
|
| 123 |
List of custom HTML snippets to insert. |
| 124 |
|
| 125 |
.. confval:: ogp_enable_meta_description |
| 126 |
:type: :code-py:`bool` |
| 127 |
:default: :code-py:`True` |
| 128 |
|
| 129 |
When ``True``, generates ``<meta name="description" content="...">`` from the page. |
| 130 |
|
| 131 |
.. confval:: ogp_canonical_url |
| 132 |
:type: :code-py:`str` |
| 133 |
:default: :confval:`!ogp_site_url` |
| 134 |
|
| 135 |
This option can be used to override the "canonical" URL for the page, |
| 136 |
and is used for ``og:url`` and the URL text in generated social media preview cards. |
| 137 |
It is most useful with versioned documentation, where you intend |
| 138 |
to set the "stable" or "latest" version as the canonical location of each page, |
| 139 |
similarly to ``rel="canonical"``. |
| 140 |
If not set, the option defaults to the value of :confval:`!ogp_site_url`. |
| 141 |
|
| 142 |
|
| 143 |
Example Config |
| 144 |
============== |
| 145 |
|
| 146 |
Simple Config |
| 147 |
------------- |
| 148 |
|
| 149 |
.. code-block:: python |
| 150 |
|
| 151 |
ogp_site_url = "http://example.org/" |
| 152 |
ogp_image = "http://example.org/image.png" |
| 153 |
|
| 154 |
Advanced Config |
| 155 |
--------------- |
| 156 |
|
| 157 |
.. code-block:: python |
| 158 |
|
| 159 |
ogp_site_url = "http://example.org/" |
| 160 |
ogp_image = "http://example.org/image.png" |
| 161 |
ogp_description_length = 300 |
| 162 |
ogp_type = "article" |
| 163 |
|
| 164 |
ogp_custom_meta_tags = [ |
| 165 |
'<meta property="og:ignore_canonical" content="true" />', |
| 166 |
] |
| 167 |
|
| 168 |
ogp_enable_meta_description = True |
| 169 |
|
| 170 |
|
| 171 |
Per Page Overrides |
| 172 |
================== |
| 173 |
|
| 174 |
`Field lists`_ can be used to override certain settings on each page |
| 175 |
and set unsupported arbitrary Open Graph tags. |
| 176 |
|
| 177 |
Make sure you place the fields at the very start of the document |
| 178 |
such that Sphinx will pick them up and also won't build them into the HTML. |
| 179 |
|
| 180 |
Overrides |
| 181 |
--------- |
| 182 |
|
| 183 |
These are some overrides that can be used on individual pages, |
| 184 |
you can actually override any tag and field lists will always take priority. |
| 185 |
|
| 186 |
``:ogp_description_length:`` |
| 187 |
Configure the amount of characters to grab for the description of the page. |
| 188 |
If the value isn't a number it will fall back to ``ogp_description_length``. |
| 189 |
|
| 190 |
``:ogp_disable:`` |
| 191 |
Disables generation of Open Graph tags on the page. |
| 192 |
|
| 193 |
``:og:description:`` |
| 194 |
Lets you override the description of the page. |
| 195 |
|
| 196 |
``:description:`` or ``.. meta:: :description:`` |
| 197 |
Sets the ``<meta name="description" content="...">`` description. |
| 198 |
|
| 199 |
``:og:title:`` |
| 200 |
Lets you override the title of the page. |
| 201 |
|
| 202 |
``:og:type:`` |
| 203 |
Override the type of the page. |
| 204 |
For the list of available types, see at https://ogp.me/#types. |
| 205 |
|
| 206 |
``:og:image:`` |
| 207 |
Set the image for the page. |
| 208 |
|
| 209 |
**Note: Relative file paths for images, videos and audio |
| 210 |
are currently not supported when using field lists. |
| 211 |
Please use an absolute path instead.** |
| 212 |
|
| 213 |
``:og:image:alt:`` |
| 214 |
Sets the alt text. Will be ignored if there is no image set. |
| 215 |
|
| 216 |
Example |
| 217 |
------- |
| 218 |
|
| 219 |
Remember that the fields **must** be placed at the very start of the file. |
| 220 |
You can verify Sphinx has picked up the fields if they aren't shown |
| 221 |
in the final HTML file. |
| 222 |
|
| 223 |
.. code-block:: rst |
| 224 |
|
| 225 |
:og:description: New description |
| 226 |
:og:image: http://example.org/image.png |
| 227 |
:og:image:alt: Example Image |
| 228 |
|
| 229 |
Page contents |
| 230 |
============= |
| 231 |
|
| 232 |
Arbitrary Tags |
| 233 |
-------------- |
| 234 |
|
| 235 |
Additionally, you can use field lists to add any arbitrary Open Graph tag |
| 236 |
not supported by the extension. |
| 237 |
The syntax for arbitrary tags is the same with ``:og:tag: content``. |
| 238 |
|
| 239 |
For example: |
| 240 |
|
| 241 |
.. code-block:: rst |
| 242 |
|
| 243 |
:og:video: http://example.org/video.mp4 |
| 244 |
|
| 245 |
Page contents |
| 246 |
============= |
| 247 |
|
| 248 |
**Note: Relative file paths for images, videos and audio |
| 249 |
are currently not supported when using field lists. |
| 250 |
Please use an absolute path instead.** |
| 251 |
|
| 252 |
.. _Open Graph metadata: https://ogp.me/ |
| 253 |
.. _Field lists: https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html |