@@ -74,9 +74,8 @@ export function buildSite(builds: Build[], siteConfig: SiteConfig, properties: P
74
74
75
75
const page = assignIfIncludedSource ( assign , topLevelItem , file , siteConfig ) ;
76
76
77
- const withContext = Object . assign ( topLevelItem , {
78
- __item_context : { page, item : topLevelItem as DocItemWithContext , file, build } ,
79
- } ) ;
77
+ const withContext = defineContext ( topLevelItem , build , file , page ) ;
78
+ defineProperties ( withContext , properties ) ;
80
79
81
80
if ( isNewFile && page !== undefined ) {
82
81
( pages [ page ] ??= [ ] ) . push ( withContext ) ;
@@ -91,26 +90,32 @@ export function buildSite(builds: Build[], siteConfig: SiteConfig, properties: P
91
90
if ( ! isDocItem ( item ) ) continue ;
92
91
if ( isNewFile && page !== undefined ) items . push ( item as DocItemWithContext ) ;
93
92
const contract = topLevelItem . nodeType === 'ContractDefinition' ? topLevelItem : undefined ;
94
- const __item_context : DocItemContext = { page , item : item as DocItemWithContext , contract , file, build } ;
95
- Object . assign ( item , { __item_context } ) ;
93
+ const withContext = defineContext ( item , build , file , page , contract ) ;
94
+ defineProperties ( withContext , properties ) ;
96
95
}
97
96
}
98
97
}
99
98
}
100
99
101
- for ( const item of items ) {
102
- for ( const [ prop , fn ] of Object . entries ( properties ) ) {
103
- const original : unknown = ( item as any ) [ prop ] ;
104
- defineGetterMemoized ( item as any , prop , ( ) => fn ( item . __item_context , original ) ) ;
105
- }
106
- }
107
-
108
100
return {
109
101
items,
110
102
pages : Object . entries ( pages ) . map ( ( [ id , pageItems ] ) => ( { id, items : pageItems } ) ) ,
111
103
} ;
112
104
}
113
105
106
+ function defineContext ( item : DocItem , build : BuildContext , file : SourceUnit , page ?: string , contract ?: ContractDefinition ) : DocItemWithContext {
107
+ return Object . assign ( item , {
108
+ [ DOC_ITEM_CONTEXT ] : { build, file, contract, page, item : item as DocItemWithContext } ,
109
+ } ) ;
110
+ }
111
+
112
+ function defineProperties ( item : DocItemWithContext , properties : Properties ) {
113
+ for ( const [ prop , fn ] of Object . entries ( properties ) ) {
114
+ const original : unknown = ( item as any ) [ prop ] ;
115
+ defineGetterMemoized ( item as any , prop , ( ) => fn ( item . __item_context , original ) ) ;
116
+ }
117
+ }
118
+
114
119
function assignIfIncludedSource (
115
120
assign : PageAssigner ,
116
121
item : DocItem ,
0 commit comments