Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ build:
jobs:
post_checkout:
- git fetch --unshallow || true # get accurate "Last updated on" info
- git fetch origin '+refs/heads/*:refs/remotes/origin/*' '+refs/tags/*:refs/tags/*' || true # date all versions for the picker

# Build documentation in the "docs/" directory with Sphinx
sphinx:
Expand Down
5 changes: 4 additions & 1 deletion _static/js/versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ document.addEventListener("readthedocs-addons-data-ready", function (event) {
const versionSwitch = document.querySelector(
"div.version-switch",
);
let versions = config.versions.active;
// newest first; tags are keyed by name, branches by branch name.
// Unknown refs (created after this build) are assumed newest.
const date = (v) => VERSION_DATES[v.type === "tag" ? v.slug : v.identifier] ?? Number.MAX_SAFE_INTEGER;
const versions = config.versions.active.sort((a, b) => date(b) - date(a));
if (config.versions.current.hidden || config.versions.current.type === "external") {
versions.unshift(config.versions.current);
}
Expand Down
1 change: 1 addition & 0 deletions _templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</script>
{%- if READTHEDOCS %}
<meta name="readthedocs-addons-api-version" content="1">
<script>const VERSION_DATES = {{ version_dates|tojson }};</script>
<script src="{{ pathto('_static/js/versions.js', 1) }}"></script>
{%- endif %}
{% endblock %}
17 changes: 17 additions & 0 deletions conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# serve to show the default.

import os
import subprocess
import sys

from pygments.formatters.html import HtmlFormatter
Expand Down Expand Up @@ -287,6 +288,22 @@ def setup(sphinx):

if os.environ.get("READTHEDOCS", "") == "True":
html_context["READTHEDOCS"] = True
# Ref dates, used by _static/js/versions.js to sort the version picker by date
refs = subprocess.run(
[
"git",
"for-each-ref",
"--format=%(refname) %(creatordate:unix)",
"refs/remotes/origin",
"refs/tags",
],
capture_output=True,
text=True,
).stdout
html_context["version_dates"] = {
ref.removeprefix("refs/tags/").removeprefix("refs/remotes/origin/"): int(date)
for ref, date in map(str.split, refs.splitlines())
}

# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
Expand Down
Loading