@@ -88,6 +88,7 @@ export interface KitSystem {
8888 kitDarkUrl ?: string ;
8989 tokensUrl ?: string ;
9090 indexUrl ?: string ;
91+ kitLabel ?: string ;
9192}
9293
9394export interface KitAsset {
@@ -134,6 +135,72 @@ const ASSET_TILES: { kind: string; label: string; file: string }[] = [
134135 { kind : 'form' , label : 'Form page' , file : 'system/artifacts/form.html' } ,
135136] ;
136137
138+ function hasAvailablePackageFile (
139+ packageInfo : DesignSystemPackageInfo | undefined ,
140+ filePath : string | undefined ,
141+ ) : filePath is string {
142+ if ( ! filePath ) return false ;
143+ const availableFiles = packageInfo ?. availableFiles ;
144+ return Array . isArray ( availableFiles ) ? availableFiles . includes ( filePath ) : true ;
145+ }
146+
147+ function firstAvailablePackageFile (
148+ packageInfo : DesignSystemPackageInfo | undefined ,
149+ files : Array < string | undefined > ,
150+ ) : string | null {
151+ return files . find ( ( filePath ) => hasAvailablePackageFile ( packageInfo , filePath ) ) ?? null ;
152+ }
153+
154+ function previewPageLabel ( pathName : string , title : string | undefined , role : string | undefined ) : string {
155+ const explicit = title ?. trim ( ) || role ?. trim ( ) ;
156+ if ( explicit ) return explicit ;
157+ const base = pathName . split ( '/' ) . pop ( ) ?. replace ( / \. [ ^ . ] + $ / , '' ) ?? pathName ;
158+ return base
159+ . split ( / [ - _ ] + / )
160+ . filter ( Boolean )
161+ . map ( ( part ) => part . charAt ( 0 ) . toUpperCase ( ) + part . slice ( 1 ) )
162+ . join ( ' ' ) || 'Preview' ;
163+ }
164+
165+ type PackagePreviewPageInput = {
166+ path ?: string ;
167+ role ?: string ;
168+ title ?: string ;
169+ } ;
170+ type PackagePreviewPage = PackagePreviewPageInput & { path : string } ;
171+
172+ function isAvailablePreviewPage (
173+ packageInfo : DesignSystemPackageInfo ,
174+ page : PackagePreviewPageInput ,
175+ ) : page is PackagePreviewPage {
176+ return typeof page . path === 'string' && hasAvailablePackageFile ( packageInfo , page . path ) ;
177+ }
178+
179+ function packageAssetTiles (
180+ packageInfo : DesignSystemPackageInfo | undefined ,
181+ staticUrl : ( rel : string ) => string ,
182+ ) : KitAsset [ ] | undefined {
183+ if ( ! packageInfo ) return undefined ;
184+ const artifactTiles = ASSET_TILES
185+ . filter ( ( a ) => hasAvailablePackageFile ( packageInfo , a . file ) )
186+ . map ( ( a ) => ( {
187+ kind : a . kind ,
188+ label : a . label ,
189+ url : staticUrl ( a . file ) ,
190+ } ) ) ;
191+ if ( artifactTiles . length > 0 ) return artifactTiles ;
192+
193+ const previewPages = packageInfo . manifest ?. preview ?. pages ?? [ ] ;
194+ const previewTiles = previewPages
195+ . filter ( ( page ) : page is PackagePreviewPage => isAvailablePreviewPage ( packageInfo , page ) )
196+ . map ( ( page ) => ( {
197+ kind : page . role ?. trim ( ) || page . path ,
198+ label : previewPageLabel ( page . path , page . title , page . role ) ,
199+ url : staticUrl ( page . path ) ,
200+ } ) ) ;
201+ return previewTiles . length > 0 ? previewTiles : undefined ;
202+ }
203+
137204function fontList ( typography : DesignKit [ 'typography' ] ) : KitFont [ ] {
138205 return [ typography . display , typography . body , typography . mono ] . filter (
139206 ( f ) : f is KitFont => Boolean ( f ) ,
@@ -453,6 +520,25 @@ export function parsedToKit(parsed: ParsedDesignMd, opts: ParsedKitOptions): Des
453520 const staticUrl = ! opts . editable && opts . designSystemId && opts . packageInfo ?. manifest
454521 ? ( rel : string ) : string => designSystemStaticUrl ( opts . designSystemId ! , rel )
455522 : null ;
523+ const manifestFiles = opts . packageInfo ?. manifest ?. files ;
524+ const kitPath = staticUrl
525+ ? firstAvailablePackageFile ( opts . packageInfo , [
526+ 'system/kit.html' ,
527+ manifestFiles ?. components ?? 'components.html' ,
528+ ] )
529+ : null ;
530+ const kitDarkPath = staticUrl
531+ ? firstAvailablePackageFile ( opts . packageInfo , [ 'system/kit.dark.html' ] )
532+ : null ;
533+ const tokensPath = staticUrl
534+ ? firstAvailablePackageFile ( opts . packageInfo , [
535+ 'system/tokens.default.json' ,
536+ manifestFiles ?. designTokens ,
537+ ] )
538+ : null ;
539+ const indexPath = staticUrl
540+ ? firstAvailablePackageFile ( opts . packageInfo , [ 'system/index.html' ] )
541+ : null ;
456542
457543 return {
458544 designSystemId : opts . designSystemId ,
@@ -471,17 +557,16 @@ export function parsedToKit(parsed: ParsedDesignMd, opts: ParsedKitOptions): Des
471557 voice : hasVoice ( parsed . voice ) ? parsed . voice : undefined ,
472558 imagery : hasImagery ( imagery ) ? imagery : undefined ,
473559 layout : hasLayout ( layout ) ? layout : undefined ,
474- system : staticUrl
560+ system : staticUrl && kitPath
475561 ? {
476- kitUrl : staticUrl ( 'system/kit.html' ) ,
477- kitDarkUrl : staticUrl ( 'system/kit.dark.html' ) ,
478- tokensUrl : staticUrl ( 'system/tokens.default.json' ) ,
479- indexUrl : staticUrl ( 'system/index.html' ) ,
562+ kitUrl : staticUrl ( kitPath ) ,
563+ ...( kitDarkPath ? { kitDarkUrl : staticUrl ( kitDarkPath ) } : { } ) ,
564+ ...( tokensPath ? { tokensUrl : staticUrl ( tokensPath ) } : { } ) ,
565+ ...( indexPath ? { indexUrl : staticUrl ( indexPath ) } : { } ) ,
566+ kitLabel : kitPath ,
480567 }
481568 : undefined ,
482- assets : staticUrl
483- ? ASSET_TILES . map ( ( a ) => ( { kind : a . kind , label : a . label , url : staticUrl ( a . file ) } ) )
484- : undefined ,
569+ assets : staticUrl ? packageAssetTiles ( opts . packageInfo , staticUrl ) : undefined ,
485570 showcaseHtml : opts . showcaseHtml ?? null ,
486571 } ;
487572}
0 commit comments