Repository navigation
publish.mjs: keep every --pages page when two share a theme - #121
juliasadler wants to merge 1 commit into
Conversation
rahul05ranjan
left a comment
There was a problem hiding this comment.
Clean fix for a real footgun — the old pages[...] = page silently ate the first page whenever two shared a theme, and the recording would just point at the later file.
I traced the fallback chain and it holds together: first same-theme page keeps the theme key, the colliding one drops to its filename slug, and a slug that's also taken gets -2, -3, … via freeSlug, so a third dark page or an unlucky slug collision can't silently overwrite either. Object.hasOwn over in is the right call too, since it ignores prototype keys.
One pre-existing edge this inherits rather than introduces: a page literally named dark.html with no theme slugs to the key dark, which consumers likely treat as the dark-theme entry. Before your change it collided head-on; now it survives as dark-2 if a themed page got there first, which is better but still slightly ambiguous. Probably not worth solving unless someone hits it — the numeric suffix at least makes the two pages distinct now.
The test covers the real case well, including the assertion that the overwritten page no longer ships as a stray source file — that's the user-visible half of the bug, easy to forget.
What this changes
detectPagesinscripts/publish.mjs: when two--pagesentries resolve to the same key (samedata-theme, or the same filename slug), the later entry now falls back to its filename slug — with a numeric suffix if that is taken too — instead of silently overwriting the earlier one. Both pages end up recorded under distinct keys.slugOfhelper, plus afreeSlughelper that finds an unclaimed key. No code comments, per repo rule.scripts/publish.test.mjswith a two-same-theme fixture, as suggested in the issue.[Unreleased]→Fixed.Why
Fixes #89. Before,
--pages main.html,alt.htmlwith both files atdata-theme="dark"producedfigure.pages = { "dark": "src/alt.html" }:main.htmlvanished as a page and shipped as a dead file underdest/src. After, it produces{ "dark": "src/main.html", "alt": "src/alt.html" }.I took the issue's first suggested direction (slug fallback) rather than throwing: it records both pages, as the issue's "Expected" section asks, and it doesn't turn a previously silent success into a hard error.
Checks
git status --shortshows only the changes I meant. —git status --shortshows onlyCHANGELOG.md,scripts/publish.mjs,scripts/publish.test.mjs. No example touched, so no figure rebuild applies;node --test scripts/publish.test.mjspasses 8/8 including the new regression test.node scripts/lines.mjs… — N/A.UnreleasedinCHANGELOG.md.Screenshots
N/A — no visual change.