Skip to content

Commit 6ea47b3

Browse files
committed
Try index.txt for viser docs version list, add version list banner cache
1 parent bb5f024 commit 6ea47b3

3 files changed

Lines changed: 64 additions & 13 deletions

File tree

.github/workflows/docs.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515
steps:
1616
# Check out source.
1717
- uses: actions/checkout@v2
18+
with:
19+
fetch-depth: 0 # This ensures the entire history is fetched so we can switch branches
1820

1921
- name: Set up Python
2022
uses: actions/setup-python@v1
@@ -42,6 +44,7 @@ jobs:
4244
- name: Get version + subdirectory
4345
run: |
4446
VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
47+
echo "VERSION=$VERSION" >> $GITHUB_ENV
4548
echo "DOCS_SUBDIR=versions/$VERSION" >> $GITHUB_ENV
4649
4750
# Get version from pyproject.toml.
@@ -60,3 +63,24 @@ jobs:
6063
keep_files: false # This will only erase the destination subdirectory.
6164
cname: viser.studio
6265
if: github.event_name != 'pull_request'
66+
67+
# We'll maintain an index of all versions under viser.studio/versions.
68+
# This will be useful for dynamically generating lists of possible doc links.
69+
- name: Update versions index.txt
70+
run: |
71+
git checkout . # Revert change to pyproject.toml from earlier...
72+
git checkout gh-pages
73+
git pull
74+
git config --global user.email "yibrenth@gmail.com"
75+
git config --global user.name "Brent Yi"
76+
FILE="versions/index.txt" # Replace with your file path
77+
if ! grep -qx "$VERSION" "$FILE"; then
78+
echo "$VERSION" >> "$FILE"
79+
git add $FILE
80+
git commit -m "Update versions.txt with new version $VERSION"
81+
git push origin gh-pages
82+
fi
83+
env:
84+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
85+
VERSION: ${{ env.VERSION }}
86+
if: github.event_name == 'push' # TODO: should be release.

docs/source/_static/css/custom.css

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,3 @@ img.sidebar-logo {
22
width: 5em;
33
margin: 1em 0 0 0;
44
}
5-
.sidebar-brand-text {
6-
font-size: 1rem;
7-
font-weight: 600;
8-
font-style: italic;
9-
}

docs/source/_templates/sidebar/brand.html

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,47 @@
99
<img class="sidebar-logo only-dark" src="{{ pathto('_static/' + theme_dark_logo, 1) }}" alt="logo" />
1010
</div>
1111
{%- endif %}
12-
<span class="sidebar-brand-text">{{ version }}</span>
12+
<!-- <span class="sidebar-brand-text">{{ project }}</span> -->
1313

14-
<!-- Dropdown for different versions of the viser docs. Slightly hacky -->
14+
{% endblock brand_content %}
15+
</div>
16+
17+
<!-- Dropdown for different versions of the viser docs. Slightly hacky. -->
18+
<div style="padding: 0 1em;">
1519
<script>
1620
var viserDocsVersionsPopulated = false;
21+
22+
function viserMemoizeHelper(fn) {
23+
const cacheKey = fn.name || 'memoizedFunction';
24+
const ttl = 3 * 60 * 1000; // 3 minutes in milliseconds.
25+
26+
return async function(...args) {
27+
const now = Date.now();
28+
const cache = JSON.parse(localStorage.getItem(cacheKey)) || {};
29+
if (cache.timestamp && (now - cache.timestamp < ttl)) {
30+
console.log("Using cached value");
31+
return cache.value;
32+
}
33+
const value = await fn(...args);
34+
if (value === null) {
35+
console.log("Using cached value (failed to recompute)");
36+
return cache.value
37+
}
38+
localStorage.setItem(cacheKey, JSON.stringify({ value, timestamp: now }));
39+
return value;
40+
};
41+
}
42+
async function getVersionListViaGithubApi() {
43+
console.log("Polling GitHub REST API");
44+
const response = await fetch(
45+
"https://api.github.com/repos/nerfstudio-project/viser/contents/versions?ref=gh-pages",
46+
);
47+
if (await response.status === 403) {
48+
console.error(await response.json());
49+
return null;
50+
}
51+
return await response.json();
52+
}
1753
async function viserDocsPopulateVersionDropDown () {
1854
// Load the version list lazily... reduces likelihood of running into GitHub API rate limits.
1955
if (viserDocsVersionsPopulated) {
@@ -23,10 +59,7 @@
2359

2460
// Populate the version list by polling the GitHub REST API.
2561
console.log("Populating docs version list!")
26-
const response = await fetch(
27-
"https://api.github.com/repos/nerfstudio-project/viser/contents/versions?ref=gh-pages",
28-
);
29-
const data = await response.json();
62+
const data = viserMemoizeHelper(getVersionListViaGithubApi)();
3063
let htmlString = "<ul style='margin: 0.5rem 0 0 0'>";
3164
htmlString += `<li><a href="https://viser.studio/latest">latest</a></li>`;
3265
for (let file of data) {
@@ -42,11 +75,10 @@
4275
style="padding: 0.5rem; background: var(--color-background-primary); border-radius: 0.5rem; border: 1px solid var(--color-sidebar-background-border);"
4376
ontoggle="viserDocsPopulateVersionDropDown()"
4477
>
45-
<summary style="cursor: pointer;">Versions</summary>
78+
<summary style="cursor: pointer;"><strong>Version:</strong> <em>{{ version }}</em></summary>
4679
<div id="version-dropdown"></div>
4780
</details>
4881
<!-- End dropdown -->
49-
{% endblock brand_content %}
5082
</div>
5183

5284
<div style="text-align: left; padding: 1em">

0 commit comments

Comments
 (0)