From ecfe8157bbde08cb8a07f4d9639e19af0fae95c8 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Wed, 11 Nov 2026 10:27:53 +0100 Subject: [PATCH] fix: keep .mdx pages through the sweep A body is written as .md and the decoration pass, after the sweep, gives a page with components its .mdx name. The sweep did not count the .mdx of the run before as produced: 402 pages were deleted and written again on every run, so write_if_changed never kept their bytes. --- scripts/sitewrite.py | 20 +++++++++++++++++--- tests/test_site_write.py | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/scripts/sitewrite.py b/scripts/sitewrite.py index c90bf78de..6a56560b6 100644 --- a/scripts/sitewrite.py +++ b/scripts/sitewrite.py @@ -54,6 +54,21 @@ def write_decorated(path: str | Path, content: str) -> bool: _record(path) return _write_artifact(str(path), content) +# A page body is written as .md and the decoration pass, which runs after +# the sweep, gives it its final extension: .mdx when it holds components, +# the other file removed. The .mdx a previous run left is that same page. +_PAGE_SUFFIXES = (".md", ".mdx") + + +def _was_produced(path: Path) -> bool: + """Whether this run produced *path*, a page under either extension.""" + if os.path.realpath(str(path)) in _produced: + return True + return path.suffix in _PAGE_SUFFIXES and any( + os.path.realpath(str(path.with_suffix(suffix))) in _produced + for suffix in _PAGE_SUFFIXES if suffix != path.suffix) + + def _sweep_generated(docs: Path, directories: list[str]) -> int: """Delete files in the generated tree that this run did not produce.""" removed = 0 @@ -62,7 +77,7 @@ def _sweep_generated(docs: Path, directories: list[str]) -> int: if not root.is_dir(): continue for path in sorted(root.rglob("*")): - if path.is_file() and os.path.realpath(str(path)) not in _produced: + if path.is_file() and not _was_produced(path): path.unlink() removed += 1 for path in sorted(root.rglob("*"), reverse=True): @@ -81,8 +96,7 @@ def _sweep_top_pages(docs: Path) -> int: """ removed = 0 for path in sorted(docs.glob("*.md*")): - if (path.is_file() and path.suffix in (".md", ".mdx") - and os.path.realpath(str(path)) not in _produced): + if path.is_file() and path.suffix in _PAGE_SUFFIXES and not _was_produced(path): path.unlink() removed += 1 return removed diff --git a/tests/test_site_write.py b/tests/test_site_write.py index e10709c1f..fc8cc2e5e 100644 --- a/tests/test_site_write.py +++ b/tests/test_site_write.py @@ -120,6 +120,21 @@ class Sweep(unittest.TestCase): self.assertTrue((self.docs / "emulators/kept.md").exists()) self.assertTrue((self.docs / "emulators/nested/deep.md").exists()) + def test_a_page_the_decoration_pass_made_mdx_is_kept(self): + """A body is written as .md, and the decoration pass, after the + sweep, renames a page with components to .mdx. The .mdx of the run + before was swept and written again on every run: 402 pages.""" + (self.docs / "platforms/kept.mdx").write_text("x") + (self.docs / "platforms/gone.mdx").write_text("x") + self._produce("platforms/kept.md") + sitewrite._sweep_generated(self.docs, ["platforms"]) + (self.docs / "top.mdx").write_text("x") + sitewrite._record(self.docs / "top.md") + self.assertEqual(sitewrite._sweep_top_pages(self.docs), 0) + self.assertTrue((self.docs / "platforms/kept.mdx").exists()) + self.assertTrue((self.docs / "top.mdx").exists()) + self.assertFalse((self.docs / "platforms/gone.mdx").exists()) + def test_a_directory_outside_the_list_is_never_touched(self): self._produce("emulators/kept.md", "emulators/nested/deep.md", "emulators/stale.md")