@@ -7,6 +7,10 @@ import fs from "node:fs";
77import path from "node:path" ;
88import { fileURLToPath } from "node:url" ;
99import { commercialLabel , esc , PERSPECTIVE_LABELS , verifiedAge } from "./lib/shared.mjs" ;
10+ import { entryPageHtml } from "./lib/entry-page.mjs" ;
11+ import { LinkError , makeLinkResolver } from "./lib/links.mjs" ;
12+ import { deprecationReason , MarkdownError , renderBlocks , renderInline , splitEntryBody } from "./lib/markdown.mjs" ;
13+ import { checkPage } from "./lib/page-checks.mjs" ;
1014
1115const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
1216const ROOT = path . resolve ( __dirname , ".." ) ;
@@ -104,6 +108,7 @@ function loadEntries(vocab) {
104108 const files = walkMarkdown ( CATALOG ) ;
105109 const entries = [ ] ;
106110 const errors = [ ] ;
111+ const bodies = new Map ( ) ;
107112
108113 for ( const file of files ) {
109114 const text = fs . readFileSync ( file , "utf8" ) ;
@@ -120,6 +125,7 @@ function loadEntries(vocab) {
120125 errors . push ( `${ rel } missing: ${ missing . join ( ", " ) } ` ) ;
121126 continue ;
122127 }
128+ bodies . set ( String ( meta . id ) , body ) ;
123129
124130 entries . push ( {
125131 id : String ( meta . id ) ,
@@ -147,6 +153,7 @@ function loadEntries(vocab) {
147153 verified : meta . verified ? String ( meta . verified ) : null ,
148154 status : String ( meta . status ) ,
149155 path : rel ,
156+ page : `entry/${ String ( meta . id ) } /` ,
150157 summary : summaryFromBody ( body ) ,
151158 ...( meta . grid_dimensions ? { grid_dimensions : String ( meta . grid_dimensions ) } : { } ) ,
152159 ...( meta . camera_perspective ? { camera_perspective : String ( meta . camera_perspective ) } : { } ) ,
@@ -163,7 +170,7 @@ function loadEntries(vocab) {
163170 }
164171
165172 entries . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ;
166- return { entries, errors } ;
173+ return { entries, errors, bodies } ;
167174}
168175
169176function edgeVar ( entry ) {
@@ -326,7 +333,8 @@ function sitemapXml(site, entries) {
326333 const urls = [
327334 ` <url><loc>${ esc ( base ) } /</loc></url>` ,
328335 ...entries . map (
329- ( e ) => ` <url><loc>${ esc ( base ) } /#entry-${ esc ( e . id ) } </loc></url>`
336+ ( e ) =>
337+ ` <url><loc>${ esc ( base ) } /entry/${ esc ( e . id ) } /</loc>${ e . verified ? `<lastmod>${ esc ( e . verified ) } </lastmod>` : "" } </url>`
330338 ) ,
331339 ] ;
332340 return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${ urls . join ( "\n" ) } \n</urlset>\n` ;
@@ -353,6 +361,7 @@ function notFoundHtml(site) {
353361 <main class="section">
354362 <h1>Not found</h1>
355363 <p>That page is not part of this catalog.</p>
364+ <p>Looking for an entry? <a href="${ esc ( base ) } /#catalog">Search the catalog</a>.</p>
356365 <p class="hero-actions">
357366 <a class="btn" href="${ esc ( base ) } /">Back to the catalog</a>
358367 </p>
@@ -372,10 +381,74 @@ function copyDir(src, dest) {
372381 }
373382}
374383
384+ /** Writes dist/entry/<id>/index.html for every entry. */
385+ function writeEntryPages ( { entries, bodies, config, stats, stamp, hasCard, now } ) {
386+ const errors = [ ] ;
387+ const written = [ ] ;
388+ const idByPath = new Map ( entries . map ( ( e ) => [ e . path , e . id ] ) ) ;
389+ const kindOf = ( repoPath ) => {
390+ const full = path . join ( ROOT , repoPath ) ;
391+ if ( ! fs . existsSync ( full ) ) return null ;
392+ return fs . statSync ( full ) . isDirectory ( ) ? "dir" : "file" ;
393+ } ;
394+ // Prev/next follow the homepage's default order: category, then name.
395+ const visible = entries . filter ( ( e ) => e . status !== "deprecated" ) ;
396+ const neighbours = new Map ( ) ;
397+ for ( const cat of Object . keys ( config . categories ) ) {
398+ const group = visible . filter ( ( e ) => e . category === cat ) ;
399+ group . forEach ( ( e , i ) => neighbours . set ( e . id , { prev : group [ i - 1 ] || null , next : group [ i + 1 ] || null } ) ) ;
400+ }
401+ for ( const entry of entries ) {
402+ const body = bodies . get ( entry . id ) || "" ;
403+ const file = `${ entry . path } (body)` ;
404+ try {
405+ const resolveLink = makeLinkResolver ( { entryPath : entry . path , idByPath, repo : config . site . repo , kindOf } ) ;
406+ const { lead, rest, restFirstLine } = splitEntryBody ( body ) ;
407+ const leadHtml = renderBlocks ( lead , { file, resolveLink } ) ;
408+ const restHtml = renderBlocks ( rest , { file, resolveLink, firstLine : restFirstLine } ) ;
409+ const reason = entry . status === "deprecated" ? deprecationReason ( body ) : null ;
410+ const { prev = null , next = null } = neighbours . get ( entry . id ) || { } ;
411+ const html = entryPageHtml ( {
412+ entry,
413+ leadHtml,
414+ restHtml,
415+ deprecatedReasonHtml : reason ? renderInline ( reason , resolveLink ) : null ,
416+ prev,
417+ next,
418+ site : config . site ,
419+ categoryLabel : config . categories [ entry . category ] ?. label || entry . category ,
420+ stamp,
421+ total : stats . total ,
422+ hasCard,
423+ now,
424+ } ) ;
425+ const dir = path . join ( DIST , "entry" , entry . id ) ;
426+ fs . mkdirSync ( dir , { recursive : true } ) ;
427+ fs . writeFileSync ( path . join ( dir , "index.html" ) , html ) ;
428+ written . push ( `entry/${ entry . id } /index.html` ) ;
429+ } catch ( err ) {
430+ if ( err instanceof MarkdownError || err instanceof LinkError ) errors . push ( err . message ) ;
431+ else throw err ;
432+ }
433+ }
434+ return { errors, written } ;
435+ }
436+
437+ /** Runs the page checks over dist-relative files. */
438+ function checkPages ( files ) {
439+ const kindOf = ( rel ) => {
440+ const full = path . join ( DIST , rel ) ;
441+ if ( ! fs . existsSync ( full ) ) return null ;
442+ if ( fs . statSync ( full ) . isDirectory ( ) ) return fs . existsSync ( path . join ( full , "index.html" ) ) ? "dir" : null ;
443+ return "file" ;
444+ } ;
445+ return files . flatMap ( ( f ) => checkPage ( fs . readFileSync ( path . join ( DIST , f ) , "utf8" ) , { file : f , kindOf } ) ) ;
446+ }
447+
375448function main ( ) {
376449 const config = JSON . parse ( fs . readFileSync ( CONFIG_PATH , "utf8" ) ) ;
377450 const vocab = JSON . parse ( fs . readFileSync ( VOCAB_PATH , "utf8" ) ) ;
378- const { entries, errors } = loadEntries ( vocab ) ;
451+ const { entries, errors, bodies } = loadEntries ( vocab ) ;
379452
380453 if ( errors . length ) {
381454 console . error ( "Build warnings:" ) ;
@@ -457,11 +530,19 @@ function main() {
457530 fs . writeFileSync ( path . join ( DIST , "robots.txt" ) , robotsTxt ( config . site ) ) ;
458531 fs . writeFileSync ( path . join ( DIST , "404.html" ) , notFoundHtml ( config . site ) ) ;
459532
533+ const pages = writeEntryPages ( { entries, bodies, config, stats, stamp, hasCard, now } ) ;
534+ const pageErrors = [ ...pages . errors , ...checkPages ( [ "index.html" , ...pages . written ] ) ] ;
535+ if ( pageErrors . length ) {
536+ console . error ( `Page build failed (${ pageErrors . length } ):` ) ;
537+ for ( const e of pageErrors . slice ( 0 , 50 ) ) console . error ( ` - ${ e } ` ) ;
538+ process . exit ( 1 ) ;
539+ }
540+
460541 console . log (
461542 `Built ${ entries . length } entries → site/dist (${ stats . active } active, ${ stats . commercialOk } commercial-ok, ${ stats . commercialVaries } per-file)`
462543 ) ;
463544 console . log (
464- `Prerendered ${ visible . length } entry rows into index.html; wrote sitemap.xml, robots.txt, 404.html`
545+ `Prerendered ${ visible . length } entry rows into index.html; wrote ${ pages . written . length } entry pages, sitemap.xml, robots.txt, 404.html`
465546 ) ;
466547 if ( errors . length ) process . exitCode = 0 ; // soft-fail missing fields as warnings
467548}
0 commit comments