// Builds the gallery pages. // // node gallery.cjs theme [note] writes /index.html // node gallery.cjs list writes /index.html // // The theme page is built only from shots.json and the file names, never from // HTML made while the theme ran. const fs = require( 'fs' ); const esc = ( s ) => String( s ).replace( /[&<>"']/g, ( c ) => `&#${ c.charCodeAt( 0 ) };` ); const page = ( title, css, body ) => `${ esc( title ) }${ body }\n`; const [ mode, dir, arg, note ] = process.argv.slice( 2 ); if ( mode === 'theme' ) { const { widths, shots, unshown = [] } = JSON.parse( fs.readFileSync( `${ dir }/shots.json`, 'utf8' ) ); const rows = shots .map( ( { title, path, template, files, notes } ) => { const cap = template ? `${ title }, ${ template }` : title; const links = widths .map( ( w ) => `${ esc( cap ) } at ${ w }px${ w }px` ) .join( '' ); const warn = notes.length ? `

${ notes.map( esc ).join( '
' ) }

` : ''; return `

${ esc( template || title ) } ${ template ? `${ esc( title ) }, ` : '' }${ esc( path ) }

${ warn }
${ links }
`; } ) .join( '' ); const missing = unshown.length ? `

Not in the screenshots: ${ unshown.map( esc ).join( ', ' ) }. Check them in the Site Editor.

` : ''; const lightbox = ` `; const css = `:root{color-scheme:dark;--bg:#111113;--mat:#1d1d21;--text:#f2f2f4;--muted:#8e8e95;--line:#26262b;--warn:#ffb057} *{box-sizing:border-box}body{margin:0;padding:56px 32px;background:var(--bg);color:var(--text);font:16px/1.55 ui-monospace,"SF Mono",Menlo,Consolas,monospace;-webkit-font-smoothing:antialiased} a{color:inherit;text-underline-offset:4px} header{max-width:720px;margin-bottom:24px}header p{margin:0}h1{font:inherit;margin:0}.back{color:var(--muted);text-decoration:none}.back:hover{color:var(--text)} hr{border:0;margin:0;height:1.55em;width:.6em;background:linear-gradient(currentColor,currentColor) left center/100% 1px no-repeat} .note{color:var(--muted)}.note a{color:var(--text)}.note+.note{margin-top:12px} section{padding-top:32px;margin-top:32px;border-top:1px solid var(--line)} h2{font:inherit;margin:0 0 16px}small{color:var(--muted);font-size:inherit;margin-left:8px}.warn{color:var(--warn);margin:-8px 0 16px} .row{display:flex;gap:24px;align-items:flex-start;overflow-x:auto;padding-bottom:4px}.row a{display:flex;flex-direction:column;gap:10px;color:var(--muted);text-decoration:none;flex:none} .mat{display:block;background:var(--mat);border-radius:4px;padding:16px}.row img{display:block;max-width:none}.row a:hover{color:var(--text)}.row a:focus-visible{outline:none}.row a:focus-visible .mat{outline:2px solid var(--text);outline-offset:2px} #lb{position:fixed;inset:0;background:rgba(10,10,12,.96);display:flex;align-items:center;justify-content:center;gap:16px}#lb[hidden]{display:none}#lb figure{margin:0;height:100vh;display:flex;flex-direction:column;align-items:center;max-width:calc(100vw - 140px)}#lb-scroll{overflow:auto;flex:1;margin-top:24px}#lb-img{display:block;max-width:100%}#lb-cap{color:var(--muted);padding:12px}#lb button{background:none;border:0;color:var(--text);font-family:inherit;font-size:48px;line-height:1;cursor:pointer}#lb-close{position:absolute;top:8px;right:16px} @media (max-width:700px){body{padding:32px 16px;font-size:15px}.mat{padding:10px}#lb figure{max-width:calc(100vw - 80px)}#lb button{font-size:32px}}`; // No token on a public page: the link opens the workflow, where someone with // access clicks Run workflow and enters the PR number. const pr = /^[0-9]+$/.test( process.env.PR_NUMBER || '' ) ? process.env.PR_NUMBER : ''; const theme = /^[A-Za-z0-9_-]+$/.test( process.env.REGEN_THEME || '' ) ? process.env.REGEN_THEME : ''; const workflow = 'Theme Screenshots workflow'; let regen = ''; if ( pr ) { regen = `

To take new screenshots, open the ${ workflow }, click Run workflow and enter PR number ${ pr }. They usually appear here in about 5 minutes. A push to PR #${ pr } also takes new ones.

`; } else if ( theme ) { regen = `

To take new screenshots, open the ${ workflow }, click Run workflow, leave the PR number empty and enter ${ theme } as the theme. They usually appear here in about 5 minutes.

`; } const head = `

← All themes


${ esc( arg ) }

${ note ? `

${ esc( note ) }

` : '' }${ regen || missing ? '
' : '' }${ regen }${ missing }
`; fs.writeFileSync( `${ dir }/index.html`, page( `${ arg } screenshots`, css, head + rows + lightbox ) ); } else if ( mode === 'list' ) { const themes = JSON.parse( fs.readFileSync( arg, 'utf8' ) ).sort( ( a, b ) => a.name.localeCompare( b.name ) ); const date = ( d ) => { const t = new Date( d ); return isNaN( t ) ? '' : t.toLocaleDateString( 'en-US', { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' } ); }; // Template and flag counts come from each gallery's shots.json, when it has one. const stats = ( slug ) => { try { const { shots } = JSON.parse( fs.readFileSync( `${ dir }/${ slug }/shots.json`, 'utf8' ) ); return { templates: new Set( shots.map( ( s ) => s.template || s.title ) ).size, flags: shots.filter( ( s ) => s.notes && s.notes.length ).length }; } catch { return null; } }; // A card per theme with its screenshot.png; themes published before covers // existed show their name instead. const items = themes .map( ( t ) => { const cover = /^[A-Za-z0-9_-]+\.webp$/.test( t.cover || '' ) ? `` : `${ esc( t.name ) }`; const s = stats( t.slug ); const meta = [ s && `${ s.templates } templates`, s && s.flags && `${ s.flags } to check` ].filter( Boolean ).map( esc ).join( ' · ' ); return `
  • ${ cover }${ esc( t.name ) } ${ meta ? `${ meta }` : '' }
  • `; } ) .join( '' ); const templates = themes.reduce( ( n, t ) => n + ( ( stats( t.slug ) || {} ).templates || 0 ), 0 ); const css = `:root{color-scheme:dark;--bg:#111113;--mat:#1d1d21;--text:#f2f2f4;--muted:#8e8e95} *{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--text);font:16px/1.55 ui-monospace,"SF Mono",Menlo,Consolas,monospace;-webkit-font-smoothing:antialiased} a{color:inherit;text-decoration:none} .grid{list-style:none;margin:0;padding:56px 32px;display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:56px 32px} .intro h1{font:inherit;margin:0}.intro p{margin:0}.intro .muted{color:var(--muted)}.intro hr{border:0;margin:0;height:1.55em;width:.6em;background:linear-gradient(currentColor,currentColor) left center/100% 1px no-repeat} .mat{display:block;background:var(--mat);border-radius:4px;padding:24px;margin-bottom:14px} .mat img,.blank{display:block;width:100%;aspect-ratio:4/3;object-fit:cover;object-position:top;transition:transform .3s ease} .blank{display:flex;align-items:center;justify-content:center;background:var(--bg);color:var(--muted)} .grid a:hover .mat img,.grid a:focus-visible .mat img{transform:scale(1.02)}.grid a:focus-visible{outline:none}.grid a:focus-visible .mat{outline:2px solid var(--text);outline-offset:2px} .cap time{color:var(--muted);margin-left:8px}.grid a:hover .name{text-decoration:underline;text-underline-offset:4px} small{display:block;color:var(--muted);font-size:inherit} @media (max-width:1000px){.grid{grid-template-columns:repeat(2,minmax(0,1fr))}} @media (max-width:700px){.grid{grid-template-columns:1fr;gap:40px;padding:32px 16px}.mat{padding:16px}body{font-size:15px}} @media (prefers-reduced-motion:reduce){.mat img{transition:none}}`; const count = `${ themes.length } themes${ templates ? ` · ${ templates } templates` : '' }`; const intro = `
  • Theme Screenshots


    Every template of a theme in review, captured straight from its pull request at desktop, tablet and mobile widths. A push to the PR usually updates the shots in about 5 minutes.


    ${ esc( count ) }

    Shows the 11 most recently updated themes.

  • `; const body = `
      ${ intro }${ items }
    `; fs.writeFileSync( `${ dir }/index.html`, page( 'Theme Screenshots', css, body ) ); } else { console.error( 'Usage: node gallery.cjs theme [note] | list ' ); process.exit( 1 ); }