@@ -60,32 +60,35 @@ function calculateChecksum(filePath) {
6060 return crypto . createHash ( 'md5' ) . update ( content ) . digest ( 'hex' ) ;
6161}
6262/**
63- * Load checksums from index.json file (inline in examples)
63+ * Load checksums and heights from index.json file (inline in examples)
6464 */
65- function loadChecksums ( ) {
65+ function loadIndex ( ) {
66+ const checksums = { } ;
67+ const heights = { } ;
6668 if ( fs . existsSync ( INDEX_FILE ) ) {
6769 try {
6870 const content = fs . readFileSync ( INDEX_FILE , 'utf-8' ) ;
6971 const indexData = JSON . parse ( content ) ;
70- const checksums = { } ;
71- // Extract checksums from inline example entries
72+ // Extract checksums and heights from inline example entries
7273 if ( indexData . components ) {
7374 for ( const componentGroup of indexData . components ) {
7475 for ( const example of componentGroup . examples || [ ] ) {
76+ const key = `${ componentGroup . component } /${ example . name } ` ;
7577 if ( example . checksum ) {
76- const key = `${ componentGroup . component } /${ example . name } ` ;
7778 checksums [ key ] = example . checksum ;
7879 }
80+ if ( typeof example . height === 'number' ) {
81+ heights [ key ] = example . height ;
82+ }
7983 }
8084 }
8185 }
82- return checksums ;
8386 } catch ( error ) {
84- console . warn ( 'Failed to load checksums from index.json, starting fresh' ) ;
85- return { } ;
87+ console . warn ( 'Failed to load index.json, starting fresh' ) ;
88+ return { checksums : { } , heights : { } } ;
8689 }
8790 }
88- return { } ;
91+ return { checksums , heights } ;
8992}
9093/**
9194 * Check if example file has changed since last screenshot
@@ -149,24 +152,45 @@ function screenshotExists(componentName, exampleName, mode) {
149152 return fs . existsSync ( screenshotPath ) ;
150153 } ) ;
151154}
155+ /**
156+ * Open an example on the screenshot route and wait for it to be ready
157+ */
158+ async function openExample ( page , componentName , exampleName ) {
159+ const url = `${ BASE_URL } ${ SCREENSHOT_ROUTE_BASE } /${ componentName } /${ exampleName } ` ;
160+ await page . goto ( url , { waitUntil : 'networkidle' } ) ;
161+ // wait for the page to be ready
162+ await page . waitForLoadState ( 'domcontentloaded' ) ;
163+ // wait if the lazy-loaded element found
164+ if ( ( await page . locator ( '.screenshot-delay' ) . count ( ) ) > 0 ) {
165+ await page . waitForTimeout ( 2000 ) ;
166+ }
167+ }
168+
169+ /**
170+ * The example's rendered height in pixels (its `.example` element, at the screenshot viewport's
171+ * width), so a docs page can hold its space before it renders. Read before controls are hidden,
172+ * as they take up space on the page. `undefined` if there's no `.example`.
173+ */
174+ async function measureHeight ( page ) {
175+ const example = page . locator ( '.example' ) . first ( ) ;
176+ if ( ( await example . count ( ) ) === 0 ) return undefined ;
177+ const height = await example . evaluate ( ( el ) => el . getBoundingClientRect ( ) . height ) ;
178+ return height > 0 ? Math . round ( height ) : undefined ;
179+ }
180+
152181/**
153182 * Generate screenshots for an example in both light and dark modes
154183 * Outputs WebP format in multiple sizes for responsive loading
184+ *
185+ * @returns the example's height (see `measureHeight`)
155186 */
156187async function captureScreenshots ( page , componentName , exampleName , modes ) {
157- const url = `${ BASE_URL } ${ SCREENSHOT_ROUTE_BASE } /${ componentName } /${ exampleName } ` ;
158188 const screenshotDir = path . join ( SCREENSHOTS_DIR , componentName ) ;
159189 // Ensure directory exists
160190 ensureDir ( screenshotDir ) ;
161191 try {
162- // Navigate to the example
163- await page . goto ( url , { waitUntil : 'networkidle' } ) ;
164- // wait for the page to be ready
165- await page . waitForLoadState ( 'domcontentloaded' ) ;
166- // wait if the lazy-loaded element found
167- if ( ( await page . locator ( '.screenshot-delay' ) . count ( ) ) > 0 ) {
168- await page . waitForTimeout ( 2000 ) ;
169- }
192+ await openExample ( page , componentName , exampleName ) ;
193+ const height = await measureHeight ( page ) ;
170194 // Hide example controls before taking screenshots
171195 await page . evaluate ( ( ) => {
172196 const controls = document . querySelectorAll ( '.screenshot-hidden' ) ;
@@ -208,6 +232,7 @@ async function captureScreenshots(page, componentName, exampleName, modes) {
208232 }
209233 console . log ( ` ✓ ${ mode } : ${ sizes . join ( ', ' ) } ` ) ;
210234 }
235+ return height ;
211236 } catch ( error ) {
212237 console . error ( ` ✗ failed:` , error instanceof Error ? error . message : error ) ;
213238 throw error ;
@@ -315,9 +340,10 @@ export async function generateScreenshots(options) {
315340 console . log ( `Found ${ examples . length } examples` ) ;
316341 // Clean up orphaned screenshots before processing
317342 cleanupOrphanedScreenshots ( examples ) ;
318- // Load existing checksums
319- const checksums = loadChecksums ( ) ;
343+ // Load existing checksums and heights
344+ const { checksums, heights } = loadIndex ( ) ;
320345 const updatedChecksums = { } ;
346+ const updatedHeights = { } ;
321347 // Limit for testing
322348 if ( TEST_LIMIT !== undefined ) {
323349 examples = examples . slice ( 0 , TEST_LIMIT ) ;
@@ -356,10 +382,25 @@ export async function generateScreenshots(options) {
356382 const darkExists = screenshotExists ( component , exampleName , 'dark' ) ;
357383 // Skip if both screenshots exist and file hasn't changed (unless --all flag is used)
358384 if ( lightExists && darkExists && ! fileChanged && ! FORCE_ALL ) {
359- console . log ( ' ⊘ Skipped (unchanged)' ) ;
360- skippedCount += 2 ;
385+ const key = `${ component } /${ exampleName } ` ;
361386 // Keep existing checksum
362- updatedChecksums [ `${ component } /${ exampleName } ` ] = checksums [ `${ component } /${ exampleName } ` ] ;
387+ updatedChecksums [ key ] = checksums [ key ] ;
388+ if ( heights [ key ] !== undefined ) {
389+ console . log ( ' ⊘ Skipped (unchanged)' ) ;
390+ updatedHeights [ key ] = heights [ key ] ;
391+ } else {
392+ // Screenshots from before heights were recorded: measure it, leaving the images be
393+ try {
394+ await openExample ( page , component , exampleName ) ;
395+ updatedHeights [ key ] = await measureHeight ( page ) ;
396+ console . log ( ` ⊘ Skipped (unchanged), measured height: ${ updatedHeights [ key ] } px` ) ;
397+ } catch ( error ) {
398+ console . error ( ` ✗ Height:` , error instanceof Error ? error . message : error ) ;
399+ await page . close ( ) . catch ( ( ) => { } ) ;
400+ page = await newPage ( ) ;
401+ }
402+ }
403+ skippedCount += 2 ;
363404 continue ;
364405 }
365406 // Show reason for regeneration
@@ -385,7 +426,12 @@ export async function generateScreenshots(options) {
385426 }
386427 // Capture screenshots if any modes need to be processed
387428 if ( modesToCapture . length > 0 ) {
388- await captureScreenshots ( page , component , exampleName , modesToCapture ) ;
429+ updatedHeights [ `${ component } /${ exampleName } ` ] = await captureScreenshots (
430+ page ,
431+ component ,
432+ exampleName ,
433+ modesToCapture
434+ ) ;
389435 processedCount += modesToCapture . length ;
390436 }
391437 // Update checksum after successful capture
@@ -394,10 +440,13 @@ export async function generateScreenshots(options) {
394440 console . error ( ` ✗ Error:` , error instanceof Error ? error . message : error ) ;
395441 errorCount ++ ;
396442 failedExamples . push ( `${ component } /${ exampleName } ` ) ;
397- // Keep old checksum on error to retry next time
443+ // Keep old checksum (and height) on error to retry next time
398444 if ( checksums [ `${ component } /${ exampleName } ` ] ) {
399445 updatedChecksums [ `${ component } /${ exampleName } ` ] = checksums [ `${ component } /${ exampleName } ` ] ;
400446 }
447+ if ( heights [ `${ component } /${ exampleName } ` ] !== undefined ) {
448+ updatedHeights [ `${ component } /${ exampleName } ` ] = heights [ `${ component } /${ exampleName } ` ] ;
449+ }
401450 // Start the next example on a fresh page, as one left busy (or mid-navigation) would
402451 // fail the examples after it too
403452 await page . close ( ) . catch ( ( ) => { } ) ;
@@ -414,10 +463,12 @@ export async function generateScreenshots(options) {
414463 examplesByComponent . set ( example . component , [ ] ) ;
415464 }
416465 const checksumKey = `${ example . component } /${ example . example } ` ;
466+ const height = updatedHeights [ checksumKey ] ;
417467 examplesByComponent . get ( example . component ) . push ( {
418468 name : example . example ,
419469 path : `/example/${ example . component } /${ example . example } ` ,
420470 checksum : updatedChecksums [ checksumKey ] || '' ,
471+ ...( height !== undefined ? { height } : { } ) ,
421472 } ) ;
422473 }
423474 // Convert to array and sort
0 commit comments