Skip to content

Commit 8774e68

Browse files
authored
EmbedAPI: support "pretty URLs" (#11811)
Tru for the URL ending with `index.html` if the original URL doesn't exist in the storage. This makes Sphinx HTMLDir build, Docusaurus and other documentation tools with pretty URLs to work properly.
1 parent c072cdd commit 8774e68

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

readthedocs/embed/v3/views.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -106,19 +106,21 @@ def _get_page_content_from_storage(self, project, version, filename):
106106
# Decode encoded URLs (e.g. convert %20 into a whitespace)
107107
filename = urllib.parse.unquote(filename)
108108

109+
# If the filename starts with `/`, the join will fail,
110+
# so we strip it before joining it.
109111
relative_filename = filename.lstrip("/")
110112
file_path = build_media_storage.join(
111113
storage_path,
112114
relative_filename,
113115
)
114116

115-
try:
116-
with build_media_storage.open(
117-
file_path
118-
) as fd: # pylint: disable=invalid-name
119-
return fd.read()
120-
except Exception: # noqa
121-
log.warning("Unable to read file.", file_path=file_path)
117+
tryfiles = [file_path, build_media_storage.join(file_path, "index.html")]
118+
for tryfile in tryfiles:
119+
try:
120+
with build_media_storage.open(tryfile) as fd:
121+
return fd.read()
122+
except Exception: # noqa
123+
log.warning("Unable to read file.", file_path=file_path)
122124

123125
return None
124126

0 commit comments

Comments
 (0)