1
1
from __future__ import annotations
2
2
3
3
import os
4
+ import posixpath
4
5
from pathlib import Path
5
6
from typing import TYPE_CHECKING
6
7
from urllib .parse import urljoin , urlparse , urlsplit , urlunsplit
@@ -100,11 +101,16 @@ def get_tags(
100
101
tags ["og:type" ] = config .ogp_type
101
102
102
103
if not config .ogp_site_url and os .getenv ("READTHEDOCS" ):
103
- config .ogp_site_url = read_the_docs_site_url (config .html_baseurl )
104
+ ogp_site_url = read_the_docs_site_url (config .html_baseurl )
105
+ else :
106
+ ogp_site_url = config .ogp_site_url
107
+
108
+ # If ogp_canonical_url is not set, default to the value of ogp_site_url
109
+ ogp_canonical_url = config .ogp_canonical_url or ogp_site_url
104
110
105
111
# url tag
106
112
# Get the URL of the specific page
107
- page_url = urljoin (config . ogp_site_url , builder .get_target_uri (context ["pagename" ]))
113
+ page_url = urljoin (ogp_canonical_url , builder .get_target_uri (context ["pagename" ]))
108
114
tags ["og:url" ] = page_url
109
115
110
116
# site name tag, False disables, default to project if ogp_site_name not
@@ -156,6 +162,8 @@ def get_tags(
156
162
title = title ,
157
163
description = description ,
158
164
pagename = context ["pagename" ],
165
+ ogp_site_url = ogp_site_url ,
166
+ ogp_canonical_url = ogp_canonical_url ,
159
167
srcdir = srcdir ,
160
168
outdir = outdir ,
161
169
config = config ,
@@ -202,7 +210,7 @@ def get_tags(
202
210
else : # ogp_image is set
203
211
# ogp_image is defined as being relative to the site root.
204
212
# This workaround is to keep that functionality from breaking.
205
- root = config . ogp_site_url
213
+ root = ogp_site_url
206
214
207
215
image_url = urljoin (root , image_url_parsed .path )
208
216
tags ["og:image" ] = image_url
@@ -251,6 +259,8 @@ def social_card_for_page(
251
259
title : str ,
252
260
description : str ,
253
261
pagename : str ,
262
+ ogp_site_url : str ,
263
+ ogp_canonical_url : str ,
254
264
* ,
255
265
srcdir : str | Path ,
256
266
outdir : str | Path ,
@@ -272,7 +282,7 @@ def social_card_for_page(
272
282
# Site URL
273
283
site_url = config_social .get ("site_url" , True )
274
284
if site_url is True :
275
- url_text = config . ogp_site_url .split ("://" )[- 1 ]
285
+ url_text = ogp_canonical_url .split ("://" )[- 1 ]
276
286
elif isinstance (site_url , str ):
277
287
url_text = site_url
278
288
@@ -286,15 +296,12 @@ def social_card_for_page(
286
296
pagename ,
287
297
srcdir = srcdir ,
288
298
outdir = outdir ,
289
- config = config ,
290
299
env = env ,
300
+ html_logo = config .html_logo ,
291
301
)
292
302
293
303
# Link the image in our page metadata
294
- # We use os.path.sep to standardize behavior acros *nix and Windows
295
- url = config .ogp_site_url .strip ("/" )
296
- image_path = str (image_path ).replace (os .path .sep , "/" ).strip ("/" )
297
- return f"{ url } /{ image_path } "
304
+ return posixpath .join (ogp_site_url , image_path .as_posix ())
298
305
299
306
300
307
def html_page_context (
@@ -320,6 +327,7 @@ def setup(app: Sphinx) -> ExtensionMetadata:
320
327
# ogp_site_url="" allows relative by default, even though it's not
321
328
# officially supported by OGP.
322
329
app .add_config_value ("ogp_site_url" , "" , "html" )
330
+ app .add_config_value ("ogp_canonical_url" , "" , "html" )
323
331
app .add_config_value ("ogp_description_length" , DEFAULT_DESCRIPTION_LENGTH , "html" )
324
332
app .add_config_value ("ogp_image" , None , "html" )
325
333
app .add_config_value ("ogp_image_alt" , None , "html" )
0 commit comments