Skip to content

Commit

Permalink
Try index.txt for viser docs version list, add version list banner cache
Browse files Browse the repository at this point in the history
  • Loading branch information
brentyi committed May 25, 2024
1 parent bb5f024 commit 7a6ab4a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 19 deletions.
36 changes: 30 additions & 6 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:
steps:
# Check out source.
- uses: actions/checkout@v2
with:
fetch-depth: 0 # This ensures the entire history is fetched so we can switch branches

- name: Set up Python
uses: actions/setup-python@v1
Expand All @@ -27,6 +29,13 @@ jobs:
uv pip install --system -e ".[dev]"
uv pip install --system -r docs/requirements.txt
# Get version from pyproject.toml.
- name: Get version + subdirectory
run: |
VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "DOCS_SUBDIR=versions/$VERSION" >> $GITHUB_ENV
# Hack to overwrite version.
- name: Set version to 'latest' for pushes (this will appear in the doc banner)
run: |
Expand All @@ -38,12 +47,6 @@ jobs:
run: |
sphinx-build docs/source docs/build -b dirhtml
# Get version from pyproject.toml.
- name: Get version + subdirectory
run: |
VERSION=$(python -c "import toml; print(toml.load('pyproject.toml')['project']['version'])")
echo "DOCS_SUBDIR=versions/$VERSION" >> $GITHUB_ENV
# Get version from pyproject.toml.
- name: Override subdirectory to `latest/` for pushes
run: |
Expand All @@ -60,3 +63,24 @@ jobs:
keep_files: false # This will only erase the destination subdirectory.
cname: viser.studio
if: github.event_name != 'pull_request'

# We'll maintain an index of all versions under viser.studio/versions.
# This will be useful for dynamically generating lists of possible doc links.
- name: Update versions index.txt
run: |
git checkout . # Revert change to pyproject.toml from earlier...
git checkout gh-pages
git pull
git config --global user.email "[email protected]"
git config --global user.name "Brent Yi"
FILE="versions/index.txt" # Replace with your file path
if ! grep -qx "$VERSION" "$FILE"; then
echo "$VERSION" >> "$FILE"
git add $FILE
git commit -m "Update versions.txt with new version $VERSION"
git push origin gh-pages
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
VERSION: ${{ env.VERSION }}
if: github.event_name == 'push' # TODO: should be release.
5 changes: 0 additions & 5 deletions docs/source/_static/css/custom.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@ img.sidebar-logo {
width: 5em;
margin: 1em 0 0 0;
}
.sidebar-brand-text {
font-size: 1rem;
font-weight: 600;
font-style: italic;
}
48 changes: 40 additions & 8 deletions docs/source/_templates/sidebar/brand.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,47 @@
<img class="sidebar-logo only-dark" src="{{ pathto('_static/' + theme_dark_logo, 1) }}" alt="logo" />
</div>
{%- endif %}
<span class="sidebar-brand-text">{{ version }}</span>
<!-- <span class="sidebar-brand-text">{{ project }}</span> -->

<!-- Dropdown for different versions of the viser docs. Slightly hacky -->
{% endblock brand_content %}
</div>

<!-- Dropdown for different versions of the viser docs. Slightly hacky. -->
<div style="padding: 0 1em;">
<script>
var viserDocsVersionsPopulated = false;

function viserMemoizeHelper(fn) {
const cacheKey = fn.name || 'memoizedFunction';
const ttl = 3 * 60 * 1000; // 3 minutes in milliseconds.

return async function(...args) {
const now = Date.now();
const cache = JSON.parse(localStorage.getItem(cacheKey)) || {};
if (cache.timestamp && (now - cache.timestamp < ttl)) {
console.log("Using cached value");
return cache.value;
}
const value = await fn(...args);
if (value === null) {
console.log("Using cached value (failed to recompute)");
return cache.value
}
localStorage.setItem(cacheKey, JSON.stringify({ value, timestamp: now }));
return value;
};
}
async function getVersionListViaGithubApi() {
console.log("Polling GitHub REST API");
const response = await fetch(
"https://api.github.com/repos/nerfstudio-project/viser/contents/versions?ref=gh-pages",
);
if (await response.status === 403) {
console.error(await response.json());
return null;
}
return await response.json();
}
async function viserDocsPopulateVersionDropDown () {
// Load the version list lazily... reduces likelihood of running into GitHub API rate limits.
if (viserDocsVersionsPopulated) {
Expand All @@ -23,10 +59,7 @@

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

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

0 comments on commit 7a6ab4a

Please sign in to comment.