Skip to content

Commit 629495c

Browse files
authored
tweak warning rendering (#526)
1 parent b77cc97 commit 629495c

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

docs/.hooks/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
from __future__ import annotations as _annotations
22

33
import re
4+
import time
45
import urllib.parse
56
from pathlib import Path
67

8+
from jinja2 import Environment
79
from mkdocs.config import Config
810
from mkdocs.structure.files import Files
911
from mkdocs.structure.pages import Page
@@ -17,6 +19,11 @@ def on_page_markdown(markdown: str, page: Page, config: Config, files: Files) ->
1719
return markdown
1820

1921

22+
def on_env(env: Environment, config: Config, files: Files) -> Environment:
23+
env.globals['build_timestamp'] = str(int(time.time()))
24+
return env
25+
26+
2027
def replace_uv_python_run(markdown: str) -> str:
2128
return re.sub(r'```bash\n(.*?)(python/uv[\- ]run|pip/uv[\- ]add|py-cli)(.+?)\n```', sub_run, markdown)
2229

docs/.overrides/main.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{% extends "base.html" %}
22
{% block content %}
3-
<div id="version-warning" style="min-height: 120px"></div>
3+
<div id="version-warning"></div>
44
<script>
5-
fetch('/version-warning.html').then(r => {
5+
fetch('/version-warning.html?v={{ build_timestamp }}').then(r => {
66
if (r.ok) {
77
r.text().then(text => { document.getElementById('version-warning').innerHTML = text })
88
} else {

docs/_worker.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ export default {
77
if (url.pathname === '/version-warning.html') {
88
try {
99
const html = await versionWarning(request, env)
10-
return new Response(html, { headers: {'Content-Type': 'text/html', 'Cache-Control': 'max-age=1800'} })
10+
const headers = {
11+
'Content-Type': 'text/plain',
12+
'Cache-Control': 'max-age=2592000', // 30 days
13+
}
14+
return new Response(html, { headers })
1115
} catch (e) {
1216
console.error(e)
1317
return new Response(
@@ -24,10 +28,10 @@ export default {
2428
// env looks like
2529
// {"CF_PAGES":"1","CF_PAGES_BRANCH":"ahead-warning","CF_PAGES_COMMIT_SHA":"...","CF_PAGES_URL":"https://..."}
2630
async function versionWarning(request, env) {
27-
const headers = new Headers({
31+
const headers = {
2832
'User-Agent': request.headers.get('User-Agent') || 'pydantic-ai-docs',
2933
'Accept': 'application/vnd.github.v3+json',
30-
})
34+
}
3135
const r1 = await fetch('https://api.github.com/repos/pydantic/pydantic-ai/releases/latest', {headers})
3236
if (!r1.ok) {
3337
const text = await r1.text()
@@ -55,7 +59,7 @@ async function versionWarning(request, env) {
5559
<p class="admonition-title">Version Notice</p>
5660
<p>
5761
${env.CF_PAGES_BRANCH === 'main' ? '' : `(<b>${env.CF_PAGES_BRANCH}</b> preview)`}
58-
This documentation is ahead of the latest release by <b>${ahead_by}</b> commit${ahead_by === 1 ? '' : 's'}.
62+
This documentation is ahead of the last release by <b>${ahead_by}</b> commit${ahead_by === 1 ? '' : 's'}.
5963
You may see documentation for features not yet supported in the latest release <a href="${html_url}">${name}</a>.
6064
</p>
6165
</div>`

docs/extra/tweaks.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@ img.index-header {
5050
font-weight: 300;
5151
font-style: italic;
5252
}
53+
54+
#version-warning {
55+
min-height: 120px;
56+
margin-bottom: 10px;
57+
}

0 commit comments

Comments
 (0)