Skip to content

Commit e1e8c6d

Browse files
fix: support RTD addons (#60)
See https://about.readthedocs.com/blog/2024/07/addons-by-default/ Changes: 1. Set configurations suggested by https://about.readthedocs.com/blog/2024/07/addons-by-default/; 2. Keep `</head>` when minifying HTML files as RTD replaces it; 3. Set z-index of our banner to 1749, as the new RTD banner uses 1750. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b75d8a3 commit e1e8c6d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

deepmodeling_sphinx/banner.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ under MIT license
2424
padding-right: 0;
2525
padding-left: 0;
2626
/* pydata theme has z-index 1030; we should be the largest */
27-
z-index: 2000;
27+
/* RTD banner use 1750; we should be smaller than RTD */
28+
z-index: 1749;
2829
}
2930

3031
.header-holder .container {

deepmodeling_sphinx/inject.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,13 @@ def minify_html_files(app, pagename, templatename, context, doctree):
121121
def render(self, template, render_context):
122122
content = old_render(template, render_context)
123123
try:
124-
return minify_html.minify(content, minify_js=True, minify_css=True)
124+
return minify_html.minify(
125+
content,
126+
minify_js=True,
127+
minify_css=True,
128+
keep_html_and_head_opening_tags=True,
129+
keep_closing_tags=True,
130+
)
125131
except SyntaxError:
126132
return content
127133

@@ -168,6 +174,20 @@ def enable_dark_mode(app, config):
168174
app.add_css_file("dark_rtd.css")
169175

170176

177+
def rtd_config(app, config):
178+
"""Set RTD configurations.
179+
180+
See https://about.readthedocs.com/blog/2024/07/addons-by-default/
181+
"""
182+
config.html_baseurl = os.environ.get("READTHEDOCS_CANONICAL_URL", "")
183+
184+
# Tell Jinja2 templates the build is running on Read the Docs
185+
if os.environ.get("READTHEDOCS", "") == "True":
186+
if "html_context" not in config:
187+
config.html_context = {}
188+
config.html_context["READTHEDOCS"] = True
189+
190+
171191
def setup(app: Sphinx) -> Dict[str, Any]:
172192
# enable deepmodeling sidebar and icp
173193
# if the repo is outside the deepmodeling, disable it
@@ -181,5 +201,6 @@ def setup(app: Sphinx) -> Dict[str, Any]:
181201
app.connect("build-finished", minify_css_files)
182202
# dark mode for rtd theme
183203
app.connect("config-inited", enable_dark_mode)
204+
app.connect("config-inited", rtd_config)
184205

185206
return {"parallel_read_safe": True}

0 commit comments

Comments
 (0)