@@ -7,6 +7,8 @@ import ts from 'typescript'
77
88import { fixtures } from './component-preview-fixtures.js'
99import { writePreviewPartial } from './generate-component-previews.js'
10+ import { writePagePreviewPartial } from './generate-page-previews.js'
11+ import { pageFixtures } from './page-preview-fixtures.js'
1012
1113const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) )
1214
@@ -863,12 +865,14 @@ export function controllerSlug(controllerKey) {
863865 * @param {string } controllerKey
864866 * @param {Array<{name: string, type: string, optional: boolean}> } uniqueProps
865867 * @param {string } [examplePath]
868+ * @param {Array<object>|null } [exampleComponents]
866869 * @returns {Record<string, unknown> }
867870 */
868871export function generatePageExample (
869872 controllerKey ,
870873 uniqueProps ,
871- examplePath = '/page-path'
874+ examplePath = '/page-path' ,
875+ exampleComponents = null
872876) {
873877 const controllerValue =
874878 controllerKey === 'PageController' ? null : controllerKey
@@ -886,6 +890,8 @@ export function generatePageExample(
886890 setNestedValue ( example , prop . name , placeholderForType ( prop . type ) )
887891 }
888892
893+ if ( exampleComponents ) example . components = exampleComponents
894+
889895 return example
890896}
891897
@@ -894,12 +900,16 @@ export function generatePageExample(
894900 * @param {Array<{name: string, type: string, optional: boolean}> } uniqueProps
895901 * @param {string } examplePath
896902 * @param {number } sidebarPosition
903+ * @param {string|null } [previewSlug]
904+ * @param {Array<object>|null } [exampleComponents]
897905 */
898- function generatePageMd (
906+ export function generatePageMd (
899907 controllerKey ,
900908 uniqueProps ,
901909 examplePath ,
902- sidebarPosition
910+ sidebarPosition ,
911+ previewSlug = null ,
912+ exampleComponents = null
903913) {
904914 const description = metadata . pages [ controllerKey ]
905915 if ( ! description ) return null
@@ -908,11 +918,16 @@ function generatePageMd(
908918 const isDefault = controllerKey === 'PageController'
909919 const links = metadata . pageLinks ?. [ controllerKey ] ?? [ ]
910920
921+ const previewImport = previewSlug
922+ ? [ `` , `import Preview from './_previews/${ previewSlug } .mdx'` ]
923+ : [ ]
924+
911925 const lines = [
912926 `---` ,
913927 `sidebar_label: "${ label } "` ,
914928 `sidebar_position: ${ sidebarPosition } ` ,
915929 `---` ,
930+ ...previewImport ,
916931 `` ,
917932 `# ${ label } ` ,
918933 `` ,
@@ -933,12 +948,21 @@ function generatePageMd(
933948 lines . push ( `**Controller value:** \`"${ controllerKey } "\`` , `` )
934949 }
935950
951+ if ( previewSlug ) {
952+ lines . push ( `## Preview` , `` , `<Preview />` , `` )
953+ }
954+
936955 lines . push (
937956 `## JSON definition` ,
938957 `` ,
939958 '```json' ,
940959 JSON . stringify (
941- generatePageExample ( controllerKey , uniqueProps , examplePath ) ,
960+ generatePageExample (
961+ controllerKey ,
962+ uniqueProps ,
963+ examplePath ,
964+ exampleComponents
965+ ) ,
942966 null ,
943967 2
944968 ) ,
@@ -1032,7 +1056,7 @@ function generatePagesIndex() {
10321056 for ( const [ key , description ] of Object . entries ( metadata . pages ) ) {
10331057 const label = controllerLabel ( key )
10341058 const slug = controllerSlug ( key )
1035- lines . push ( `- [**${ label } **](./${ slug } .md ) — ${ description } ` )
1059+ lines . push ( `- [**${ label } **](./${ slug } .mdx ) — ${ description } ` )
10361060 }
10371061
10381062 lines . push ( `` )
@@ -1092,6 +1116,8 @@ function main() {
10921116 fs . rmSync ( pagesOutputDir , { recursive : true , force : true } )
10931117 }
10941118 fs . mkdirSync ( pagesOutputDir , { recursive : true } )
1119+ const pagePreviewsDir = path . resolve ( pagesOutputDir , '_previews' )
1120+ fs . mkdirSync ( pagePreviewsDir , { recursive : true } )
10951121
10961122 // Parse sources
10971123 const interfaces = parseComponentInterfaces ( componentsDtsPath )
@@ -1180,13 +1206,25 @@ function main() {
11801206 }
11811207 const { props : uniqueProps = [ ] , examplePath = '/page-path' } =
11821208 pageInterfaces [ key ] ?? { }
1183- const content = generatePageMd ( key , uniqueProps , examplePath , i + 1 )
1209+ const fixture = pageFixtures [ key ]
1210+ const sidebarPosition = i + 1
1211+ const content = generatePageMd (
1212+ key ,
1213+ uniqueProps ,
1214+ examplePath ,
1215+ sidebarPosition ,
1216+ fixture ? slug : null ,
1217+ fixture ?. exampleComponents ?? null
1218+ )
11841219 if ( content ) {
1185- fs . writeFileSync ( path . join ( pagesOutputDir , `${ slug } .md` ) , content )
1220+ fs . writeFileSync ( path . join ( pagesOutputDir , `${ slug } .mdx` ) , content )
1221+ }
1222+ if ( fixture ) {
1223+ writePagePreviewPartial ( pagePreviewsDir , slug , fixture )
11861224 }
11871225 }
11881226
1189- fs . writeFileSync ( path . join ( pagesOutputDir , 'index.md ' ) , generatePagesIndex ( ) )
1227+ fs . writeFileSync ( path . join ( pagesOutputDir , 'index.mdx ' ) , generatePagesIndex ( ) )
11901228
11911229 console . log (
11921230 `Generated ${ componentOrder . length } component pages and ${ Object . keys ( metadata . pages ) . length } page type pages.`
0 commit comments