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