mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 22:15:50 +00:00
fix(website): improve page loading, image labels, and legacy redirects
This commit is contained in:
1 parent
f27aa1ad90
commit
e1c37d68a4
53 files changed
+2520
-2170
No files matched your search
@@ -6,7 +6,7 @@ import sys
|
||||
import xml.etree.ElementTree as ET
|
||||
from zipfile import ZIP_DEFLATED, ZipFile, ZipInfo
|
||||
|
||||
from website import PageMetadata, page_url
|
||||
from website import SITE_ORIGIN, PageMetadata, page_url
|
||||
from website_agents import prepare_discovery, prepare_markdown
|
||||
|
||||
|
||||
@@ -77,8 +77,34 @@ def prepare_notes_example(site):
|
||||
print(f"Prepared examples/notes-example.zip ({len(EXAMPLE_FILES)} files).")
|
||||
|
||||
|
||||
def prepare_redirects(site):
|
||||
"""Publish permanent Cloudflare redirects and equivalent static Pages links."""
|
||||
routes = {
|
||||
"showcases": ("showcase", "REA showcases"),
|
||||
"environments": ("guides/rl-environments", "Build RL environments"),
|
||||
}
|
||||
rules = []
|
||||
for alias, (target, title) in routes.items():
|
||||
rules.extend(f"/{alias}{suffix} /{target}/ 301" for suffix in ("", "/"))
|
||||
directory = site / alias
|
||||
directory.mkdir()
|
||||
directory.joinpath("index.html").write_text(
|
||||
f'<!doctype html><html lang="en"><head><meta charset="utf-8">\n'
|
||||
f'<title>{title}</title><meta name="robots" content="noindex">\n'
|
||||
f'<link rel="canonical" href="{SITE_ORIGIN}/{target}/">\n'
|
||||
f'<meta http-equiv="refresh" content="0; url=../{target}/"></head>\n'
|
||||
f'<body><h1><a href="../{target}/">{title}</a></h1></body></html>\n',
|
||||
encoding="utf-8",
|
||||
)
|
||||
(site / "_redirects").write_text("\n".join(rules) + "\n", encoding="utf-8")
|
||||
|
||||
|
||||
def prepare_assets(site):
|
||||
"""Finish the shared publish artifact before it replaces the current preview."""
|
||||
from rcssmin import cssmin
|
||||
stylesheet = site / "assets/styles.css"
|
||||
stylesheet.write_text(cssmin(stylesheet.read_text(encoding="utf-8")), encoding="utf-8")
|
||||
prepare_redirects(site)
|
||||
prepare_sharing_image(site)
|
||||
prepare_sitemap(site)
|
||||
prepare_notes_example(site)
|
||||
|
||||
@@ -909,7 +909,7 @@ class WebsiteTranslationChecks(unittest.TestCase):
|
||||
arabic.feed((site / "ar/faq/index.html").read_text())
|
||||
self.assertEqual(arabic.lang, "ar")
|
||||
|
||||
def test_every_reading_page_loads_theme_before_styles(self):
|
||||
def test_reading_pages_set_the_initial_theme_and_defer_external_scripts(self):
|
||||
site = ROOT / "website/_site"
|
||||
english_pages = reading_pages(ROOT / "website/public")
|
||||
self.assertTrue(english_pages)
|
||||
@@ -924,12 +924,12 @@ class WebsiteTranslationChecks(unittest.TestCase):
|
||||
self.assertEqual(len(colors), 1)
|
||||
self.assertEqual(colors[0].attrs.get("content"), "#ffffff")
|
||||
self.assertNotIn("media", colors[0].attrs)
|
||||
theme = [node for node in doc.nodes if node.tag == "script" and (node.attrs.get("src") or "").endswith("/assets/theme.js")]
|
||||
self.assertEqual(len(theme), 1)
|
||||
for deferred in ("defer", "async", "type"):
|
||||
self.assertNotIn(deferred, theme[0].attrs)
|
||||
scripts = [node for node in doc.nodes if node.tag == "script"]
|
||||
for node in scripts:
|
||||
if node.attrs.get("src"):
|
||||
self.assertTrue("defer" in node.attrs or "async" in node.attrs or node.attrs.get("type") == "module")
|
||||
stylesheet = next(node for node in doc.nodes if node.tag == "link" and node.attrs.get("rel") == "stylesheet")
|
||||
self.assertLess(theme[0].start, stylesheet.start)
|
||||
self.assertTrue(any(node.start < stylesheet.start and "src" not in node.attrs and "type" not in node.attrs for node in scripts))
|
||||
toggle = [node for node in doc.nodes if "data-theme-toggle" in node.attrs]
|
||||
self.assertEqual(len(toggle), 1)
|
||||
self.assertEqual(toggle[0].tag, "button")
|
||||
|
||||
@@ -559,10 +559,13 @@ def render_page(root, source_path, language, messages, available, figures, page_
|
||||
payload = json.dumps(runtime, ensure_ascii=False).replace("<", "\\u003c").replace("&", "\\u0026")
|
||||
# Only Korean editions request the font stylesheet; English fallbacks keep
|
||||
# their system fonts. The variable face also supplies the site's 650 weight.
|
||||
font_stylesheet = (
|
||||
'<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100..900&display=swap" />\n '
|
||||
if code == "ko" else ""
|
||||
)
|
||||
font_stylesheet = ""
|
||||
if code == "ko":
|
||||
href = "https://fonts.googleapis.com/css2?family=Noto+Sans+KR:wght@100..900&display=swap"
|
||||
font_stylesheet = (
|
||||
f"""<link rel="preload" as="style" fetchpriority="low" href="{href}" onload="this.onload=null;this.rel='stylesheet'" />"""
|
||||
f'<noscript><link rel="stylesheet" href="{href}" /></noscript>\n '
|
||||
)
|
||||
source = source.replace("</head>", f'{font_stylesheet}<script id="website-messages" type="application/json">{payload}</script>\n </head>')
|
||||
return source
|
||||
|
||||
|
||||
Reference in new issue
Block a user