File: index.rst

package info (click to toggle)
sphinxext-opengraph 0.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,200 kB
  • sloc: python: 1,130; makefile: 11; sh: 8
file content (253 lines) | stat: -rw-r--r-- 6,643 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
===================
sphinxext-opengraph
===================

.. toctree::
   :hidden:

   socialcards

.. role:: code-py(code)
   :language: Python

Sphinx extension to generate `Open Graph metadata`_ for your documentation.


Installation
============

.. code-block:: sh

   python -m pip install sphinxext-opengraph

The ``matplotlib`` package is required to generate social cards:

.. code-block:: sh

   python -m pip install sphinxext-opengraph[social_cards]


Usage
=====

Add ``sphinxext.opengraph`` to the extensions list in ``conf.py``:

.. code-block:: python

   extensions = [
      'sphinxext.opengraph',
   ]

Then set the :confval:`!ogp_site_url` configuration value
to the public URL for your documentation, e.g.:

.. code-block:: python

   ogp_site_url = 'https://docs.example.org/'

.. tip::
   Documentation hosted on Read The Docs automatically detects the site URL
   through the :envvar:`!READTHEDOCS_CANONICAL_URL` environment variable,
   so no configuration is needed.


Options
=======

These values are placed in the ``conf.py`` of your Sphinx project.

.. confval:: ogp_site_url
   :type: :code-py:`str`
   :default: :code-py:`''`

   This config option is important, set it to the URL the site is being hosted on.

.. confval:: ogp_description_length
   :type: :code-py:`int`
   :default: :code-py:`200`

   Configure the number of characters taken from a page.

.. confval:: ogp_site_name
   :type: :code-py:`str | Literal[False]`
   :default: :confval:`!project`

   Name of the site.
   This is displayed above the title.
   Defaults to the :confval:`!project` config value.
   Set to ``False`` to unset and use no default.

.. confval:: ogp_social_cards
   :type: :code-py:`dict[str, bool | str]`
   :default: :code-py:`{}`

   Configuration for automatically creating social media card PNGs for each page.
   See :doc:`the social media cards <socialcards>` page for more information.

.. confval:: ogp_image
   :type: :code-py:`str | None`
   :default: :code-py:`None`

   Link to image to show.
   Note that all relative paths are converted to be relative to
   the root of the HTML output as defined by :confval:`!ogp_site_url`.

.. confval:: ogp_image_alt
   :type: :code-py:`str | Literal[False]`
   :default: :code-py:`None`

   Alt text for image.
   Defaults to using :confval:`!ogp_site_name` or the document's title as alt text,
   if available.
   Set to ``False`` to disable alt text.

.. confval:: ogp_use_first_image
   :type: :code-py:`bool`
   :default: :code-py:`False`

   Set to ``True`` to use each page's first image, if available.
   If set to ``True`` but no image is found, Sphinx will use :confval:`!ogp_image` instead.

.. confval:: ogp_type
   :type: :code-py:`str`
   :default: :code-py:`'website'`

   This sets the ogp type attribute.
   For more information on the types available, see at https://ogp.me/#types.
   By default, it is set to ``website``, which should be fine for most use cases.

.. confval:: ogp_custom_meta_tags
   :type: :code-py:`Sequence[str]`
   :default: :code-py:`()`

   List of custom HTML snippets to insert.

.. confval:: ogp_enable_meta_description
   :type: :code-py:`bool`
   :default: :code-py:`True`

   When ``True``, generates ``<meta name="description" content="...">`` from the page.

.. confval:: ogp_canonical_url
   :type: :code-py:`str`
   :default: :confval:`!ogp_site_url`

   This option can be used to override the "canonical" URL for the page,
   and is used for ``og:url`` and the URL text in generated social media preview cards.
   It is most useful with versioned documentation, where you intend
   to set the "stable" or "latest" version as the canonical location of each page,
   similarly to ``rel="canonical"``.
   If not set, the option defaults to the value of :confval:`!ogp_site_url`.


Example Config
==============

Simple Config
-------------

.. code-block:: python

   ogp_site_url = "http://example.org/"
   ogp_image = "http://example.org/image.png"

Advanced Config
---------------

.. code-block:: python

   ogp_site_url = "http://example.org/"
   ogp_image = "http://example.org/image.png"
   ogp_description_length = 300
   ogp_type = "article"

   ogp_custom_meta_tags = [
       '<meta property="og:ignore_canonical" content="true" />',
   ]

   ogp_enable_meta_description = True


Per Page Overrides
==================

`Field lists`_ can be used to override certain settings on each page
and set unsupported arbitrary Open Graph tags.

Make sure you place the fields at the very start of the document
such that Sphinx will pick them up and also won't build them into the HTML.

Overrides
---------

These are some overrides that can be used on individual pages,
you can actually override any tag and field lists will always take priority.

``:ogp_description_length:``
  Configure the amount of characters to grab for the description of the page.
  If the value isn't a number it will fall back to ``ogp_description_length``.

``:ogp_disable:``
  Disables generation of Open Graph tags on the page.

``:og:description:``
  Lets you override the description of the page.

``:description:`` or ``.. meta:: :description:``
  Sets the ``<meta name="description" content="...">`` description.

``:og:title:``
  Lets you override the title of the page.

``:og:type:``
  Override the type of the page.
  For the list of available types, see at https://ogp.me/#types.

``:og:image:``
  Set the image for the page.

  **Note: Relative file paths for images, videos and audio
  are currently not supported when using field lists.
  Please use an absolute path instead.**

``:og:image:alt:``
  Sets the alt text. Will be ignored if there is no image set.

Example
-------

Remember that the fields **must** be placed at the very start of the file.
You can verify Sphinx has picked up the fields if they aren't shown
in the final HTML file.

.. code-block:: rst

   :og:description: New description
   :og:image: http://example.org/image.png
   :og:image:alt: Example Image

   Page contents
   =============

Arbitrary Tags
--------------

Additionally, you can use field lists to add any arbitrary Open Graph tag
not supported by the extension.
The syntax for arbitrary tags is the same with ``:og:tag: content``.

For example:

.. code-block:: rst

   :og:video: http://example.org/video.mp4

   Page contents
   =============

**Note: Relative file paths for images, videos and audio
are currently not supported when using field lists.
Please use an absolute path instead.**

.. _Open Graph metadata: https://ogp.me/
.. _Field lists: https://www.sphinx-doc.org/en/master/usage/restructuredtext/field-lists.html